diff --git a/admin_interface/admin.py b/admin_interface/admin.py index 504fe43b..edf474b0 100644 --- a/admin_interface/admin.py +++ b/admin_interface/admin.py @@ -58,6 +58,7 @@ class ThemeAdmin(admin.ModelAdmin): "logo_max_height", "logo_color", "logo_visible", + "static_logo_path", ), }, ), diff --git a/admin_interface/apps.py b/admin_interface/apps.py index 93fe6951..323d45b0 100644 --- a/admin_interface/apps.py +++ b/admin_interface/apps.py @@ -11,3 +11,6 @@ def ready(self): from admin_interface import settings settings.check_installed_apps() + + # must check if LOCAL_FILE_DIR is set in settings + settings.check_settings("LOCAL_FILE_DIR") diff --git a/admin_interface/locale/de/LC_MESSAGES/django.mo b/admin_interface/locale/de/LC_MESSAGES/django.mo index fed60dbd..83c4bcec 100644 Binary files a/admin_interface/locale/de/LC_MESSAGES/django.mo and b/admin_interface/locale/de/LC_MESSAGES/django.mo differ diff --git a/admin_interface/locale/de/LC_MESSAGES/django.po b/admin_interface/locale/de/LC_MESSAGES/django.po index 73683d94..570a00a9 100644 --- a/admin_interface/locale/de/LC_MESSAGES/django.po +++ b/admin_interface/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: 2023-05-10 18:20-0500\n" +"POT-Creation-Date: 2024-03-19 18:27-0500\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -114,6 +114,10 @@ msgstr "Farbe" msgid "visible" msgstr "sichtbar" +#: admin_interface/models.py +msgid "static logo" +msgstr "statisches Logo" + #: admin_interface/models.py msgid "Leave blank to use the default Django logo" msgstr "Dieses Feld leer lassen um das Django Standard Logo zu benutzen" diff --git a/admin_interface/locale/es/LC_MESSAGES/django.mo b/admin_interface/locale/es/LC_MESSAGES/django.mo index 289e0d60..a5bab3bc 100644 Binary files a/admin_interface/locale/es/LC_MESSAGES/django.mo and b/admin_interface/locale/es/LC_MESSAGES/django.mo differ diff --git a/admin_interface/locale/es/LC_MESSAGES/django.po b/admin_interface/locale/es/LC_MESSAGES/django.po index b74e44c9..e5af78d8 100644 --- a/admin_interface/locale/es/LC_MESSAGES/django.po +++ b/admin_interface/locale/es/LC_MESSAGES/django.po @@ -10,7 +10,7 @@ msgid "" msgstr "" "Project-Id-Version: django-admin-interface\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2023-05-10 18:20-0500\n" +"POT-Creation-Date: 2024-03-19 18:27-0500\n" "PO-Revision-Date: 2023-08-04 11:52-0300\n" "Last-Translator: Santiago Muñoz \n" "Language-Team: Spanish \n" @@ -116,6 +116,10 @@ msgstr "color" msgid "visible" msgstr "visible" +#: admin_interface/models.py +msgid "static logo" +msgstr "logo estático" + #: admin_interface/models.py msgid "Leave blank to use the default Django logo" msgstr "Dejar en blanco para usar el logo por defecto de Django" @@ -236,7 +240,8 @@ msgstr "anclar posición" #: admin_interface/models.py msgid "quick remove links for active filters at top of sidebar" -msgstr "remover enlaces para filtros activos en la parte superior de la barra lateral" +msgstr "" +"remover enlaces para filtros activos en la parte superior de la barra lateral" #: admin_interface/models.py msgid "foldable apps" diff --git a/admin_interface/locale/fa/LC_MESSAGES/django.mo b/admin_interface/locale/fa/LC_MESSAGES/django.mo index 537e1295..b17b55b5 100644 Binary files a/admin_interface/locale/fa/LC_MESSAGES/django.mo and b/admin_interface/locale/fa/LC_MESSAGES/django.mo differ diff --git a/admin_interface/locale/fa/LC_MESSAGES/django.po b/admin_interface/locale/fa/LC_MESSAGES/django.po index 90d098f9..3c1d7d76 100644 --- a/admin_interface/locale/fa/LC_MESSAGES/django.po +++ b/admin_interface/locale/fa/LC_MESSAGES/django.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: django-admin-interface\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2023-05-10 18:20-0500\n" +"POT-Creation-Date: 2024-03-19 18:27-0500\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: Amir Ajorloo \n" "Language-Team: Farsi \n" @@ -113,6 +113,10 @@ msgstr "رنگ" msgid "visible" msgstr "نمایان" +#: admin_interface/models.py +msgid "static logo" +msgstr "لوگوی استاتیک" + #: admin_interface/models.py msgid "Leave blank to use the default Django logo" msgstr "برای نمایش لوگوی پیش فرض، خالی بگذارید." diff --git a/admin_interface/locale/fr/LC_MESSAGES/django.mo b/admin_interface/locale/fr/LC_MESSAGES/django.mo index 1f254460..21560b0f 100644 Binary files a/admin_interface/locale/fr/LC_MESSAGES/django.mo and b/admin_interface/locale/fr/LC_MESSAGES/django.mo differ diff --git a/admin_interface/locale/fr/LC_MESSAGES/django.po b/admin_interface/locale/fr/LC_MESSAGES/django.po index 5b5f1464..2af15641 100644 --- a/admin_interface/locale/fr/LC_MESSAGES/django.po +++ b/admin_interface/locale/fr/LC_MESSAGES/django.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2023-05-10 18:20-0500\n" +"POT-Creation-Date: 2024-03-19 18:27-0500\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -114,6 +114,10 @@ msgstr "couleur" msgid "visible" msgstr "visible" +#: admin_interface/models.py +msgid "static logo" +msgstr "logo statique" + #: admin_interface/models.py msgid "Leave blank to use the default Django logo" msgstr "Laisser vide pour utiliser le logo Django par défaut" diff --git a/admin_interface/locale/it/LC_MESSAGES/django.mo b/admin_interface/locale/it/LC_MESSAGES/django.mo index a75aa70a..ab971e31 100644 Binary files a/admin_interface/locale/it/LC_MESSAGES/django.mo and b/admin_interface/locale/it/LC_MESSAGES/django.mo differ diff --git a/admin_interface/locale/it/LC_MESSAGES/django.po b/admin_interface/locale/it/LC_MESSAGES/django.po index 580ab43d..11acc62a 100644 --- a/admin_interface/locale/it/LC_MESSAGES/django.po +++ b/admin_interface/locale/it/LC_MESSAGES/django.po @@ -9,7 +9,7 @@ msgid "" msgstr "" "Project-Id-Version: django-admin-interface\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2023-05-10 18:20-0500\n" +"POT-Creation-Date: 2024-03-19 18:27-0500\n" "PO-Revision-Date: 2018-12-30 19:13-0500\n" "Last-Translator: Fabio Caccamo \n" "Language-Team: Italian \n" @@ -115,6 +115,10 @@ msgstr "colore" msgid "visible" msgstr "visibile" +#: admin_interface/models.py +msgid "static logo" +msgstr "logo statico" + #: admin_interface/models.py msgid "Leave blank to use the default Django logo" msgstr "Lasciare vuoto per usare il logo Django di default" diff --git a/admin_interface/locale/pl/LC_MESSAGES/django.mo b/admin_interface/locale/pl/LC_MESSAGES/django.mo index 0587f3fd..256256b5 100644 Binary files a/admin_interface/locale/pl/LC_MESSAGES/django.mo and b/admin_interface/locale/pl/LC_MESSAGES/django.mo differ diff --git a/admin_interface/locale/pl/LC_MESSAGES/django.po b/admin_interface/locale/pl/LC_MESSAGES/django.po index 127fd7b1..9bbbe059 100644 --- a/admin_interface/locale/pl/LC_MESSAGES/django.po +++ b/admin_interface/locale/pl/LC_MESSAGES/django.po @@ -9,7 +9,7 @@ msgid "" msgstr "" "Project-Id-Version: django-admin-interface\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2023-05-10 18:20-0500\n" +"POT-Creation-Date: 2024-03-19 18:27-0500\n" "PO-Revision-Date: 2022-02-04 19:13-0500\n" "Last-Translator: Kamil Paduszyński \n" "Language-Team: Polish \n" @@ -115,6 +115,10 @@ msgstr "kolor" msgid "visible" msgstr "widoczny" +#: admin_interface/models.py +msgid "static logo" +msgstr "statyczne logo" + #: admin_interface/models.py msgid "Leave blank to use the default Django logo" msgstr "Puste ustawia domyślne logo Django" diff --git a/admin_interface/locale/pt_BR/LC_MESSAGES/django.mo b/admin_interface/locale/pt_BR/LC_MESSAGES/django.mo index c81bb244..80a5cf16 100644 Binary files a/admin_interface/locale/pt_BR/LC_MESSAGES/django.mo and b/admin_interface/locale/pt_BR/LC_MESSAGES/django.mo differ diff --git a/admin_interface/locale/pt_BR/LC_MESSAGES/django.po b/admin_interface/locale/pt_BR/LC_MESSAGES/django.po index 78169b0b..c02fd359 100644 --- a/admin_interface/locale/pt_BR/LC_MESSAGES/django.po +++ b/admin_interface/locale/pt_BR/LC_MESSAGES/django.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2023-05-10 18:20-0500\n" +"POT-Creation-Date: 2024-03-19 18:27-0500\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -114,6 +114,10 @@ msgstr "cor" msgid "visible" msgstr "visível" +#: admin_interface/models.py +msgid "static logo" +msgstr "logo estático" + #: admin_interface/models.py msgid "Leave blank to use the default Django logo" msgstr "Deixe em branco para usar o logotipo padrão do Django" diff --git a/admin_interface/locale/ru/LC_MESSAGES/django.mo b/admin_interface/locale/ru/LC_MESSAGES/django.mo index 1f77b536..f7b1b58a 100644 Binary files a/admin_interface/locale/ru/LC_MESSAGES/django.mo and b/admin_interface/locale/ru/LC_MESSAGES/django.mo differ diff --git a/admin_interface/locale/ru/LC_MESSAGES/django.po b/admin_interface/locale/ru/LC_MESSAGES/django.po index 03a31a31..6a766fa3 100644 --- a/admin_interface/locale/ru/LC_MESSAGES/django.po +++ b/admin_interface/locale/ru/LC_MESSAGES/django.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2023-06-23 16:28-0500\n" +"POT-Creation-Date: 2024-03-19 18:27-0500\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -116,6 +116,10 @@ msgstr "цвет" msgid "visible" msgstr "видимый" +#: admin_interface/models.py +msgid "static logo" +msgstr "статический логотип" + #: admin_interface/models.py msgid "Leave blank to use the default Django logo" msgstr "Оставьте пустым, чтобы по умолчанию использовать логотип Django" diff --git a/admin_interface/locale/tr/LC_MESSAGES/django.mo b/admin_interface/locale/tr/LC_MESSAGES/django.mo index 9a1606f0..8ae7a127 100644 Binary files a/admin_interface/locale/tr/LC_MESSAGES/django.mo and b/admin_interface/locale/tr/LC_MESSAGES/django.mo differ diff --git a/admin_interface/locale/tr/LC_MESSAGES/django.po b/admin_interface/locale/tr/LC_MESSAGES/django.po index 8ae3bba9..f6da6e6e 100644 --- a/admin_interface/locale/tr/LC_MESSAGES/django.po +++ b/admin_interface/locale/tr/LC_MESSAGES/django.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: django-admin-interface\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2023-05-10 18:20-0500\n" +"POT-Creation-Date: 2024-03-19 18:27-0500\n" "PO-Revision-Date: 2020-01-29 04:53+0300\n" "Last-Translator: Ishak Okutan \n" "Language-Team: \n" @@ -115,6 +115,10 @@ msgstr "renk" msgid "visible" msgstr "göster" +#: admin_interface/models.py +msgid "static logo" +msgstr "sabit logo" + #: admin_interface/models.py msgid "Leave blank to use the default Django logo" msgstr "Varsayılan Django logosunu kullanmak için boş bırakın" diff --git a/admin_interface/migrations/0031_theme_static_logo_path.py b/admin_interface/migrations/0031_theme_static_logo_path.py new file mode 100644 index 00000000..a2e60960 --- /dev/null +++ b/admin_interface/migrations/0031_theme_static_logo_path.py @@ -0,0 +1,24 @@ +# Generated by Django 5.0.3 on 2024-03-19 22:29 + +import admin_interface.models +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("admin_interface", "0030_theme_collapsible_stacked_inlines_and_more"), + ] + + operations = [ + migrations.AddField( + model_name="theme", + name="static_logo_path", + field=models.FilePathField( + blank=True, + match="^.*\\.(jpg|jpeg|png|svg)$", + max_length=255, + path=admin_interface.models.static_logo_directory_path, + verbose_name="static logo", + ), + ), + ] diff --git a/admin_interface/models.py b/admin_interface/models.py index a29f5238..5fdd134d 100644 --- a/admin_interface/models.py +++ b/admin_interface/models.py @@ -5,10 +5,16 @@ from django.dispatch import receiver from django.utils.encoding import force_str from django.utils.translation import gettext_lazy as _ +from django.conf import settings +import os from .cache import del_cached_active_theme +def static_logo_directory_path(): + return settings.LOCAL_FILE_DIR + + class ThemeQuerySet(models.QuerySet): def get_active(self): objs_active_qs = self.filter(active=True) @@ -62,6 +68,14 @@ class Theme(models.Model): verbose_name=_("visible"), ) + static_logo_path = models.FilePathField( + path=static_logo_directory_path, + blank=True, + verbose_name=_("static logo"), + match=r"^.*\.(jpg|jpeg|png|svg)$", + max_length=255, + ) + logo = models.FileField( upload_to="admin-interface/logo/", blank=True, @@ -401,10 +415,26 @@ class Theme(models.Model): objects = ThemeQuerySet.as_manager() + @property + def static_logo_relative_url(self): + return os.path.relpath(self.static_logo_path, settings.STATIC_ROOT) + def set_active(self): self.active = True self.save() + def save(self, *args, **kwargs): + if self.static_logo_path: + # clear cache if static logo path has changed + try: + obj = Theme.objects.get(pk=self.pk) + if obj.static_logo_path != self.static_logo_path: + del_cached_active_theme() + except Theme.DoesNotExist: + pass + + super().save(*args, **kwargs) + class Meta: app_label = "admin_interface" verbose_name = _("Theme") diff --git a/admin_interface/settings.py b/admin_interface/settings.py index f63f139d..95eeaa5e 100644 --- a/admin_interface/settings.py +++ b/admin_interface/settings.py @@ -25,5 +25,18 @@ def check_installed_app(app, max_dj_version=None): ) +def check_settings(setting_attribute): + """ + Check if the setting_attribute is set in the settings module. + """ + + if not hasattr(settings, setting_attribute): + raise ImproperlyConfigured( + "You must set the {} setting in your settings module.".format( + setting_attribute + ) + ) + + def check_installed_apps(): check_installed_app("colorfield") diff --git a/admin_interface/templates/admin/base_site.html b/admin_interface/templates/admin/base_site.html index 9df49110..2fe57272 100644 --- a/admin_interface/templates/admin/base_site.html +++ b/admin_interface/templates/admin/base_site.html @@ -169,6 +169,8 @@

