| 1 |
from django.conf import settings as dj_settings |
| 2 |
from django.core.signals import setting_changed |
| 3 |
from django.utils.translation import gettext_lazy as _ |
| 4 |
|
| 5 |
from .utils import deprecate |
| 6 |
|
| 7 |
DEFAULTS = { |
| 8 |
"DISABLE_HELP_TEXT": False, |
| 9 |
"DEFAULT_LOOKUP_EXPR": "exact", |
| 10 |
# empty/null choices |
| 11 |
"EMPTY_CHOICE_LABEL": "---------", |
| 12 |
"NULL_CHOICE_LABEL": None, |
| 13 |
"NULL_CHOICE_VALUE": "null", |
| 14 |
"VERBOSE_LOOKUPS": { |
| 15 |
# transforms don't need to be verbose, since their expressions are chained |
| 16 |
"date": _("date"), |
| 17 |
"year": _("year"), |
| 18 |
"month": _("month"), |
| 19 |
"day": _("day"), |
| 20 |
"week_day": _("week day"), |
| 21 |
"hour": _("hour"), |
| 22 |
"minute": _("minute"), |
| 23 |
"second": _("second"), |
| 24 |
# standard lookups |
| 25 |
"exact": "", |
| 26 |
"iexact": "", |
| 27 |
"contains": _("contains"), |
| 28 |
"icontains": _("contains"), |
| 29 |
"in": _("is in"), |
| 30 |
"gt": _("is greater than"), |
| 31 |
"gte": _("is greater than or equal to"), |
| 32 |
"lt": _("is less than"), |
| 33 |
"lte": _("is less than or equal to"), |
| 34 |
"startswith": _("starts with"), |
| 35 |
"istartswith": _("starts with"), |
| 36 |
"endswith": _("ends with"), |
| 37 |
"iendswith": _("ends with"), |
| 38 |
"range": _("is in range"), |
| 39 |
"isnull": _("is null"), |
| 40 |
"regex": _("matches regex"), |
| 41 |
"iregex": _("matches regex"), |
| 42 |
"search": _("search"), |
| 43 |
# postgres lookups |
| 44 |
"contained_by": _("is contained by"), |
| 45 |
"overlap": _("overlaps"), |
| 46 |
"has_key": _("has key"), |
| 47 |
"has_keys": _("has keys"), |
| 48 |
"has_any_keys": _("has any keys"), |
| 49 |
"trigram_similar": _("search"), |
| 50 |
}, |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
DEPRECATED_SETTINGS = [] |
| 55 |
|
| 56 |
|
| 57 |
def is_callable(value): |
| 58 |
# check for callables, except types |
| 59 |
return callable(value) and not isinstance(value, type) |
| 60 |
|
| 61 |
|
| 62 |
class Settings: |
| 63 |
def __getattr__(self, name): |
| 64 |
if name not in DEFAULTS: |
| 65 |
msg = "'%s' object has no attribute '%s'" |
| 66 |
raise AttributeError(msg % (self.__class__.__name__, name)) |
| 67 |
|
| 68 |
value = self.get_setting(name) |
| 69 |
|
| 70 |
if is_callable(value): |
| 71 |
value = value() |
| 72 |
|
| 73 |
# Cache the result |
| 74 |
setattr(self, name, value) |
| 75 |
return value |
| 76 |
|
| 77 |
def get_setting(self, setting): |
| 78 |
django_setting = "FILTERS_%s" % setting |
| 79 |
|
| 80 |
if setting in DEPRECATED_SETTINGS and hasattr(dj_settings, django_setting): |
| 81 |
deprecate("The '%s' setting has been deprecated." % django_setting) |
| 82 |
|
| 83 |
return getattr(dj_settings, django_setting, DEFAULTS[setting]) |
| 84 |
|
| 85 |
def change_setting(self, setting, value, enter, **kwargs): |
| 86 |
if not setting.startswith("FILTERS_"): |
| 87 |
return |
| 88 |
setting = setting[8:] # strip 'FILTERS_' |
| 89 |
|
| 90 |
# ensure a valid app setting is being overridden |
| 91 |
if setting not in DEFAULTS: |
| 92 |
return |
| 93 |
|
| 94 |
# if exiting, delete value to repopulate |
| 95 |
if enter: |
| 96 |
setattr(self, setting, value) |
| 97 |
else: |
| 98 |
delattr(self, setting) |
| 99 |
|
| 100 |
|
| 101 |
settings = Settings() |
| 102 |
setting_changed.connect(settings.change_setting) |