Skip to content
Snippets Groups Projects
Commit fc61d801 authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

Ensure that favicons are always created and updated

parent 93560627
No related branches found
No related tags found
1 merge request!601Resolve "favicon / icon generation mauls database"
......@@ -14,7 +14,7 @@ from .registries import (
site_preferences_registry,
)
from .util.apps import AppConfig
from .util.core_helpers import has_person
from .util.core_helpers import get_or_create_favicon, has_person
from .util.sass_helpers import clean_scss
......@@ -41,6 +41,8 @@ class CoreConfig(AppConfig):
def ready(self):
super().ready()
from django.conf import settings # noqa
# Autodiscover various modules defined by AlekSIS
autodiscover_modules("form_extensions", "model_extensions", "checks")
......@@ -66,6 +68,10 @@ class CoreConfig(AppConfig):
plugin_dir.register(MediaBackupAgeHealthCheck)
plugin_dir.register(BackupJobHealthCheck)
# Ensure that default Favicon object exists
for name, default in settings.DEFAULT_FAVICON_PATHS.items():
get_or_create_favicon(name, default, is_favicon=name == "favicon")
@classmethod
def _load_data_checks(cls):
"""Get all data checks from all loaded models."""
......@@ -85,6 +91,8 @@ class CoreConfig(AppConfig):
new_value: Optional[Any] = None,
**kwargs,
) -> None:
from django.conf import settings # noqa
if section == "theme":
if name in ("primary", "secondary"):
clean_scss()
......@@ -95,11 +103,14 @@ class CoreConfig(AppConfig):
if new_value:
Favicon.on_site.update_or_create(
title=name,
defaults={"isFavicon": name == "favicon", "faviconImage": new_value,},
title=name, defaults={"isFavicon": is_favicon, "faviconImage": new_value},
)
else:
Favicon.on_site.filter(title=name, isFavicon=is_favicon).delete()
if name in settings.DEFAULT_FAVICON_PATHS:
get_or_create_favicon(
name, settings.DEFAULT_FAVICON_PATHS[name], is_favicon=is_favicon
)
def post_migrate(
self,
......
......@@ -588,7 +588,10 @@ if _settings.get("dev.uwsgi.celery", DEBUG):
UWSGI["attach-daemon"].append(f"celery -A aleksis.core worker --concurrency={concurrency}")
UWSGI["attach-daemon"].append("celery -A aleksis.core beat")
_default_icon_path = os.path.join(STATIC_ROOT, "img/aleksis-icon.png")
DEFAULT_FAVICON_PATHS = {
"pwa_icon": os.path.join(STATIC_ROOT, "img/aleksis-icon.png"),
"favicon": os.path.join(STATIC_ROOT, "img/aleksis-icon.png"),
}
PWA_APP_NAME = lazy_preference("general", "title")
PWA_APP_DESCRIPTION = lazy_preference("general", "description")
PWA_APP_THEME_COLOR = lazy_preference("theme", "primary")
......@@ -596,14 +599,14 @@ PWA_APP_BACKGROUND_COLOR = "#ffffff"
PWA_APP_DISPLAY = "standalone"
PWA_APP_ORIENTATION = "any"
PWA_APP_ICONS = lazy_get_favicons(
"pwa_icon", default=_default_icon_path, config={"android": [192, 512]}
"pwa_icon", default=DEFAULT_FAVICON_PATHS["pwa_icon"], config={"android": [192, 512]}
)
PWA_APP_ICONS_APPLE = lazy_get_favicons(
"pwa_icon", default=_default_icon_path, config={"apple": [76, 114, 152, 180]}
"pwa_icon", default=DEFAULT_FAVICON_PATHS["pwa_icon"], config={"apple": [76, 114, 152, 180]}
)
PWA_APP_SPLASH_SCREEN = lazy_get_favicons(
"pwa_icon",
default=_default_icon_path,
default=DEFAULT_FAVICON_PATHS["pwa_icon"],
config={"apple": [192]},
add_attrs={
"media": "(device-width: 320px) and (device-height: 568px) and"
......
......@@ -126,6 +126,19 @@ def lazy_preference(section: str, name: str) -> Callable[[str, str], Any]:
return lazy(_get_preference, str)(section, name)
def get_or_create_favicon(title: str, default: str, is_favicon: bool = False) -> "Favicon":
"""Ensure that there is always a favicon object."""
from favicon.models import Favicon # noqa
favicon, created = Favicon.on_site.update_or_create(
title=title, defaults={"isFavicon": is_favicon}
)
if created:
favicon.faviconImage.save(os.path.basename(default), File(open(default, "rb")))
favicon.save()
return favicon
def lazy_get_favicons(
title: str,
config: dict[str, list[int]],
......@@ -137,12 +150,7 @@ def lazy_get_favicons(
add_attrs = {}
def _get_favicons() -> list[dict[str, str]]:
from favicon.models import Favicon # noqa
favicon, created = Favicon.on_site.get_or_create(title=title)
if created:
favicon.faviconImage.save(os.path.basename(default), File(open(default, "rb")))
favicon.save()
favicon = get_or_create_favicon(title, default)
favicon_imgs = favicon.get_favicons(config_override=config)
return [
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment