Skip to content
Snippets Groups Projects
Verified Commit a09481d0 authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

Make constance configs usable in other settings

Introduces a utility function lazy_config() that can lazily bind
a constance config key. As an example, it is used to use the primary colour
configured through constance as the app's theme colour.

Advances #139
parent e503ac41
No related branches found
No related tags found
1 merge request!118Make constance configs usable in other settings
Pipeline #634 failed
......@@ -7,7 +7,7 @@ from django.utils.translation import gettext_lazy as _
from dynaconf import LazySettings
from easy_thumbnails.conf import Settings as thumbnail_settings
from .util.core_helpers import get_app_packages, merge_app_settings
from .util.core_helpers import get_app_packages, lazy_config, merge_app_settings
ENVVAR_PREFIX_FOR_DYNACONF = "ALEKSIS"
DIRS_FOR_DYNACONF = ["/etc/aleksis"]
......@@ -368,7 +368,7 @@ _settings.populate_obj(sys.modules[__name__])
PWA_APP_NAME = "AlekSIS" # dbsettings
PWA_APP_DESCRIPTION = "AlekSIS – The free school information system" # dbsettings
PWA_APP_THEME_COLOR = _settings.get("pwa.color", "#da1f3d") # dbsettings
PWA_APP_THEME_COLOR = lazy_config("COLOUR_PRIMARY")
PWA_APP_BACKGROUND_COLOR = "#ffffff"
PWA_APP_DISPLAY = "standalone"
PWA_APP_SCOPE = "/"
......
import pkgutil
from importlib import import_module
from typing import Sequence, Union
from typing import Any, Callable, Sequence, Union
from django.conf import settings
from django.db.models import Model
from django.http import HttpRequest
from django.utils.functional import lazy
def dt_show_toolbar(request: HttpRequest) -> bool:
......@@ -67,6 +68,20 @@ def merge_app_settings(setting: str, original: Union[dict, list], deduplicate: b
raise TypeError("Only dict and list settings can be merged.")
def lazy_config(key: str) -> Callable[[str], Any]:
""" Lazily get a config value from constance. Useful to bind constance
configs to other global settings to make them available to third-party
apps that are not aware of constance.
"""
def _get_config(key: str) -> Any:
from constance import config # noqa
return getattr(config, key)
# The type is guessed from the default value to improve lazy()'s behaviour
return lazy(_get_config, type(settings.CONSTANCE_CONFIG[key][0]))(key)
def is_impersonate(request: HttpRequest) -> bool:
if hasattr(request, "user"):
return getattr(request.user, "is_impersonate", False)
......
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