Workaround for tagged_object_list.
authorRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Wed, 2 Jun 2010 07:55:42 +0000 (09:55 +0200)
committerRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Wed, 2 Jun 2010 07:55:42 +0000 (09:55 +0200)
Reverted newtagging to its original state,
Workaround in tagged_object_list, by using keys and not creating
  complex querysets that newtagging can't handle,
fixed displaying short_html.

apps/catalogue/models.py
apps/catalogue/views.py
apps/newtagging/models.py

index 2097e17..e85462e 100644 (file)
@@ -134,7 +134,7 @@ class Book(models.Model):
         key = '_short_html_%s' % get_language()
         short_html = getattr(self, key)
         
         key = '_short_html_%s' % get_language()
         short_html = getattr(self, key)
         
-        if len(short_html):
+        if short_html and len(short_html):
             return mark_safe(short_html)
         else:
             tags = self.tags.filter(~Q(category__in=('set', 'theme', 'book')))
             return mark_safe(short_html)
         else:
             tags = self.tags.filter(~Q(category__in=('set', 'theme', 'book')))
@@ -345,7 +345,7 @@ class Fragment(models.Model):
     def short_html(self):
         key = '_short_html_%s' % get_language()
         short_html = getattr(self, key)         
     def short_html(self):
         key = '_short_html_%s' % get_language()
         short_html = getattr(self, key)         
