2023-06-24 00:10:23 +00:00
|
|
|
""" Django notifications settings file """
|
2018-05-30 05:18:33 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2023-07-11 23:28:23 +00:00
|
|
|
from typing import Any, TypedDict
|
|
|
|
|
|
2018-05-30 05:18:33 +00:00
|
|
|
from django.conf import settings
|
|
|
|
|
|
2023-07-11 23:28:23 +00:00
|
|
|
# Import from `django.core.signals` instead of the official location
|
|
|
|
|
# `django.test.signals` to avoid importing the test module unnecessarily.
|
|
|
|
|
from django.core.signals import setting_changed
|
|
|
|
|
|
|
|
|
|
NOTIFICATION_DEFAULTS = {
|
2023-06-24 00:10:23 +00:00
|
|
|
"PAGINATE_BY": 20,
|
|
|
|
|
"USE_JSONFIELD": False,
|
|
|
|
|
"SOFT_DELETE": False,
|
|
|
|
|
"NUM_TO_FETCH": 10,
|
|
|
|
|
"CACHE_TIMEOUT": 2,
|
2018-05-30 05:18:33 +00:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 03:05:08 +00:00
|
|
|
|
2023-07-11 23:28:23 +00:00
|
|
|
class NotificationDefaultsType(TypedDict):
|
|
|
|
|
PAGINATE_BY: int
|
|
|
|
|
USE_JSONFIELD: bool
|
|
|
|
|
SOFT_DELETE: bool
|
|
|
|
|
NUM_TO_FETCH: int
|
|
|
|
|
CACHE_TIMEOUT: int
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NotificationSettings:
|
|
|
|
|
"""
|
|
|
|
|
A settings object that allows Notification settings to be accessed as
|
|
|
|
|
properties. For example:
|
|
|
|
|
|
|
|
|
|
from notifications.settings import notification_settings
|
|
|
|
|
print(notification_settings.SOFT_DELETE)
|
|
|
|
|
|
|
|
|
|
Note:
|
|
|
|
|
This is an internal class that is only compatible with settings namespaced
|
2023-07-18 00:38:27 +00:00
|
|
|
under the DJANGO_NOTIFICATIONS_CONFIG name. It is not intended to be used by 3rd-party
|
2023-07-11 23:28:23 +00:00
|
|
|
apps, and test helpers like `override_settings` may not work as expected.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, defaults: None | NotificationDefaultsType = None):
|
|
|
|
|
self._user_settings = None
|
|
|
|
|
self.defaults = defaults or NotificationDefaultsType(**NOTIFICATION_DEFAULTS)
|
|
|
|
|
self._cached_attrs: set[str] = set()
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def user_settings(self) -> NotificationDefaultsType:
|
|
|
|
|
if not self._user_settings:
|
2023-07-18 00:38:27 +00:00
|
|
|
self._user_settings = NotificationDefaultsType(**getattr(settings, "DJANGO_NOTIFICATIONS_CONFIG") or {})
|
2023-07-11 23:28:23 +00:00
|
|
|
return self._user_settings
|
|
|
|
|
|
|
|
|
|
def __getattr__(self, attr: str) -> NotificationDefaultsType:
|
|
|
|
|
if attr not in self.defaults:
|
|
|
|
|
raise AttributeError(f'Invalid Notification setting: "{attr}"')
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
# Check if present in user settings
|
|
|
|
|
val = self.user_settings[attr]
|
|
|
|
|
except KeyError:
|
|
|
|
|
# Fall back to defaults
|
|
|
|
|
val = self.defaults[attr]
|
|
|
|
|
|
|
|
|
|
# Cache the result
|
|
|
|
|
self._cached_attrs.add(attr)
|
|
|
|
|
setattr(self, attr, val)
|
|
|
|
|
return val
|
|
|
|
|
|
|
|
|
|
def reload(self):
|
|
|
|
|
for attr in self._cached_attrs:
|
|
|
|
|
delattr(self, attr)
|
|
|
|
|
self._cached_attrs.clear()
|
|
|
|
|
self._user_settings = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
notification_settings = NotificationSettings(NOTIFICATION_DEFAULTS)
|
|
|
|
|
|
|
|
|
|
|
2023-11-02 22:49:45 +00:00
|
|
|
def reload_notification_settings(*args: Any, **kwargs: Any):
|
2023-07-11 23:28:23 +00:00
|
|
|
setting = kwargs["setting"]
|
2023-07-18 00:38:27 +00:00
|
|
|
if setting == "DJANGO_NOTIFICATIONS_CONFIG":
|
2023-07-11 23:28:23 +00:00
|
|
|
notification_settings.reload()
|
|
|
|
|
|
2018-05-30 05:18:33 +00:00
|
|
|
|
2023-07-11 23:28:23 +00:00
|
|
|
setting_changed.connect(reload_notification_settings)
|
2018-05-30 05:18:33 +00:00
|
|
|
|
2023-07-11 23:28:23 +00:00
|
|
|
__all__ = ("notification_settings",)
|