Repository: feincms/feincms3-sites (language: python)

Issue:
Feature: Add a default language list filter for convenience
As requested by you, here is a PR that adds a simple list filter for the default language.

By default, the filter will not be added to the site admin and the user can use it in a custom version of the admin when required.


Discussion / hints:

Fix patch:
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index d057820..82f3d92 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -5,6 +5,13 @@ Change log
 `Next version`_
 ~~~~~~~~~~~~~~~
 
+- Add DefaultLanguageListFilter to limit choices to settings.LANGUAGE
+- Add default list_filter to SiteAdmin (is_active, host, default_language)
+
+
+`0.13.1`_ (2021-10-12)
+~~~~~~~~~~~~~~~~~~~~~~
+
 - Fixed exception when using custom site models.
 
 
diff --git a/feincms3_sites/admin.py b/feincms3_sites/admin.py
index 0ce1862..7f13723 100644
--- a/feincms3_sites/admin.py
+++ b/feincms3_sites/admin.py
@@ -8,6 +8,24 @@
 from .models import Site
 
 
+class DefaultLanguageListFilter(admin.SimpleListFilter):
+    """
+    Simple list filter for the default_language property.
+    """
+
+    title = capfirst(_("default language"))
+    parameter_name = "default_language"
+
+    def lookups(self, request, model_admin):
+        return [("", capfirst(_("no language")))] + list(settings.LANGUAGES)
+
+    def queryset(self, request, queryset):
+        if self.value():
+            return queryset.filter(default_language=self.value())
+        else:
+            return queryset.all()
+
+
 class SiteForm(forms.ModelForm):
     default_language = Site._meta.get_field("default_language").formfield(
         choices=[("", "----------")] + list(settings.LANGUAGES),
@@ -42,3 +60,4 @@ class SiteAdmin(admin.ModelAdmin):
     ]
     list_editable = ["is_active", "is_default"]
     ordering = ["-is_default", "host"]
+    list_filter = ["is_active", "host", DefaultLanguageListFilter]
diff --git a/feincms3_sites/locale/de/LC_MESSAGES/django.mo b/feincms3_sites/locale/de/LC_MESSAGES/django.mo
index c8245d5..2d6c1f9 100644
Binary files a/feincms3_sites/locale/de/LC_MESSAGES/django.mo and b/feincms3_sites/locale/de/LC_MESSAGES/django.mo differ
diff --git a/feincms3_sites/locale/de/LC_MESSAGES/django.po b/feincms3_sites/locale/de/LC_MESSAGES/django.po
index c4cffda..75dbec7 100644
--- a/feincms3_sites/locale/de/LC_MESSAGES/django.po
+++ b/feincms3_sites/locale/de/LC_MESSAGES/django.po
@@ -8,7 +8,7 @@ msgid ""
 msgstr ""
 "Project-Id-Version: PACKAGE VERSION\n"
 "Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2018-10-02 13:52+0200\n"
+"POT-Creation-Date: 2021-10-20 11:40+0200\n"
 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
 "Language-Team: LANGUAGE <LL@li.org>\n"
@@ -18,39 +18,47 @@ msgstr ""
 "Content-Transfer-Encoding: 8bit\n"
 "Plural-Forms: nplurals=2; plural=(n != 1);\n"
 
-#: admin.py:27
+#: admin.py:15 models.py:42
+msgid "default language"
+msgstr "Standardsprache"
+
+#: admin.py:19
+msgid "no language"
+msgstr "Keine Sprache"
+
+#: admin.py:44
 msgid "advanced"
 msgstr "Fortgeschritten"
 
-#: apps.py:8 models.py:56
+#: apps.py:10 models.py:58
 msgid "sites"
 msgstr "Webseiten"
 
 #: models.py:32
+msgid "is active"
+msgstr ""
+
+#: models.py:33
 msgid "is default"
 msgstr "ist standard"
 
