From af5d82d4f0314cc9c9daa158b9398ee8688b8594 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Mon, 14 Jun 2010 15:15:14 +0200 Subject: [PATCH] ask user when ambigous tags found --- apps/catalogue/models.py | 22 ++++++++++++--- apps/catalogue/views.py | 16 +++++++++++ .../catalogue/differentiate_tags.html | 28 +++++++++++++++++++ 3 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 wolnelektury/templates/catalogue/differentiate_tags.html diff --git a/apps/catalogue/models.py b/apps/catalogue/models.py index d63581f47..2008f0970 100644 --- a/apps/catalogue/models.py +++ b/apps/catalogue/models.py @@ -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) diff --git a/apps/catalogue/views.py b/apps/catalogue/views.py index 745ff10e2..b04cf43c5 100644 --- a/apps/catalogue/views.py +++ b/apps/catalogue/views.py @@ -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 index 000000000..86e34bb40 --- /dev/null +++ b/wolnelektury/templates/catalogue/differentiate_tags.html @@ -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 %} +

{% title_from_tags tags %}

+ {% breadcrumbs tags %} + +

{% trans "The criteria are ambiguous. Please select one of the following options:" %}

+
+ {% for option in options %} + + {% endfor %} +
+ +
+ +
+

* {% trans "Loading" %}

+
+
+{% endblock %} \ No newline at end of file -- 2.20.1