{% if theme.logo_visible %} {% if theme.logo %} + {% elif theme.static_logo_path %} + {% else %} {% endif %} diff --git a/tests/settings.py b/tests/settings.py index 812a8214..d4938e9e 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -120,3 +120,4 @@ STATIC_ROOT = os.path.join(BASE_DIR, "admin_interface/public/static/") STATIC_URL = "/static/" +LOCAL_FILE_DIR = os.path.join(BASE_DIR, "admin_interface/public/") diff --git a/tests/test_models.py b/tests/test_models.py index 5754e967..42f5ad87 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -4,7 +4,7 @@ from django.conf import settings from django.test import TestCase -from admin_interface.models import Theme +from admin_interface.models import Theme, static_logo_directory_path class AdminInterfaceModelsTestCase(TestCase): @@ -90,6 +90,40 @@ def test_str(self): theme = Theme.objects.get_active() self.assertEqual(str(theme), "Django") + def test_save_clear_cache(self): + theme = Theme.objects.get_active() + theme.static_logo_path = "test" + theme.save() + + self.assertEqual(str(theme), "Django") + + fake_dir = settings.LOCAL_FILE_DIR + "/fake" + theme.static_logo_path = fake_dir + theme.save() + + assert theme.static_logo_path == fake_dir + + def test_save_clear_cache_no_matched_theme(self): + theme = Theme( + name="Custom 1", + active=True, + static_logo_path=settings.LOCAL_FILE_DIR + "/fake", + ) + theme.pk = 999 + theme.save() + + def test_static_path(self): + # the `static_logo_directory_path` function is used as the callable + # in the `path` argument of the `static_logo_path` field + assert static_logo_directory_path() == settings.LOCAL_FILE_DIR + + def test_property_static_logo(self): + theme = Theme.objects.get_active() + theme.static_logo_path = settings.LOCAL_FILE_DIR + "/fake" + theme.save() + + assert theme.static_logo_relative_url == "../fake" + # class AdminInterfaceModelsMultiDBTestCase(TestCase): # databases = ["default", "replica"] diff --git a/tests/test_settings.py b/tests/test_settings.py index 4a7e64eb..df4790ee 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -2,7 +2,8 @@ from django.core.exceptions import ImproperlyConfigured from django.test import TestCase, override_settings -from admin_interface.settings import check_installed_apps +from admin_interface.settings import check_installed_apps, check_settings +import os class AdminInterfaceSettingsTestCase(TestCase): @@ -28,6 +29,12 @@ def __test_installed_apps(self): else: check_installed_apps() + def __test_check_settings(self): + if not hasattr(settings, "LOCAL_FILE_DIR"): + self.assertRaises(ImproperlyConfigured, check_settings, "LOCAL_FILE_DIR") + else: + check_settings("LOCAL_FILE_DIR") + @override_settings( INSTALLED_APPS=[ "admin_interface", @@ -47,3 +54,7 @@ def test_installed_apps_all(self): ) def test_installed_apps_no_colorfield(self): self.__test_installed_apps() + + @override_settings(LOCAL_FILE_DIR=os.path.join(settings.BASE_DIR, "local_file_dir")) + def test_check_settings_local_file_dir(self): + self.__test_check_settings()