src/iconolab/views/userpages.py
changeset 246 895563933e8f
parent 245 51c9ac85fac0
child 283 d8fcfac848ed
--- a/src/iconolab/views/userpages.py	Tue Nov 15 14:42:02 2016 +0100
+++ b/src/iconolab/views/userpages.py	Tue Nov 15 15:10:56 2016 +0100
@@ -190,23 +190,32 @@
             annotation_queryset = annotation_queryset.filter(current_revision__tagginginfo__accuracy__gte=min_accuracy)
         
         metacategories_filter = []
-        mtcg_annotations_ids = []
+        mtcg_annotations_ids = {}
         filtering_on_metacategories = False
         context["qarg_metacategories_filters"] = {}
         for metacategory in collection.metacategories.all():
-            mtcg_filter = request.GET.get("min_metacategory_"+str(metacategory.id), "")
-            if mtcg_filter and mtcg_filter.isdigit():
+            # Default filter is 0
+            mtcg_filter = request.GET.get("min_metacategory_"+str(metacategory.id), "0")
+            # We ignore filters that aren't integers
+            if mtcg_filter and mtcg_filter.isdigit(): 
+                # For each metacategory we have a dict entry with key=id that will be a list of the annotation matching the filter
+                mtcg_annotations_ids[str(metacategory.id)] = []
+                # Queryarg for autocompleting the form on page load
                 context["qarg_metacategories_filters"]["min_metacategory_"+str(metacategory.id)] = mtcg_filter
+                # If we got this far we did a filter on metacategories
                 filtering_on_metacategories = True
                 for annotation in annotation_queryset.all():
-                    if MetaCategoriesCountInfo.objects.filter(metacategory=metacategory, annotation_stats_obj=annotation.stats, count__gte=int(mtcg_filter)).exists():
-                        mtcg_annotations_ids.append(annotation.annotation_guid)
-#                 mtcg_annotations_ids.append(
-#                     annotation_queryset.filter(
-#                         stats__metacategoriescountinfo_set__metacategory__id=metacategory.id,
-#                     ).values_list('annotation_guid', flat=True))
+                    if int(mtcg_filter) == 0 or MetaCategoriesCountInfo.objects.filter(metacategory=metacategory, annotation_stats_obj=annotation.stats, count__gte=int(mtcg_filter)).exists():
+                         mtcg_annotations_ids[str(metacategory.id)].append(annotation.annotation_guid)
         if filtering_on_metacategories:
-            annotation_queryset = annotation_queryset.filter(annotation_guid__in=mtcg_annotations_ids)
+            # If we did a filter on metacategories we have several list of matching annotations. We only display the intersection of all these lists.
+            annotation_queryset = annotation_queryset.filter(annotation_guid__in=
+                list(
+                     set.intersection(*[
+                        set(value) for value in list(mtcg_annotations_ids.values())
+                    ])
+                )
+            )
         
         # ordering
         ordering = []