Add search in tags only.
authorAlexandre Segura <mex.zktk@gmail.com>
Mon, 22 May 2017 11:39:26 +0200
changeset 515 c1077e8f595d
parent 514 accd1fded1a5
child 516 6c81f780fb74
Add search in tags only.
src/iconolab/search_indexes/forms.py
src/iconolab/search_indexes/views.py
src/iconolab/templates/iconolab/home.html
src/iconolab/views/objects.py
--- a/src/iconolab/search_indexes/forms.py	Fri May 19 16:05:10 2017 +0200
+++ b/src/iconolab/search_indexes/forms.py	Mon May 22 11:39:26 2017 +0200
@@ -8,6 +8,7 @@
 class IconolabSearchForm(SearchForm):
 
     model_type = forms.ChoiceField(required=False, choices=(("images","Images"), ("annotations","Annotations")) )
+    tags = forms.BooleanField(required=False, initial=False)
 
     def __init__(self, *args, **kwargs):
         self.collection_name = kwargs.pop("collection_name")
@@ -30,7 +31,7 @@
         qs = self.get_model_type_queryset(self.searchqueryset, selected_type).load_all()
         return qs
 
-    def get_model_type_queryset(self, qs, model_type):
+    def get_model_type_queryset(self, qs, model_type, tags_only):
 
         if model_type == 'images':
             qs = qs.models(Item).load_all_queryset(Item, Item.objects.select_related('collection', 'metadatas'))
@@ -40,13 +41,19 @@
         if self.collection_name is not None:
             qs = qs.filter(collection = self.collection_name)
 
+        if tags_only:
+            qs = qs.filter(tags=self.cleaned_data.get("q"))
+
         return qs
 
     def search(self):
         selected_type = self.cleaned_data.get("model_type")
+        tags_only = self.cleaned_data.get("tags")
+
         qs = super(IconolabSearchForm, self).search()
+
         if qs.count() == 0:
             return qs
         else:
-            qs = self.get_model_type_queryset(qs, selected_type).load_all()
+            qs = self.get_model_type_queryset(qs, selected_type, tags_only).load_all()
         return qs
--- a/src/iconolab/search_indexes/views.py	Fri May 19 16:05:10 2017 +0200
+++ b/src/iconolab/search_indexes/views.py	Mon May 22 11:39:26 2017 +0200
@@ -18,7 +18,7 @@
         "annotations": "search/annotation_search.html"
     }
 
-    def complete_url(self, url):
+    def complete_url(self, url, tags_only):
         query = self.request.GET.get("q", None)
         page = self.request.GET.get("page", None)
 
@@ -31,6 +31,9 @@
         if page is not None:
             queryargs.append("page=" + page)
 
+        if tags_only:
+            queryargs.append("tags=1")
+
         if len(queryargs):
             query_string = "&".join(queryargs)
             url += "?" + query_string
@@ -43,14 +46,19 @@
         self.paginate_by = request.GET.get('perpage', 10)
         collection_name = self.kwargs.get('collection_name', None)
 
+        tags_only = False
+        if (self.model_type == 'tags'):
+            self.model_type = 'annotations'
+            tags_only = True
+
         if self.model_type is not None:
             if collection_name is None:
                 #redirect to all_model_type
                 redirect_url = reverse('search_indexes:model_search', kwargs={'model_type': self.model_type})
-                return redirect(self.complete_url(redirect_url))
+                return redirect(self.complete_url(redirect_url, tags_only))
             else:
                 redirect_url = reverse('search_indexes:collection_with_model_search', kwargs={'collection_name': collection_name, 'model_type':self.model_type})
-                return redirect(self.complete_url(redirect_url))
+                return redirect(self.complete_url(redirect_url, tags_only))
         else:
             has_error, redirectView = self.check_kwargs(**kwargs)
             if has_error:
--- a/src/iconolab/templates/iconolab/home.html	Fri May 19 16:05:10 2017 +0200
+++ b/src/iconolab/templates/iconolab/home.html	Mon May 22 11:39:26 2017 +0200
@@ -101,7 +101,7 @@
         {% for most_accurate_tag in most_accurate_tags %}
         <li class="list-group-item">
           <span class="badge">{{ most_accurate_tag.annotation_count }}</span>
-          <a href="{% url 'search_indexes:model_search' 'annotations' %}?q={{ most_accurate_tag.tag.label }}">
+          <a href="{% url 'search_indexes:model_search' 'annotations' %}?q={{ most_accurate_tag.tag.label }}&tags=1">
             {{ most_accurate_tag.tag.label }}
           </a>
         </li>
--- a/src/iconolab/views/objects.py	Fri May 19 16:05:10 2017 +0200
+++ b/src/iconolab/views/objects.py	Mon May 22 11:39:26 2017 +0200
@@ -53,6 +53,13 @@
         context['best_contributors'] = best_contributors
 
         # Most accurate tags (tags with accuracy >= 4)
+        # SELECT ti.tag_id, ar.title, COUNT(DISTINCT(a.id)) AS cnt
+        # FROM iconolab_tagginginfo ti
+        # JOIN iconolab_annotationrevision ar ON ti.revision_id = ar.id
+        # JOIN iconolab_annotation a ON ar.annotation_id = a.id
+        # WHERE ti.accuracy >= 4
+        # GROUP BY ti.tag_id
+        # ORDER BY cnt desc
         rows = TaggingInfo.objects\
             .prefetch_related('revision', 'revision__annotation')\
             .filter(accuracy__gte=4)\