1+ # core/gql/max_length_constraints.py
2+
3+ from graphene .types .generic import GenericScalar
4+ import graphene
5+ from django .apps import apps
6+
7+ try :
8+ from insuree .models import Insuree
9+ except ImportError :
10+ Insuree = None
11+
12+
13+ def get_model (model_name ):
14+ """Récupère un modèle à partir de son nom."""
15+ try :
16+ return apps .get_model ('core' , model_name )
17+ except LookupError :
18+ return None
19+
20+
21+ MAX_LENGTH_CONSTRAINTS_FIELDS = {
22+ "admin" : {
23+ "user" : {
24+ "username" : ("User" , "username" ),
25+ "lastName" : ("InteractiveUser" , "last_name" ),
26+ "otherNames" : ("InteractiveUser" , "other_names" ),
27+ "phone" : ("InteractiveUser" , "phone" ),
28+ "email" : ("InteractiveUser" , "email" ),
29+ },
30+ },
31+ }
32+
33+ if Insuree :
34+ MAX_LENGTH_CONSTRAINTS_FIELDS ["insuree" ] = {
35+ "insuree" : {
36+ "uuid" : (Insuree , "uuid" ),
37+ "chfId" : (Insuree , "chf_id" ),
38+ "lastName" : (Insuree , "last_name" ),
39+ "otherNames" : (Insuree , "other_names" ),
40+ "marital" : (Insuree , "marital" ),
41+ "passport" : (Insuree , "passport" ),
42+ "phone" : (Insuree , "phone" ),
43+ "email" : (Insuree , "email" ),
44+ "currentAddress" : (Insuree , "current_address" ),
45+ "geolocation" : (Insuree , "geolocation" ),
46+ "status" : (Insuree , "status" ),
47+ },
48+ }
49+
50+
51+ def build_max_length_constraints ():
52+ constraints = {}
53+
54+ for module_name , forms in MAX_LENGTH_CONSTRAINTS_FIELDS .items ():
55+ module_constraints = {}
56+
57+ for form_name , fields in forms .items ():
58+ form_constraints = {}
59+
60+ for field_name , (model_or_name , model_field_name ) in fields .items ():
61+ if isinstance (model_or_name , str ):
62+ model = get_model (model_or_name )
63+ if model is None :
64+ continue
65+ else :
66+ model = model_or_name
67+
68+ try :
69+ field = model ._meta .get_field (model_field_name )
70+ if field .max_length :
71+ form_constraints [field_name ] = field .max_length
72+ except Exception :
73+ continue
74+
75+ if form_constraints :
76+ module_constraints [form_name ] = form_constraints
77+
78+ if module_constraints :
79+ constraints [module_name ] = module_constraints
80+
81+ return constraints
82+
83+
84+ class MaxLengthConstraintsGQLType (graphene .ObjectType ):
85+ """
86+ Returns max_length constraints used by the frontend to enforce field length
87+ limits in supported forms.
88+ """
89+ constraints = GenericScalar ()
90+
91+ @staticmethod
92+ def resolve_constraints (root , info ):
93+ return build_max_length_constraints ()
0 commit comments