33from django .core .cache import cache
44from hope_flex_fields .models import DataChecker
55
6- from country_workspace .models import Office , Program , SyncLog
6+ from country_workspace .models import BeneficiaryGroup , Office , Program , SyncLog
77
88from .. import constants
99from ..client import HopeClient
@@ -30,10 +30,11 @@ def sync_offices(stdout: TextIOBase | None = None) -> dict[str, int]:
3030 )
3131 totals ["add" if created else "upd" ] += 1
3232 SyncLog .objects .register_sync (Office )
33- return totals
33+ return totals
3434
3535
3636def sync_programs (limit_to_office : "Office | None" = None , stdout : TextIOBase | None = None ) -> dict [str , int ]:
37+ sync_beneficiary_groups (stdout = stdout )
3738 if stdout :
3839 stdout .write ("Fetching Programs data from HOPE..." )
3940 client = HopeClient ()
@@ -50,6 +51,8 @@ def sync_programs(limit_to_office: "Office | None" = None, stdout: TextIOBase |
5051 office = Office .objects .get (code = record ["business_area_code" ])
5152 if record ["status" ] not in [Program .ACTIVE , Program .DRAFT ]:
5253 continue
54+ # TODO: beneficiary group
55+ beneficiary_group = BeneficiaryGroup .objects .get (pk = 3 )
5356 p , created = Program .objects .get_or_create (
5457 hope_id = record ["id" ],
5558 defaults = {
@@ -58,6 +61,7 @@ def sync_programs(limit_to_office: "Office | None" = None, stdout: TextIOBase |
5861 "status" : record ["status" ],
5962 "sector" : record ["sector" ],
6063 "country_office" : office ,
64+ "beneficiary_group" : beneficiary_group ,
6165 },
6266 )
6367 if created :
@@ -73,6 +77,29 @@ def sync_programs(limit_to_office: "Office | None" = None, stdout: TextIOBase |
7377 return totals
7478
7579
80+ def sync_beneficiary_groups (stdout : TextIOBase | None = None ) -> bool :
81+ totals = {"add" : 0 , "upd" : 0 }
82+ client = HopeClient ()
83+ if stdout :
84+ stdout .write ("Fetching Beneficiary Groups data from HOPE..." )
85+ with cache .lock ("sync-beneficiary-groups" ):
86+ for record in client .get ("beneficiary-groups" ):
87+ __ , created = BeneficiaryGroup .objects .get_or_create (
88+ hope_id = record ["id" ],
89+ defaults = {
90+ "name" : record ["name" ],
91+ "group_label" : record ["group_label" ],
92+ "group_label_plural" : record ["group_label_plural" ],
93+ "member_label" : record ["member_label" ],
94+ "member_label_plural" : record ["member_label_plural" ],
95+ "master_detail" : record ["master_detail" ],
96+ },
97+ )
98+ totals ["add" if created else "upd" ] += 1
99+ SyncLog .objects .register_sync (BeneficiaryGroup )
100+ return totals
101+
102+
76103def sync_all (stdout : TextIOBase | None = None ) -> bool :
77104 sync_offices (stdout = stdout )
78105 sync_programs (stdout = stdout )
0 commit comments