From d467f1fa349730919e9fe906287e63563c888063 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Tue, 25 May 2010 10:48:12 +0200 Subject: [PATCH] fixed theme counts (#318, #319) --- apps/catalogue/views.py | 67 ++++++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 18 deletions(-) diff --git a/apps/catalogue/views.py b/apps/catalogue/views.py index cf1461feb..de72b046a 100644 --- a/apps/catalogue/views.py +++ b/apps/catalogue/views.py @@ -83,28 +83,59 @@ def tagged_object_list(request, tags=''): if len([tag for tag in tags if tag.category == 'book']): raise Http404 - model = models.Book - shelf = [tag for tag in tags if tag.category == 'set'] - shelf_is_set = (len(tags) == 1 and tags[0].category == 'set') - theme_is_set = len([tag for tag in tags if tag.category == 'theme']) > 0 + theme_is_set = [tag for tag in tags if tag.category == 'theme'] + shelf_is_set = len(tags) == 1 and tags[0].category == 'set' + my_shelf_is_set = shelf_is_set and request.user.is_authenticated() and request.user == shelf[0].user + + objects = only_author = pd_counter = categories = None + if theme_is_set: - model = models.Fragment - only_author = len(tags) == 1 and tags[0].category == 'author' - pd_counter = only_author and tags[0].goes_to_pd() - - user_is_owner = (len(shelf) and request.user.is_authenticated() and request.user == shelf[0].user) + shelf_tags = [tag for tag in tags if tag.category == 'set'] + fragment_tags = [tag for tag in tags if tag.category != 'set'] + fragments = models.Fragment.tagged.with_all(fragment_tags) + + if shelf_tags: + books = models.Book.tagged.with_all(shelf_tags) + l_tags = [models.Tag.objects.get(slug = 'l-' + book.slug) for book in books] + fragments = (fragment for fragment in models.Fragment.tagged.with_any(l_tags) if fragment in fragments) + + fragment_keys = (fragment.pk for fragment in fragments) + if fragment_keys: + related_tags = models.Fragment.tags.usage(counts=True, + filters={'pk__in': fragment_keys}, + extra={'where': ["catalogue_tag.category != 'book'"]}) + related_tags = (tag for tag in related_tags if tag not in fragment_tags) + categories = split_tags(related_tags) + + objects = fragments + else: + books = models.Book.tagged.with_all(tags) + l_tags = [models.Tag.objects.get(slug = 'l-' + book.slug) for book in books] + book_keys = [book.pk for book in books] + if book_keys: + related_tags = models.Book.tags.usage(counts=True, + filters={'pk__in': book_keys}, + extra={'where': ["catalogue_tag.category NOT IN ('set', 'book', 'theme')"]}) + categories = split_tags(related_tags) - extra_where = "catalogue_tag.category NOT IN ('set', 'book')" - related_tags = models.Tag.objects.related_for_model(tags, model, counts=True, extra={'where': [extra_where]}) - categories = split_tags(related_tags) - - if not (theme_is_set or shelf_is_set): - model=models.Book.objects.filter(parent=None) + fragment_keys = [fragment.pk for fragment in models.Fragment.tagged.with_any(l_tags)] + if fragment_keys: + categories['theme'] = models.Fragment.tags.usage(counts=True, + filters={'pk__in': fragment_keys}, + extra={'where': ["catalogue_tag.category = 'theme'"]}) + + books = books.exclude(parent__in = book_keys) + objects = books + + if not objects: + only_author = len(tags) == 1 and tags[0].category == 'author' + pd_counter = only_author and tags[0].goes_to_pd() + objects = models.Book.objects.none() return newtagging_views.tagged_object_list( request, tag_model=models.Tag, - queryset_or_model=model, + queryset_or_model=objects, tags=tags, template_name='catalogue/tagged_object_list.html', extra_context = { @@ -112,7 +143,7 @@ def tagged_object_list(request, tags=''): 'shelf_is_set': shelf_is_set, 'only_author': only_author, 'pd_counter': pd_counter, - 'user_is_owner': user_is_owner, + 'user_is_owner': my_shelf_is_set, 'formats_form': forms.DownloadFormatsForm(), }, ) @@ -532,4 +563,4 @@ def clock(request): in a format suitable for Date.parse() """ from datetime import datetime - return HttpResponse(datetime.now().strftime('%Y/%m/%d %H:%M:%S')) \ No newline at end of file + return HttpResponse(datetime.now().strftime('%Y/%m/%d %H:%M:%S')) -- 2.20.1