Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use static file as logo (if sets) #378

Closed
wants to merge 8 commits into from
1 change: 1 addition & 0 deletions admin_interface/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class ThemeAdmin(admin.ModelAdmin):
"logo_max_height",
"logo_color",
"logo_visible",
"static_logo_path",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would show this after the logo field.

),
},
),
Expand Down
3 changes: 3 additions & 0 deletions admin_interface/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This setting should be optional, it makes no sense to ask users to set a setting that they probably don't need.

Binary file modified admin_interface/locale/de/LC_MESSAGES/django.mo
Binary file not shown.
6 changes: 5 additions & 1 deletion admin_interface/locale/de/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -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"
Expand Down
Binary file modified admin_interface/locale/es/LC_MESSAGES/django.mo
Binary file not shown.
9 changes: 7 additions & 2 deletions admin_interface/locale/es/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>\n"
"Language-Team: Spanish <[email protected]>\n"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
Binary file modified admin_interface/locale/fa/LC_MESSAGES/django.mo
Binary file not shown.
6 changes: 5 additions & 1 deletion admin_interface/locale/fa/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>\n"
"Language-Team: Farsi <[email protected]>\n"
Expand Down Expand Up @@ -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 "برای نمایش لوگوی پیش فرض، خالی بگذارید."
Expand Down
Binary file modified admin_interface/locale/fr/LC_MESSAGES/django.mo
Binary file not shown.
6 changes: 5 additions & 1 deletion admin_interface/locale/fr/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -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"
Expand Down
Binary file modified admin_interface/locale/it/LC_MESSAGES/django.mo
Binary file not shown.
6 changes: 5 additions & 1 deletion admin_interface/locale/it/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>\n"
"Language-Team: Italian <[email protected]>\n"
Expand Down Expand Up @@ -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"
Expand Down
Binary file modified admin_interface/locale/pl/LC_MESSAGES/django.mo
Binary file not shown.
6 changes: 5 additions & 1 deletion admin_interface/locale/pl/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>\n"
"Language-Team: Polish <[email protected]>\n"
Expand Down Expand Up @@ -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"
Expand Down
Binary file modified admin_interface/locale/pt_BR/LC_MESSAGES/django.mo
Binary file not shown.
6 changes: 5 additions & 1 deletion admin_interface/locale/pt_BR/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -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"
Expand Down
Binary file modified admin_interface/locale/ru/LC_MESSAGES/django.mo
Binary file not shown.
6 changes: 5 additions & 1 deletion admin_interface/locale/ru/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -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"
Expand Down
Binary file modified admin_interface/locale/tr/LC_MESSAGES/django.mo
Binary file not shown.
6 changes: 5 additions & 1 deletion admin_interface/locale/tr/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>\n"
"Language-Team: \n"
Expand Down Expand Up @@ -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"
Expand Down
24 changes: 24 additions & 0 deletions admin_interface/migrations/0031_theme_static_logo_path.py
Original file line number Diff line number Diff line change
@@ -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",
),
),
]
30 changes: 30 additions & 0 deletions admin_interface/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please name this method get_logo_static_path?

return settings.LOCAL_FILE_DIR


class ThemeQuerySet(models.QuerySet):
def get_active(self):
objs_active_qs = self.filter(active=True)
Expand Down Expand Up @@ -62,6 +68,14 @@ class Theme(models.Model):
verbose_name=_("visible"),
)

static_logo_path = models.FilePathField(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please name this field logo_static?

path=static_logo_directory_path,
blank=True,
verbose_name=_("static logo"),
match=r"^.*\.(jpg|jpeg|png|svg)$",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also gif and webp formats should be accepted.

max_length=255,
)

logo = models.FileField(
upload_to="admin-interface/logo/",
blank=True,
Expand Down Expand Up @@ -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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This query (and the related logic) can be avoided by checking if the logo_static path value is different from it's initial value: https://stackoverflow.com/a/1793323/2096218

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")
Expand Down
13 changes: 13 additions & 0 deletions admin_interface/settings.py
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The best way to define settings in third-party packages that allow the best flexibility when testing them (overridding them with different values) is this:

In package __init__.py:

from django.core.exceptions import ImproperlyConfigured

try:
    from admin_interface import settings  # noqa: F401
except ImproperlyConfigured:
    pass

In package settings.py:

from django.conf import settings

if not hasattr(settings, "ADMIN_INTERFACE_MY_SETTING"):
    settings.ADMIN_INTERFACE_MY_SETTING = "default value"

Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,18 @@
)


def check_settings(setting_attribute):
"""
Check if the setting_attribute is set in the settings module.
"""

if not hasattr(settings, setting_attribute):
raise ImproperlyConfigured(

Check warning on line 34 in admin_interface/settings.py

View check run for this annotation

Codecov / codecov/patch

admin_interface/settings.py#L34

Added line #L34 was not covered by tests
"You must set the {} setting in your settings module.".format(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests coverage should be improved.

setting_attribute
)
)


def check_installed_apps():
check_installed_app("colorfield")
2 changes: 2 additions & 0 deletions admin_interface/templates/admin/base_site.html
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ <h1 id="site-name">
{% if theme.logo_visible %}
{% if theme.logo %}
<img class="logo" style="display:none;" src="{{ theme.logo.url }}" {% if theme.logo.width %}width="{{ theme.logo.width }}"{% endif %} {% if theme.logo.height %}height="{{ theme.logo.height }}"{% endif %}>
{% elif theme.static_logo_path %}
<img class="logo" style="display:none;" src="{%static theme.static_logo_relative_url %}" width="104" height="36">
{% else %}
<img class="logo default" style="display:none;" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" width="104" height="36">
{% endif %}
Expand Down
1 change: 1 addition & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this name is too generic (doesn’t indicate it’s for django-admin-interface) and at the same time too vague! (local file dir? what is a file dir, what is a non-local file dir)

So: I think this could be named something like ADMIN_INTERFACE_STATIC_LOGO_PATH, with an example value of theme-logos/logo.png. Then let django use the normal static file system to resolve the actual full path to project-dir/some-app/theme-logos/logo.png.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe change the settings name with ADMIN_INTERFACE_STATIC_LOGO_PATH is better

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering that this is a setting for specifying the static logo directory, I would name it ADMIN_INTERFACE_STATIC_LOGO_DIR.

36 changes: 35 additions & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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"]
Expand Down
Loading