ask user when ambigous tags found
authorRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Mon, 14 Jun 2010 13:15:14 +0000 (15:15 +0200)
committerRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Mon, 14 Jun 2010 13:15:14 +0000 (15:15 +0200)
apps/catalogue/models.py
apps/catalogue/views.py
wolnelektury/templates/catalogue/differentiate_tags.html [new file with mode: 0644]

index d63581f..2008f09 100644 (file)
@@ -102,8 +102,10 @@ class Tag(TagBase):
     def get_tag_list(tags):
         if isinstance(tags, basestring):
             real_tags = []
+            ambiguous_slugs = []
             category = None
-            for name in tags.split('/'):
+            tags_splitted = tags.split('/')
+            for index, name in enumerate(tags_splitted):
                 if name in Tag.categories_rev:
                     category = Tag.categories_rev[name]
                 else:
@@ -111,10 +113,22 @@ class Tag(TagBase):
                         real_tags.append(Tag.objects.get(slug=name, category=category))
                         category = None
                     else:
-                        real_tags.append(Tag.objects.get(slug=name))
+                        try:
+                            real_tags.append(Tag.objects.exclude(category='book').get(slug=name))
+                        except Tag.MultipleObjectsReturned, e:
+                            ambiguous_slugs.append(name)
+                            
             if category:
-                raise Http404
-            return real_tags
+                # something strange left off
+                raise Tag.DoesNotExist()
+            if ambiguous_slugs:
+                # some tags should be qualified
+                e = Tag.MultipleObjectsReturned()
+                e.tags = real_tags
+                e.ambiguous_slugs = ambiguous_slugs
+                raise e
+            else:
+                return real_tags
         else:
             return TagBase.get_tag_list(tags)
     
index 745ff10..b04cf43 100644 (file)
@@ -73,11 +73,27 @@ def book_list(request):
         context_instance=RequestContext(request))
 
 
+def differentiate_tags(request, tags, ambiguous_slugs):
+    beginning = '/'.join(tag.url_chunk for tag in tags)
+    unparsed = '/'.join(ambiguous_slugs[1:])
+    options = []
+    for tag in models.Tag.objects.exclude(category='book').filter(slug=ambiguous_slugs[0]):
+        options.append({
+            'url_args': '/'.join((beginning, tag.url_chunk, unparsed)).rstrip('/'),
+            'tags': tags + [tag]
+        })
+    return render_to_response('catalogue/differentiate_tags.html',
+                {'tags': tags, 'options': options, 'unparsed': unparsed}, 
+                context_instance=RequestContext(request))
+
+
 def tagged_object_list(request, tags=''):
     try:
         tags = models.Tag.get_tag_list(tags)
     except models.Tag.DoesNotExist:
         raise Http404
+    except models.Tag.MultipleObjectsReturned, e:
+        return differentiate_tags(request, e.tags, e.ambiguous_slugs)
 
     try:
         if len(tags) > settings.MAX_TAG_LIST:
diff --git a/wolnelektury/templates/catalogue/differentiate_tags.html b/wolnelektury/templates/catalogue/differentiate_tags.html
new file mode 100644 (file)
index 0000000..86e34bb
--- /dev/null
@@ -0,0 +1,28 @@
+{% extends "base.html" %}
+{% load i18n %}
+{% load catalogue_tags %}
+
+{% block title %}{% title_from_tags tags %} w WolneLektury.pl{% endblock %}
+
+{% block bodyid %}differentiate_tags{% endblock %}
+
+{% block body %}
+    <h1>{% title_from_tags tags %}</h1>
+    {% breadcrumbs tags %}
+    
+       <p>{% trans "The criteria are ambiguous. Please select one of the following options:" %}</p>
+    <div id="books-list">
+        {% for option in options %}
+        <div class="book-description"
+            <p><a href="{% url tagged_object_list option.url_args %}">{% title_from_tags option.tags %}{% if unparsed %}, &hellip;{% endif %}</a></p>
+               </div>
+        {% endfor %}
+    </div>
+
+    <div id="set-window">
+        <div class="header"><a href="#" class="jqmClose">{% trans "Close" %}</a></div>
+        <div class="target">
+            <p><img src="{{ STATIC_URL }}img/indicator.gif" alt="*"/> {% trans "Loading" %}</p>
+        </div>
+    </div>
+{% endblock %}
\ No newline at end of file