File: checks.py

package info (click to toggle)
python-django-constance 4.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 796 kB
  • sloc: python: 2,080; makefile: 25; javascript: 23; sh: 6
file content (64 lines) | stat: -rw-r--r-- 2,520 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from __future__ import annotations

from django.core import checks
from django.core.checks import CheckMessage
from django.utils.translation import gettext_lazy as _


def check_fieldsets(*args, **kwargs) -> list[CheckMessage]:
    """
    A Django system check to make sure that, if defined,
    CONFIG_FIELDSETS is consistent with settings.CONFIG.
    """
    from . import settings

    errors = []

    if hasattr(settings, 'CONFIG_FIELDSETS') and settings.CONFIG_FIELDSETS:
        missing_keys, extra_keys = get_inconsistent_fieldnames()
        if missing_keys:
            check = checks.Warning(
                _('CONSTANCE_CONFIG_FIELDSETS is missing field(s) that exists in CONSTANCE_CONFIG.'),
                hint=', '.join(sorted(missing_keys)),
                obj='settings.CONSTANCE_CONFIG',
                id='constance.E001',
            )
            errors.append(check)
        if extra_keys:
            check = checks.Warning(
                _('CONSTANCE_CONFIG_FIELDSETS contains extra field(s) that does not exist in CONFIG.'),
                hint=', '.join(sorted(extra_keys)),
                obj='settings.CONSTANCE_CONFIG',
                id='constance.E002',
            )
            errors.append(check)
    return errors


def get_inconsistent_fieldnames() -> tuple[set, set]:
    """
    Returns a pair of values:
    1) set of keys from settings.CONFIG that are not accounted for in settings.CONFIG_FIELDSETS
    2) set of keys from settings.CONFIG_FIELDSETS that are not present in settings.CONFIG
    If there are no fieldnames in settings.CONFIG_FIELDSETS, returns an empty set.
    """
    from . import settings

    if isinstance(settings.CONFIG_FIELDSETS, dict):
        fieldset_items = settings.CONFIG_FIELDSETS.items()
    else:
        fieldset_items = settings.CONFIG_FIELDSETS

    unique_field_names = set()
    for _fieldset_title, fields_list in fieldset_items:
        # fields_list can be a dictionary, when a fieldset is defined as collapsible
        # https://django-constance.readthedocs.io/en/latest/#fieldsets-collapsing
        if isinstance(fields_list, dict) and 'fields' in fields_list:
            fields_list = fields_list['fields']
        unique_field_names.update(fields_list)
    if not unique_field_names:
        return unique_field_names, unique_field_names
    config_keys = set(settings.CONFIG.keys())
    missing_keys = config_keys - unique_field_names
    extra_keys = unique_field_names - config_keys
    return missing_keys, extra_keys