1+ from typing import Any
12from django import forms
23from hope_flex_fields .models import DataChecker , FieldDefinition , Fieldset
34
78 PEOPLE_CHECKER_NAME ,
89)
910
11+ type FieldSpec = tuple [str , FieldDefinition , dict [str , Any ] | None ]
1012
11- def create_hope_checkers () -> None :
12- _char = FieldDefinition .objects .get (field_type = forms .CharField )
13- _date = FieldDefinition .objects .get (field_type = forms .DateField )
14- _bool = FieldDefinition .objects .get (field_type = forms .BooleanField )
15- _int = FieldDefinition .objects .get (field_type = forms .IntegerField )
16-
17- _h_country = FieldDefinition .objects .get (name = "CountryChoice" )
18- _h_residence = FieldDefinition .objects .get (slug = "hope-hh-residencestatus" )
19- _i_gender = FieldDefinition .objects .get (slug = "hope-ind-gender" )
20- _i_disability = FieldDefinition .objects .get (slug = "hope-ind-disability" )
21- _i_role = FieldDefinition .objects .get (slug = "hope-ind-role" )
22- _i_relationship = FieldDefinition .objects .get (slug = "hope-ind-relationship" )
23-
24- _p_type = FieldDefinition .objects .get (slug = "hope-people-type" )
2513
26- hh_fs , __ = Fieldset .objects .get_or_create (name = HOUSEHOLD_CHECKER_NAME )
27- hh_fs .fields .get_or_create (name = "address" , definition = _char )
28- hh_fs .fields .get_or_create (name = "admin1" , definition = _char )
29- hh_fs .fields .get_or_create (name = "admin2" , definition = _char )
30- hh_fs .fields .get_or_create (name = "admin3" , definition = _char )
31- hh_fs .fields .get_or_create (name = "admin4" , definition = _char )
32- hh_fs .fields .get_or_create (name = "collect_individual_data" , definition = _bool )
33- hh_fs .fields .get_or_create (name = "consent" , definition = _bool )
34- hh_fs .fields .get_or_create (name = "country" , attrs = {"label" : "Country" , "required" : True }, definition = _h_country )
35- hh_fs .fields .get_or_create (name = "country_origin" , definition = _h_country )
36- hh_fs .fields .get_or_create (name = "household_id" , attrs = {"label" : "Household ID" }, definition = _char )
37- hh_fs .fields .get_or_create (name = "name_enumerator" , attrs = {"label" : "Enumerator" }, definition = _char )
38- hh_fs .fields .get_or_create (name = "org_enumerator" , definition = _char )
39- hh_fs .fields .get_or_create (name = "registration_method" , definition = _char )
40- hh_fs .fields .get_or_create (name = "residence_status" , definition = _h_residence )
41- hh_fs .fields .get_or_create (name = "size" , definition = _int )
14+ def create_hope_checkers () -> None :
15+ try :
16+ defs : dict [str , FieldDefinition ] = {
17+ "char" : FieldDefinition .objects .get (field_type = forms .CharField ),
18+ "date" : FieldDefinition .objects .get (field_type = forms .DateField ),
19+ "bool" : FieldDefinition .objects .get (field_type = forms .BooleanField ),
20+ "int" : FieldDefinition .objects .get (field_type = forms .IntegerField ),
21+ "h_country" : FieldDefinition .objects .get (name = "CountryChoice" ),
22+ "h_residence" : FieldDefinition .objects .get (slug = "hope-hh-residencestatus" ),
23+ "i_gender" : FieldDefinition .objects .get (slug = "hope-ind-gender" ),
24+ "i_disability" : FieldDefinition .objects .get (slug = "hope-ind-disability" ),
25+ "i_role" : FieldDefinition .objects .get (slug = "hope-ind-role" ),
26+ "i_relationship" : FieldDefinition .objects .get (slug = "hope-ind-relationship" ),
27+ "p_type" : FieldDefinition .objects .get (slug = "hope-people-type" ),
28+ }
29+ except FieldDefinition .DoesNotExist as e :
30+ raise LookupError (f"Could not find base FieldDefinitions needed for Hope checkers: { e } " ) from e
4231
43- for segment in [
32+ household_fields_spec : list [FieldSpec ] = [
33+ ("address" , defs ["char" ], None ),
34+ ("admin1" , defs ["char" ], None ),
35+ ("admin2" , defs ["char" ], None ),
36+ ("admin3" , defs ["char" ], None ),
37+ ("admin4" , defs ["char" ], None ),
38+ ("collect_individual_data" , defs ["bool" ], None ),
39+ ("consent" , defs ["bool" ], None ),
40+ ("country" , defs ["h_country" ], {"label" : "Country" , "required" : True }),
41+ ("country_origin" , defs ["h_country" ], None ),
42+ ("household_id" , defs ["char" ], {"label" : "Household ID" }),
43+ ("name_enumerator" , defs ["char" ], {"label" : "Enumerator" }),
44+ ("org_enumerator" , defs ["char" ], None ),
45+ ("registration_method" , defs ["char" ], None ),
46+ ("residence_status" , defs ["h_residence" ], None ),
47+ ("size" , defs ["int" ], None ),
48+ ]
49+ demographic_segments : list [str ] = [
4450 "female_age_group_0_5_count" ,
4551 "female_age_group_6_11_count" ,
4652 "female_age_group_12_17_count" ,
@@ -62,58 +68,63 @@ def create_hope_checkers() -> None:
6268 "male_age_group_12_17_disabled_count" ,
6369 "male_age_group_18_59_disabled_count" ,
6470 "male_age_group_60_disabled_count" ,
65- ]:
66- hh_fs .fields .get_or_create (name = segment , definition = _int , attrs = {"required" : False })
71+ ]
72+ household_fields_spec .extend ([(segment , defs ["int" ], {"required" : False }) for segment in demographic_segments ])
73+
74+ individual_fields_spec : list [FieldSpec ] = [
75+ ("address" , defs ["char" ], None ),
76+ ("alternate_collector_id" , defs ["char" ], {"label" : "Alternative Collector for" }),
77+ ("birth_date" , defs ["date" ], {"label" : "Birth Date" , "required" : True }),
78+ ("disability" , defs ["i_disability" ], {"label" : "Disability" }),
79+ ("estimated_birth_date" , defs ["bool" ], {"label" : "Estimated Birth Date" , "required" : False }),
80+ ("family_name" , defs ["char" ], {"label" : "Family Name" }),
81+ ("full_name" , defs ["char" ], {"label" : "Full Name" , "required" : True }),
82+ ("gender" , defs ["i_gender" ], None ),
83+ ("given_name" , defs ["char" ], {"label" : "Given Name" }),
84+ ("middle_name" , defs ["char" ], {"label" : "Middle Name" }),
85+ ("national_id_issuer" , defs ["char" ], None ),
86+ ("national_id_no" , defs ["char" ], None ),
87+ ("national_id_photo" , defs ["char" ], None ),
88+ ("phone_no" , defs ["char" ], None ),
89+ ("primary_collector_id" , defs ["char" ], {"label" : "Primary Collector for" }),
90+ ("relationship" , defs ["i_relationship" ], {"label" : "Relationship" , "required" : True }),
91+ ("role" , defs ["i_role" ], {"label" : "Role" }),
92+ ]
6793
68- ind_fs , __ = Fieldset .objects .get_or_create (name = INDIVIDUAL_CHECKER_NAME )
69- ind_fs .fields .get_or_create (name = "address" , definition = _char )
70- ind_fs .fields .get_or_create (
71- name = "alternate_collector_id" ,
72- attrs = {"label" : "Alternative Collector for" },
73- definition = _char ,
74- )
75- ind_fs .fields .get_or_create (name = "birth_date" , attrs = {"label" : "Birth Date" , "required" : True }, definition = _date )
76- ind_fs .fields .get_or_create (name = "disability" , attrs = {"label" : "Disability" }, definition = _i_disability )
77- ind_fs .fields .get_or_create (
78- name = "estimated_birth_date" , attrs = {"label" : "Estimated Birth Date" , "required" : False }, definition = _bool
79- )
80- ind_fs .fields .get_or_create (name = "family_name" , attrs = {"label" : "Family Name" }, definition = _char )
81- ind_fs .fields .get_or_create (name = "full_name" , attrs = {"label" : "Full Name" , "required" : True }, definition = _char )
82- ind_fs .fields .get_or_create (name = "gender" , definition = _i_gender )
83- ind_fs .fields .get_or_create (name = "given_name" , attrs = {"label" : "Given Name" }, definition = _char )
84- ind_fs .fields .get_or_create (name = "middle_name" , attrs = {"label" : "Middle Name" }, definition = _char )
85- ind_fs .fields .get_or_create (name = "national_id_issuer" , definition = _char )
86- ind_fs .fields .get_or_create (name = "national_id_no" , definition = _char )
87- ind_fs .fields .get_or_create (name = "national_id_photo" , definition = _char )
88- ind_fs .fields .get_or_create (name = "phone_no" , definition = _char )
89- ind_fs .fields .get_or_create (name = "primary_collector_id" , attrs = {"label" : "Primary Collector for" }, definition = _char )
90- ind_fs .fields .get_or_create (
91- name = "relationship" , attrs = {"label" : "Relationship" , "required" : True }, definition = _i_relationship
92- )
93- ind_fs .fields .get_or_create (name = "role" , attrs = {"label" : "Role" }, definition = _i_role )
94+ people_fields_spec : list [FieldSpec ] = [
95+ ("type" , defs ["p_type" ], {"label" : "People Type" , "required" : True }),
96+ ("full_name" , defs ["char" ], {"label" : "Full Name" , "required" : True }),
97+ ("country" , defs ["h_country" ], {"label" : "Country" , "required" : True }),
98+ ("residence_status" , defs ["h_residence" ], {"label" : "Residence Status" , "required" : True }),
99+ ("gender" , defs ["i_gender" ], None ),
100+ ("birth_date" , defs ["date" ], {"label" : "Birth Date" , "required" : True }),
101+ ]
94102
95- pp_fs , __ = Fieldset .objects .get_or_create (name = PEOPLE_CHECKER_NAME )
96- pp_fs .fields .get_or_create (name = "type" , attrs = {"label" : "People Type" , "required" : True }, definition = _p_type )
97- pp_fs .fields .get_or_create (name = "full_name" , attrs = {"label" : "Full Name" , "required" : True }, definition = _char )
98- pp_fs .fields .get_or_create (name = "country" , attrs = {"label" : "Country" , "required" : True }, definition = _h_country )
99- pp_fs .fields .get_or_create (
100- name = "residence_status" , attrs = {"label" : "Residence Status" , "required" : True }, definition = _h_residence
101- )
102- pp_fs .fields .get_or_create (name = "gender" , definition = _i_gender )
103- pp_fs .fields .get_or_create (name = "birth_date" , attrs = {"label" : "Birth Date" , "required" : True }, definition = _date )
103+ def _add_fields (fieldset : Fieldset , fields_spec : list [FieldSpec ]) -> None :
104+ for name , definition , attrs in fields_spec :
105+ fieldset .fields .get_or_create (name = name , definition = definition , defaults = {"attrs" : attrs or {}})
104106
105- hh_dc , __ = DataChecker .objects .get_or_create (name = HOUSEHOLD_CHECKER_NAME )
106- hh_dc .fieldsets .add (hh_fs )
107- ind_dc , __ = DataChecker .objects .get_or_create (name = INDIVIDUAL_CHECKER_NAME )
108- ind_dc .fieldsets .add (ind_fs )
109- pp_dc , __ = DataChecker .objects .get_or_create (name = PEOPLE_CHECKER_NAME )
110- pp_dc .fieldsets .add (pp_fs )
107+ hh_fs , _ = Fieldset .objects .get_or_create (name = HOUSEHOLD_CHECKER_NAME )
108+ ind_fs , _ = Fieldset .objects .get_or_create (name = INDIVIDUAL_CHECKER_NAME )
109+ pp_fs , _ = Fieldset .objects .get_or_create (name = PEOPLE_CHECKER_NAME )
110+
111+ _add_fields (hh_fs , household_fields_spec )
112+ _add_fields (ind_fs , individual_fields_spec )
113+ _add_fields (pp_fs , people_fields_spec )
114+
115+ hh_dc , _ = DataChecker .objects .get_or_create (name = HOUSEHOLD_CHECKER_NAME )
116+ ind_dc , _ = DataChecker .objects .get_or_create (name = INDIVIDUAL_CHECKER_NAME )
117+ pp_dc , _ = DataChecker .objects .get_or_create (name = PEOPLE_CHECKER_NAME )
118+
119+ hh_dc .fieldsets .set ([hh_fs ])
120+ ind_dc .fieldsets .set ([ind_fs ])
121+ pp_dc .fieldsets .set ([pp_fs ])
111122
112123
113124def removes_hope_checkers () -> None :
114125 DataChecker .objects .filter (name = HOUSEHOLD_CHECKER_NAME ).delete ()
115126 DataChecker .objects .filter (name = INDIVIDUAL_CHECKER_NAME ).delete ()
127+ DataChecker .objects .filter (name = PEOPLE_CHECKER_NAME ).delete ()
116128 Fieldset .objects .filter (name = HOUSEHOLD_CHECKER_NAME ).delete ()
117129 Fieldset .objects .filter (name = INDIVIDUAL_CHECKER_NAME ).delete ()
118130 Fieldset .objects .filter (name = PEOPLE_CHECKER_NAME ).delete ()
119- Fieldset .objects .filter (name = PEOPLE_CHECKER_NAME ).delete ()
0 commit comments