-#: models.py:33
+#: models.py:34
 msgid "host"
 msgstr "Domainname"
 
-#: models.py:35
+#: models.py:36
 msgid "manage the host regex"
 msgstr "Domain-Regex verwalten"
 
-#: models.py:37
+#: models.py:38
 msgid "Deactivate this to specify the regex yourself."
 msgstr "Deaktivieren, um die Domain-Regex selbst zu setzen."
 
-#: models.py:39
+#: models.py:40
 msgid "host regular expression"
 msgstr "Domain-Regex"
 
-#: models.py:41
-msgid "default language"
-msgstr "Standardsprache"
-
-#: models.py:47
+#: models.py:48
 msgid ""
 "The default language will be overridden by more specific settings such as "
 "the language of individual pages."
@@ -58,23 +66,19 @@ msgstr ""
 "Die Standardsprache wird von spezifischeren Einstellungen überschrieben wie "
 "beispielsweise der Sprache von einzelnen Seiten."
 
-#: models.py:55 models.py:106
+#: models.py:57 models.py:165
 msgid "site"
 msgstr "Webseite"
 
-#: models.py:66
-msgid "Only one site can be the default site."
-msgstr "Nur eine Webseite kann die Standard-Webseite sein."
-
-#: models.py:74
-msgid "The regular expression does not match the host."
-msgstr "Der Domainname passt nicht zur Domain-Regex."
-
-#: models.py:78
+#: models.py:71
 #, python-format
 msgid "Error while validating the regular expression: %s"
 msgstr "Fehler während Validierung des Domain-Regex: %s"
 
+#: models.py:76
+msgid "The regular expression does not match the host."
+msgstr "Der Domainname passt nicht zur Domain-Regex."
+
 #: models.py:111
 msgid "path"
 msgstr "Pfad"
@@ -87,14 +91,17 @@ msgstr "Automatisch generiert falls 'statischer Pfad' nicht gesetzt ist."
 msgid "Path must start and end with a slash (/)."
 msgstr "Pfad muss mit einem Slash (/) starten und enden."
 
-#: models.py:128
+#: models.py:129
 msgid "page"
 msgstr "Seite"
 
-#: models.py:129
+#: models.py:130
 msgid "pages"
 msgstr "Seiten"
 
-#: models.py:149
+#: models.py:148
 msgid "The site is required when creating root nodes."
 msgstr "Die Webseite wird zur Erstellung von Wurzelobjekten benötigt."
+
+#~ msgid "Only one site can be the default site."
+#~ msgstr "Nur eine Webseite kann die Standard-Webseite sein."


Test patch:
diff --git a/tests/testapp/test_feincms3.py b/tests/testapp/test_feincms3.py
index dd60e0c..53a5225 100644
--- a/tests/testapp/test_feincms3.py
+++ b/tests/testapp/test_feincms3.py
@@ -507,6 +507,30 @@ def test_language(self):
         )
 
 
+class SiteAdminTest(TestCase):
+    def setUp(self):
+        self.user = User.objects.create_superuser("admin", "admin@test.ch", "blabla")
+        deactivate_all()
+
+        self.test_site = Site.objects.create(host="testserver", is_default=True)
+
+    def test_default_language_list_filter(self):
+        self.client.login(username="admin", password="blabla")
+        response = self.client.get("/admin/feincms3_sites/site/")
+        self.assertContains(response, "<h3> By Default language </h3>", 1)
+        self.assertContains(
+            response,
+            '<a href="?default_language=" title="No language">No language</a>',
+            1,
+        )
+        self.assertContains(
+            response, '<a href="?default_language=en" title="English">English</a>', 1
+        )
+        self.assertContains(
+            response, '<a href="?default_language=de" title="German">German</a>', 1
+        )
+
+
 class SiteModelDeclaredTest(TestCase):
     def setUp(self):
         self.user = User.objects.create_superuser("admin", "admin@test.ch", "blabla")