Repository: mozmeao/snippets-service

Issue:
ASRSnippet list view, filter by template type



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
@@ -451,6 +451,7 @@ class ASRSnippetAdmin(admin.ModelAdmin):
     )
     list_filter = (
         filters.ModifiedFilter,
+        filters.TemplateFilter,
         ('locales', RelatedOnlyDropdownFilter),
         'status',
         filters.ChannelFilter,
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
@@ -1,10 +1,12 @@
 from distutils.util import strtobool
 from datetime import datetime, timedelta
 
+from django.apps import apps
 from django.contrib import admin
 from django.utils.encoding import force_text
 
 from snippets.base.managers import SnippetQuerySet
+from snippets.base.models import Template
 
 
 class ModifiedFilter(admin.SimpleListFilter):
@@ -100,3 +102,28 @@ def queryset(self, request, queryset):
             return queryset.exclude(publish_start=None, publish_end=None)
         else:
             return queryset.filter(publish_start=None, publish_end=None)
+
+
+class TemplateFilter(admin.SimpleListFilter):
+    title = 'template type'
+    parameter_name = 'template'
+    LOOKUPS = sorted([
+        (model.__name__, model.NAME)
+        for model in apps.get_models()
+        if issubclass(model, Template) and not model.__name__ == 'Template'
+    ], key=lambda x: x[1])
+
+    def lookups(self, request, model_admin):
+        return self.LOOKUPS
+
+    def queryset(self, request, queryset):
+        value = self.value()
+        if not value:
+            return queryset
+
+        filters = {}
+        for k, v in self.LOOKUPS:
+            if value != k:
+                filters['template_relation__{}'.format(k.lower())] = None
+
+        return queryset.filter(**filters)
diff --git a/snippets/base/models.py b/snippets/base/models.py
--- a/snippets/base/models.py
+++ b/snippets/base/models.py
@@ -659,6 +659,7 @@ def get_main_url(self):
 
 class SimpleTemplate(Template):
     VERSION = '1.0.0'
+    NAME = 'Simple'
 
     title_icon = models.ForeignKey(
         Icon,
@@ -765,6 +766,7 @@ def get_rich_text_fields(self):
 class FundraisingTemplate(Template):
     """Also known as EOY Template"""
     VERSION = '1.0.0'
+    NAME = 'Fundraising'
 
     donation_form_url = models.URLField(
         default='https://donate.mozilla.org/?utm_source=desktop-snippet&utm_medium=snippet',
@@ -885,6 +887,7 @@ def get_rich_text_fields(self):
 
 class FxASignupTemplate(Template):
     VERSION = '1.0.0'
+    NAME = 'FxA Sign up'
 
     scene1_title_icon = models.ForeignKey(
         Icon,
@@ -1008,6 +1011,7 @@ def get_main_body(self, bleached=False):
 
 class NewsletterTemplate(Template):
     VERSION = '1.0.0'
+    NAME = 'Newsletter'
 
     scene1_title_icon = models.ForeignKey(
         Icon,
@@ -1151,6 +1155,7 @@ def get_main_body(self, bleached=False):
 
 class SendToDeviceTemplate(Template):
     VERSION = '1.0.0'
+    NAME = 'Send to Device'
 
     scene1_title_icon = models.ForeignKey(
         Icon,
@@ -1324,6 +1329,7 @@ def get_main_body(self, bleached=False):
 
 class SimpleBelowSearchTemplate(Template):
     VERSION = '1.0.0'
+    NAME = 'Simple below Search Bar'
 
     text = models.TextField(
         help_text='Main body text of snippet. HTML subset allowed: i, b, u, strong, em, br',


Test patch: