forked from openimis/openimis-be-core_py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmax_length_constraints.py
More file actions
93 lines (74 loc) · 2.67 KB
/
Copy pathmax_length_constraints.py
File metadata and controls
93 lines (74 loc) · 2.67 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# core/gql/max_length_constraints.py
from graphene.types.generic import GenericScalar
import graphene
from django.apps import apps
try:
from insuree.models import Insuree
except ImportError:
Insuree = None
def get_model(model_name):
"""Récupère un modèle à partir de son nom."""
try:
return apps.get_model('core', model_name)
except LookupError:
return None
MAX_LENGTH_CONSTRAINTS_FIELDS = {
"admin": {
"user": {
"username": ("User", "username"),
"lastName": ("InteractiveUser", "last_name"),
"otherNames": ("InteractiveUser", "other_names"),
"phone": ("InteractiveUser", "phone"),
"email": ("InteractiveUser", "email"),
},
},
}
if Insuree:
MAX_LENGTH_CONSTRAINTS_FIELDS["insuree"] = {
"insuree": {
"uuid": (Insuree, "uuid"),
"chfId": (Insuree, "chf_id"),
"lastName": (Insuree, "last_name"),
"otherNames": (Insuree, "other_names"),
"marital": (Insuree, "marital"),
"passport": (Insuree, "passport"),
"phone": (Insuree, "phone"),
"email": (Insuree, "email"),
"currentAddress": (Insuree, "current_address"),
"geolocation": (Insuree, "geolocation"),
"status": (Insuree, "status"),
},
}
def build_max_length_constraints():
constraints = {}
for module_name, forms in MAX_LENGTH_CONSTRAINTS_FIELDS.items():
module_constraints = {}
for form_name, fields in forms.items():
form_constraints = {}
for field_name, (model_or_name, model_field_name) in fields.items():
if isinstance(model_or_name, str):
model = get_model(model_or_name)
if model is None:
continue
else:
model = model_or_name
try:
field = model._meta.get_field(model_field_name)
if field.max_length:
form_constraints[field_name] = field.max_length
except Exception:
continue
if form_constraints:
module_constraints[form_name] = form_constraints
if module_constraints:
constraints[module_name] = module_constraints
return constraints
class MaxLengthConstraintsGQLType(graphene.ObjectType):
"""
Returns max_length constraints used by the frontend to enforce field length
limits in supported forms.
"""
constraints = GenericScalar()
@staticmethod
def resolve_constraints(root, info):
return build_max_length_constraints()