Skip to content

Commit 744630f

Browse files
committed
consolidate system checks into one to predict order
1 parent f43af2b commit 744630f

File tree

1 file changed

+39
-18
lines changed

1 file changed

+39
-18
lines changed

edc_consent/system_checks.py

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
from django.core.checks import CheckMessage, Error, Warning
22

3+
from . import ConsentDefinitionError
34
from .consent_definition import ConsentDefinition
45
from .site_consents import site_consents
56

67

7-
def check_consents_cdef_registered(app_configs, **kwargs) -> list[CheckMessage]:
8+
def check_consents(app_configs, **kwargs) -> list[CheckMessage]:
9+
errors = []
10+
errors.extend(check_consents_cdef_registered())
11+
errors.extend(check_consents_models())
12+
if not errors:
13+
errors.extend(check_consents_versions())
14+
errors.extend(check_consents_durations())
15+
return errors
16+
17+
18+
def check_consents_cdef_registered() -> list[CheckMessage]:
819
errors = []
920
if not site_consents.registry:
1021
errors.append(
@@ -13,39 +24,49 @@ def check_consents_cdef_registered(app_configs, **kwargs) -> list[CheckMessage]:
1324
return errors
1425

1526

16-
def check_consents_proxy_models(app_configs, **kwargs) -> list[CheckMessage]:
17-
"""Expect proxy models only in ConsentDefinitions"""
27+
def check_consents_models() -> list[CheckMessage]:
28+
"""Expect proxy models only in ConsentDefinitions.
29+
30+
- ConsentDefinition may only be associated with a proxy model.
31+
- check proxy models use custom 'objects' and 'on_site' model
32+
managers.
33+
"""
1834
errors = []
1935
for cdef in site_consents.registry.values():
20-
if not cdef.model_cls._meta.proxy:
21-
errors.append(
22-
Error(
23-
(
24-
f"Consent definition model is not a proxy model. Got {cdef.model}."
25-
f"See {cdef.name}"
26-
),
27-
id="edc_consent.E002",
36+
try:
37+
cdef.model_cls
38+
except ConsentDefinitionError as e:
39+
errors.append(Error(str(e), id="edc_consent.E002"))
40+
else:
41+
if not cdef.model_cls._meta.proxy:
42+
errors.append(
43+
Error(
44+
(
45+
f"Consent definition model is not a proxy model. Got {cdef.model}."
46+
f"See {cdef.name}"
47+
),
48+
id="edc_consent.E003",
49+
)
2850
)
29-
)
3051
return errors
3152

3253

33-
def check_consents_versions(app_configs, **kwargs) -> list[CheckMessage]:
54+
def check_consents_versions() -> list[CheckMessage]:
3455
"""Expect versions to be unique across `proxy_for` model"""
3556
errors = []
3657
used = []
3758
for cdef in [cdef for cdef in site_consents.registry.values()]:
3859
if cdef in used:
3960
continue
40-
err, used = inspect_others_using_same_proxy_for_model_with_duplicate_versions(
61+
err, used = _inspect_others_using_same_proxy_for_model_with_duplicate_versions(
4162
cdef, used
4263
)
4364
if err:
4465
errors.append(err)
4566
return errors
4667

4768

48-
def check_consents_durations(app_configs, **kwargs) -> list[CheckMessage]:
69+
def check_consents_durations() -> list[CheckMessage]:
4970
"""Durations may not overlap across `proxy_for` model
5071
5172
This check needs models to be ready otherwise we would add it
@@ -58,13 +79,13 @@ def check_consents_durations(app_configs, **kwargs) -> list[CheckMessage]:
5879
for cdef2 in cdefs:
5980
if cdef1 == cdef2:
6081
continue
61-
err, found = inspect_possible_overlap_in_validity_period(cdef1, cdef2, found)
82+
err, found = _inspect_possible_overlap_in_validity_period(cdef1, cdef2, found)
6283
if err:
6384
errors.append(err)
6485
return errors
6586

6687

67-
def inspect_possible_overlap_in_validity_period(
88+
def _inspect_possible_overlap_in_validity_period(
6889
cdef1, cdef2, found
6990
) -> tuple[Warning | None, list]:
7091
"""Durations between cdef1 and cdef2 may not overlap
@@ -92,7 +113,7 @@ def inspect_possible_overlap_in_validity_period(
92113
return err, found
93114

94115

95-
def inspect_others_using_same_proxy_for_model_with_duplicate_versions(
116+
def _inspect_others_using_same_proxy_for_model_with_duplicate_versions(
96117
cdef1: ConsentDefinition, used: list[ConsentDefinition]
97118
) -> tuple[Warning | None, list[ConsentDefinition]]:
98119
err = None

0 commit comments

Comments
 (0)