Repository: mozmeao/snippets-service

Issue:
Icon model: Add used in currently published snippet filter
Add a list filter for icons, so we can identify which icons are currently published and thus prioritize their update to larger sizes and better quality


Discussion:

Resolving patch:
diff --git a/snippets/base/admin/adminmodels.py b/snippets/base/admin/adminmodels.py
--- a/snippets/base/admin/adminmodels.py
+++ b/snippets/base/admin/adminmodels.py
@@ -122,8 +122,13 @@ class IconAdmin(admin.ModelAdmin):
         'name',
         'width',
         'height',
+        'number_of_snippets',
+        'number_of_published_snippets',
         'preview',
     ]
+    list_filter = [
+        filters.IconPublishedFilter,
+    ]
 
     class Media:
         css = {
@@ -150,6 +155,12 @@ def snippets(self, obj):
         template = get_template('base/snippets_related_with_obj.jinja')
         return mark_safe(template.render({'snippets': obj.snippets, 'type': 'Icon'}))
 
+    def number_of_snippets(self, obj):
+        return obj.snippets.count()
+
+    def number_of_published_snippets(self, obj):
+        return obj.snippets.filter(status=models.STATUS_CHOICES['Published']).count()
+
 
 class SimpleTemplateInline(admin.StackedInline):
     model = models.SimpleTemplate
diff --git a/snippets/base/admin/filters.py b/snippets/base/admin/filters.py
--- a/snippets/base/admin/filters.py
+++ b/snippets/base/admin/filters.py
@@ -6,7 +6,7 @@
 from django.utils.encoding import force_text
 
 from snippets.base.managers import SnippetQuerySet
-from snippets.base.models import Template
+from snippets.base import models
 
 
 class ModifiedFilter(admin.SimpleListFilter):
@@ -110,7 +110,7 @@ class TemplateFilter(admin.SimpleListFilter):
     LOOKUPS = sorted([
         (model.__name__, model.NAME)
         for model in apps.get_models()
-        if issubclass(model, Template) and not model.__name__ == 'Template'
+        if issubclass(model, models.Template) and not model.__name__ == 'Template'
     ], key=lambda x: x[1])
 
     def lookups(self, request, model_admin):
@@ -127,3 +127,28 @@ def queryset(self, request, queryset):
                 filters['template_relation__{}'.format(k.lower())] = None
 
         return queryset.filter(**filters)
+
+
+class IconPublishedFilter(admin.SimpleListFilter):
+    title = 'Currently Published'
+    parameter_name = 'is_currently_published'
+
+    def lookups(self, request, model_admin):
+        return (
+            ('yes', 'Yes'),
+            ('no', 'No'),
+        )
+
+    def queryset(self, request, queryset):
+        if self.value() is None:
+            return queryset
+
+        icon_ids = []
+        for icon in queryset.all():
+            if icon.snippets.filter(status=models.STATUS_CHOICES['Published']).count():
+                icon_ids.append(icon.id)
+
+        if self.value() == 'yes':
+            return queryset.filter(id__in=icon_ids)
+        elif self.value() == 'no':
+            return queryset.exclude(id__in=icon_ids)
diff --git a/snippets/base/models.py b/snippets/base/models.py
--- a/snippets/base/models.py
+++ b/snippets/base/models.py
@@ -600,7 +600,7 @@ def snippets(self):
                 if related_snippets:
                     all_snippets.extend(related_snippets)
 
-        return ASRSnippet.objects.filter(pk__in=all_snippets)
+        return ASRSnippet.objects.filter(pk__in=all_snippets).distinct()
 
 
 class Template(models.Model):


Test patch: