1
1
from django .core .checks import CheckMessage , Error , Warning
2
2
3
+ from . import ConsentDefinitionError
3
4
from .consent_definition import ConsentDefinition
4
5
from .site_consents import site_consents
5
6
6
7
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 ]:
8
19
errors = []
9
20
if not site_consents .registry :
10
21
errors .append (
@@ -13,39 +24,49 @@ def check_consents_cdef_registered(app_configs, **kwargs) -> list[CheckMessage]:
13
24
return errors
14
25
15
26
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
+ """
18
34
errors = []
19
35
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
+ )
28
50
)
29
- )
30
51
return errors
31
52
32
53
33
- def check_consents_versions (app_configs , ** kwargs ) -> list [CheckMessage ]:
54
+ def check_consents_versions () -> list [CheckMessage ]:
34
55
"""Expect versions to be unique across `proxy_for` model"""
35
56
errors = []
36
57
used = []
37
58
for cdef in [cdef for cdef in site_consents .registry .values ()]:
38
59
if cdef in used :
39
60
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 (
41
62
cdef , used
42
63
)
43
64
if err :
44
65
errors .append (err )
45
66
return errors
46
67
47
68
48
- def check_consents_durations (app_configs , ** kwargs ) -> list [CheckMessage ]:
69
+ def check_consents_durations () -> list [CheckMessage ]:
49
70
"""Durations may not overlap across `proxy_for` model
50
71
51
72
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]:
58
79
for cdef2 in cdefs :
59
80
if cdef1 == cdef2 :
60
81
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 )
62
83
if err :
63
84
errors .append (err )
64
85
return errors
65
86
66
87
67
- def inspect_possible_overlap_in_validity_period (
88
+ def _inspect_possible_overlap_in_validity_period (
68
89
cdef1 , cdef2 , found
69
90
) -> tuple [Warning | None , list ]:
70
91
"""Durations between cdef1 and cdef2 may not overlap
@@ -92,7 +113,7 @@ def inspect_possible_overlap_in_validity_period(
92
113
return err , found
93
114
94
115
95
- def inspect_others_using_same_proxy_for_model_with_duplicate_versions (
116
+ def _inspect_others_using_same_proxy_for_model_with_duplicate_versions (
96
117
cdef1 : ConsentDefinition , used : list [ConsentDefinition ]
97
118
) -> tuple [Warning | None , list [ConsentDefinition ]]:
98
119
err = None
0 commit comments