|
| 1 | +"""Application settings.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +from dataclasses import dataclass |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +from django.conf import settings as django_settings |
| 8 | + |
| 9 | +# All attributes accessed with this prefix are possible |
| 10 | +# to overwrite through django.conf.settings. |
| 11 | +SETTINGS_PREFIX = "DJANGO_CELERY_RESULTS_" |
| 12 | + |
| 13 | + |
| 14 | +@dataclass(frozen=True) |
| 15 | +class AppSettings: |
| 16 | + """Proxy class to encapsulate all the app settings. |
| 17 | +
|
| 18 | + This instance should be accessed via the singleton |
| 19 | + ``django_celery_results.conf.app_settings``. |
| 20 | +
|
| 21 | + You shouldn't have to set any of these yourself, the class checks a Django |
| 22 | + settings with the same name and use these if defined, defaulting to the |
| 23 | + values documented here. |
| 24 | + """ |
| 25 | + |
| 26 | + DJANGO_CELERY_RESULTS_DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' |
| 27 | + |
| 28 | + def __getattribute__(self, __name: str) -> Any: |
| 29 | + """Check if a Django project settings should override the app default. |
| 30 | +
|
| 31 | + In order to avoid returning any random properties of the Django |
| 32 | + settings, we first inspect the prefix. |
| 33 | + """ |
| 34 | + if ( |
| 35 | + __name.startswith(SETTINGS_PREFIX) |
| 36 | + and hasattr(django_settings, __name) |
| 37 | + ): |
| 38 | + return getattr(django_settings, __name) |
| 39 | + |
| 40 | + return super().__getattribute__(__name) |
| 41 | + |
| 42 | + |
| 43 | +app_settings = AppSettings() |
0 commit comments