Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/django_filters/conf.py: 56%
34 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1from django.conf import settings as dj_settings
2from django.core.signals import setting_changed
3from django.utils.translation import gettext_lazy as _
5from .utils import deprecate
7DEFAULTS = {
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}
54DEPRECATED_SETTINGS = []
57def is_callable(value):
58 # check for callables, except types
59 return callable(value) and not isinstance(value, type)
62class Settings:
63 def __getattr__(self, name):
64 if name not in DEFAULTS: 64 ↛ 65line 64 didn't jump to line 65, because the condition on line 64 was never true
65 msg = "'%s' object has no attribute '%s'"
66 raise AttributeError(msg % (self.__class__.__name__, name))
68 value = self.get_setting(name)
70 if is_callable(value): 70 ↛ 71line 70 didn't jump to line 71, because the condition on line 70 was never true
71 value = value()
73 # Cache the result
74 setattr(self, name, value)
75 return value
77 def get_setting(self, setting):
78 django_setting = "FILTERS_%s" % setting
80 if setting in DEPRECATED_SETTINGS and hasattr(dj_settings, django_setting): 80 ↛ 81line 80 didn't jump to line 81, because the condition on line 80 was never true
81 deprecate("The '%s' setting has been deprecated." % django_setting)
83 return getattr(dj_settings, django_setting, DEFAULTS[setting])
85 def change_setting(self, setting, value, enter, **kwargs):
86 if not setting.startswith("FILTERS_"):
87 return
88 setting = setting[8:] # strip 'FILTERS_'
90 # ensure a valid app setting is being overridden
91 if setting not in DEFAULTS:
92 return
94 # if exiting, delete value to repopulate
95 if enter:
96 setattr(self, setting, value)
97 else:
98 delattr(self, setting)
101settings = Settings()
102setting_changed.connect(settings.change_setting)