bookmedia - fix overwriting and tests
[wolnelektury.git] / src / catalogue / models / tag.py
index 57935f8..00bdcb5 100644 (file)
@@ -7,8 +7,10 @@ from django.core.cache import caches
 from django.contrib.auth.models import User
 from django.db import models
 from django.db.models import permalink
+from django.db.models.query import Prefetch
 from django.dispatch import Signal
 from django.utils.translation import ugettext_lazy as _
+
 from newtagging.models import TagBase
 from ssify import flush_ssi_includes
 
@@ -48,7 +50,9 @@ class Tag(TagBase):
     after_change = Signal(providing_args=['instance', 'languages'])
 
     class UrlDeprecationWarning(DeprecationWarning):
-        pass
+        def __init__(self, tags=None):
+            super(Tag.UrlDeprecationWarning, self).__init__()
+            self.tags = tags
 
     categories_rev = {
         'autor': 'author',
@@ -157,24 +161,24 @@ class Tag(TagBase):
     has_description.boolean = True
 
     @staticmethod
-    def get_tag_list(tags):
-        if isinstance(tags, basestring):
-            if not tags:
+    def get_tag_list(tag_str):
+        if isinstance(tag_str, basestring):
+            if not tag_str:
                 return []
-            real_tags = []
+            tags = []
             ambiguous_slugs = []
             category = None
             deprecated = False
-            tags_splitted = tags.split('/')
+            tags_splitted = tag_str.split('/')
             for name in tags_splitted:
                 if category:
-                    real_tags.append(Tag.objects.get(slug=name, category=category))
+                    tags.append(Tag.objects.get(slug=name, category=category))
                     category = None
                 elif name in Tag.categories_rev:
                     category = Tag.categories_rev[name]
                 else:
                     try:
-                        real_tags.append(Tag.objects.get(slug=name))
+                        tags.append(Tag.objects.get(slug=name))
                         deprecated = True
                     except Tag.MultipleObjectsReturned:
                         ambiguous_slugs.append(name)
@@ -185,16 +189,14 @@ class Tag(TagBase):
             if ambiguous_slugs:
                 # some tags should be qualified
                 e = Tag.MultipleObjectsReturned()
-                e.tags = real_tags
+                e.tags = tags
                 e.ambiguous_slugs = ambiguous_slugs
                 raise e
             if deprecated:
-                e = Tag.UrlDeprecationWarning()
-                e.tags = real_tags
-                raise e
-            return real_tags
+                raise Tag.UrlDeprecationWarning(tags=tags)
+            return tags
         else:
-            return TagBase.get_tag_list(tags)
+            return TagBase.get_tag_list(tag_str)
 
     @property
     def url_chunk(self):
@@ -245,3 +247,18 @@ class Tag(TagBase):
 
 # Pickle complains about not having this.
 TagRelation = Tag.intermediary_table_model
+
+
+def prefetch_relations(objects, category, only_name=True):
+    queryset = TagRelation.objects.filter(tag__category=category).select_related('tag')
+    if only_name:
+        queryset = queryset.only('tag__name_pl', 'object_id')
+    return objects.prefetch_related(
+        Prefetch('tag_relations', queryset=queryset, to_attr='%s_relations' % category))
+
+
+def prefetched_relations(obj, category):
+    if hasattr(obj, '%s_relations' % category):
+        return getattr(obj, '%s_relations' % category)
+    else:
+        return None