-        if len(short_html):
+        if short_html and len(short_html):
             return mark_safe(short_html)
         else:
             book_authors = [mark_safe(u'<a href="%s">%s</a>' % (tag.get_absolute_url(), tag.name)) 
             return mark_safe(short_html)
         else:
             book_authors = [mark_safe(u'<a href="%s">%s</a>' % (tag.get_absolute_url(), tag.name)) 
index aaf57fc..7f37ccb 100644 (file)
@@ -100,27 +100,39 @@ def tagged_object_list(request, tags=''):
             l_tags = [models.Tag.objects.get(slug = 'l-' + book.slug) for book in books]
             fragments = models.Fragment.tagged.with_any(l_tags, fragments)
         
             l_tags = [models.Tag.objects.get(slug = 'l-' + book.slug) for book in books]
             fragments = models.Fragment.tagged.with_any(l_tags, fragments)
         
-        related_tags = models.Tag.objects.usage_for_queryset(fragments, counts=True, 
-                            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)
+        # newtagging goes crazy if we just try:
+        #related_tags = models.Tag.objects.usage_for_queryset(fragments, counts=True, 
+        #                    extra={'where': ["catalogue_tag.category != 'book'"]})
+        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
+            objects = fragments
     else:
         books = models.Book.tagged.with_all(tags).order_by()
         l_tags = [models.Tag.objects.get(slug = 'l-' + book.slug) for book in books]
         book_keys = [book.pk for book in books]
     else:
         books = models.Book.tagged.with_all(tags).order_by()
         l_tags = [models.Tag.objects.get(slug = 'l-' + book.slug) for book in books]
         book_keys = [book.pk for book in books]
-        related_tags = models.Tag.objects.usage_for_queryset(books, counts=True, 
-                            extra={'where': ["catalogue_tag.category NOT IN ('set', 'book', 'theme')"]})
-        related_tags = (tag for tag in related_tags if tag not in tags)
-        categories = split_tags(related_tags)
-
-        fragments = models.Fragment.tagged.with_any(l_tags)
-        categories['theme'] = models.Tag.objects.usage_for_queryset(fragments, counts=True,  
-                            extra={'where': ["catalogue_tag.category = 'theme'"]})
-            
-        books = books.exclude(parent__in = book_keys)
-        objects = books
+        # newtagging goes crazy if we just try:
+        #related_tags = models.Tag.objects.usage_for_queryset(books, counts=True, 
+        #                    extra={'where': ["catalogue_tag.category NOT IN ('set', 'book', 'theme')"]})
+        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)
+    
+            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'
         
     if not objects:
         only_author = len(tags) == 1 and tags[0].category == 'author'
index b70ab9a..2055ec3 100644 (file)
@@ -84,7 +84,7 @@ class TagManager(models.Manager):
         return self.filter(items__content_type__pk=ctype.pk,
                            items__object_id=obj.pk)
     
         return self.filter(items__content_type__pk=ctype.pk,
                            items__object_id=obj.pk)
     
-    def _get_usage(self, model, counts=False, min_count=None, extra_joins=None, extra_criteria=None, params=None, extra=None, extra_tables=None):
+    def _get_usage(self, model, counts=False, min_count=None, extra_joins=None, extra_criteria=None, params=None, extra=None):
         """
         Perform the custom SQL query for ``usage_for_model`` and
         ``usage_for_queryset``.
         """
         Perform the custom SQL query for ``usage_for_model`` and
         ``usage_for_queryset``.
@@ -104,15 +104,15 @@ class TagManager(models.Manager):
         SELECT DISTINCT %(tag_columns)s%(count_sql)s
         FROM
             %(tag)s
         SELECT DISTINCT %(tag_columns)s%(count_sql)s
         FROM
             %(tag)s
-            INNER JOIN %(tagged_item)s AS %(tagged_item_alias)s
-                ON %(tag)s.id = %(tagged_item_alias)s.tag_id
+            INNER JOIN %(tagged_item)s
+                ON %(tag)s.id = %(tagged_item)s.tag_id
             INNER JOIN %(model)s
             INNER JOIN %(model)s
-                ON %(tagged_item_alias)s.object_id = %(model_pk)s
+                ON %(tagged_item)s.object_id = %(model_pk)s
             %%s
             %%s
-        WHERE %(tagged_item_alias)s.content_type_id = %(content_type_id)s
+        WHERE %(tagged_item)s.content_type_id = %(content_type_id)s
             %%s
             %(extra_where)s
             %%s
             %(extra_where)s
-        GROUP BY %(tag_columns)s, %(tag)s.id, %(tag)s.name%(extra_tables)s
+        GROUP BY %(tag_columns)s, %(tag)s.id, %(tag)s.name
         %%s
         ORDER BY %(tag)s.%(ordering)s ASC""" % {
             'tag': qn(self.model._meta.db_table),
         %%s
         ORDER BY %(tag)s.%(ordering)s ASC""" % {
             'tag': qn(self.model._meta.db_table),
@@ -120,11 +120,9 @@ class TagManager(models.Manager):
             'tag_columns': tag_columns,
             'count_sql': counts and (', COUNT(%s)' % model_pk) or '',
             'tagged_item': qn(self.intermediary_table_model._meta.db_table),
             'tag_columns': tag_columns,
             'count_sql': counts and (', COUNT(%s)' % model_pk) or '',
             'tagged_item': qn(self.intermediary_table_model._meta.db_table),
-            'tagged_item_alias': qn('_newtagging_' + self.intermediary_table_model._meta.db_table),
             'model': model_table,
             'model_pk': model_pk,
             'extra_where': extra_where,
             'model': model_table,
             'model_pk': model_pk,
             'extra_where': extra_where,
-            'extra_tables': ''.join((', %s.id' % qn(table)) for table in extra_tables),
             'content_type_id': ContentType.objects.get_for_model(model).pk,
         }
 
             'content_type_id': ContentType.objects.get_for_model(model).pk,
         }
 
@@ -202,12 +200,11 @@ class TagManager(models.Manager):
 
         extra_joins = ' '.join(queryset.query.get_from_clause()[0][1:])
         where, params = queryset.query.where.as_sql()
 
         extra_joins = ' '.join(queryset.query.get_from_clause()[0][1:])
         where, params = queryset.query.where.as_sql()
-        extra_tables = queryset.query.extra_tables
         if where:
             extra_criteria = 'AND %s' % where
         else:
             extra_criteria = ''
         if where:
             extra_criteria = 'AND %s' % where
         else:
             extra_criteria = ''
-        return self._get_usage(queryset.model, counts, min_count, extra_joins, extra_criteria, params, extra, extra_tables=extra_tables)
+        return self._get_usage(queryset.model, counts, min_count, extra_joins, extra_criteria, params, extra)
 
     def related_for_model(self, tags, model, counts=False, min_count=None, extra=None):
         """
 
     def related_for_model(self, tags, model, counts=False, min_count=None, extra=None):
         """