v1.53.1 : better tagging cloud management V01.53.01
authorcavaliet
Mon, 14 Apr 2014 15:17:17 +0200
changeset 1298 1494fc70a5a2
parent 1297 eb12e89fdbb1
child 1299 22db13f77355
v1.53.1 : better tagging cloud management
src/ldt/ldt/__init__.py
src/ldt/ldt/templatetags/taggit_extras_ldt.py
--- a/src/ldt/ldt/__init__.py	Thu Apr 10 15:56:20 2014 +0200
+++ b/src/ldt/ldt/__init__.py	Mon Apr 14 15:17:17 2014 +0200
@@ -1,6 +1,6 @@
 __all__ = ["VERSION", "get_version", "__version__"]
 
-VERSION = (1, 53, 0, "final", 0)
+VERSION = (1, 53, 1, "final", 0)
 
 
 def get_version():
--- a/src/ldt/ldt/templatetags/taggit_extras_ldt.py	Thu Apr 10 15:56:20 2014 +0200
+++ b/src/ldt/ldt/templatetags/taggit_extras_ldt.py	Mon Apr 14 15:17:17 2014 +0200
@@ -32,9 +32,9 @@
     else:
         if isinstance(forvar, str) or isinstance(forvar, SafeText):
             # extract app label and model name
-            beginning, applabel, model = None, None, None
+            _, applabel, model = None, None, None
             try:
-                beginning, applabel, model = forvar.rsplit('.', 2)
+                _, applabel, model = forvar.rsplit('.', 2)
             except ValueError:
                 try:
                     applabel, model = forvar.rsplit('.', 1)
@@ -46,23 +46,25 @@
                 queryset = TaggedItem.objects.filter(content_type__app_label=applabel.lower())
             if model:
                 queryset = queryset.filter(content_type__model=model.lower())
+            tag_ids = queryset.values_list('tag_id', flat=True)
+            queryset = Tag.objects.filter(id__in=tag_ids)
+            
+            if applabel and not model :
+                return queryset.extra(select={ "num_times": 'SELECT count(*) FROM "taggit_taggeditem" U0 INNER JOIN "django_content_type" U1  ON ( U0."content_type_id" = U1."id" ) WHERE ( U1."app_label" = \'' + applabel.lower() + '\' ) AND ("taggit_tag"."id" = U0."tag_id")' })
+            elif applabel and model:
+                return queryset.extra(select={ "num_times": 'SELECT count(*) FROM "taggit_taggeditem" U0 INNER JOIN "django_content_type" U1  ON ( U0."content_type_id" = U1."id" ) WHERE (U1."app_label" = \'' + applabel.lower() + '\'  AND U1."model" = \'' + model.lower() + '\' ) AND ("taggit_tag"."id" = U0."tag_id")' })
+        
         else:
             # forvar is a query set
             content_type = ContentType.objects.get_for_model(forvar[0])
             queryset = TaggedItem.objects.filter(content_type=content_type, object_id__in=[o.pk for o in forvar])
-            
-        # get tags
-        tag_ids = queryset.values_list('tag_id', flat=True)
-        queryset = Tag.objects.filter(id__in=tag_ids)
-            
-
-    # Retain compatibility with older versions of Django taggit
-    # a version check (for example taggit.VERSION <= (0,8,0)) does NOT
-    # work because of the version (0,8,0) of the current dev version of django-taggit
-    try:
-        return queryset.annotate(num_times=Count('taggeditem_items'))
-    except FieldError:
-        return queryset.annotate(num_times=Count('taggit_taggeditem_items'))
+            # get tags
+            tag_ids = queryset.values_list('tag_id', flat=True)
+            queryset = Tag.objects.filter(id__in=tag_ids)
+            return queryset.annotate(num_times=Count('taggit_taggeditem_items'))
+    
+    return queryset.annotate(num_times=Count('taggit_taggeditem_items'))
+    
 
 def get_weight_fun(t_min, t_max, f_min, f_max):
     def weight_fun(f_i, t_min=t_min, t_max=t_max, f_min=f_min, f_max=f_max):