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,7 @@ 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+ beneficiary_group = BeneficiaryGroup .objects .get (hope_id = record ["beneficiary_group" ])
5355 p , created = Program .objects .get_or_create (
5456 hope_id = record ["id" ],
5557 defaults = {
@@ -58,6 +60,7 @@ def sync_programs(limit_to_office: "Office | None" = None, stdout: TextIOBase |
5860 "status" : record ["status" ],
5961 "sector" : record ["sector" ],
6062 "country_office" : office ,
63+ "beneficiary_group" : beneficiary_group ,
6164 },
6265 )
6366 if created :
@@ -73,6 +76,29 @@ def sync_programs(limit_to_office: "Office | None" = None, stdout: TextIOBase |
7376 return totals
7477
7578
79+ def sync_beneficiary_groups (stdout : TextIOBase | None = None ) -> bool :
80+ totals = {"add" : 0 , "upd" : 0 }
81+ client = HopeClient ()
82+ if stdout :
83+ stdout .write ("Fetching Beneficiary Groups data from HOPE..." )
84+ with cache .lock ("sync-beneficiary-groups" ):
85+ for record in client .get ("beneficiary-groups" ):
86+ __ , created = BeneficiaryGroup .objects .get_or_create (
87+ hope_id = record ["id" ],
88+ defaults = {
89+ "name" : record ["name" ],
90+ "group_label" : record ["group_label" ],
91+ "group_label_plural" : record ["group_label_plural" ],
92+ "member_label" : record ["member_label" ],
93+ "member_label_plural" : record ["member_label_plural" ],
94+ "master_detail" : record ["master_detail" ],
95+ },
96+ )
97+ totals ["add" if created else "upd" ] += 1
98+ SyncLog .objects .register_sync (BeneficiaryGroup )
99+ return totals
100+
101+
76102def sync_all (stdout : TextIOBase | None = None ) -> bool :
77103 sync_offices (stdout = stdout )
78104 sync_programs (stdout = stdout )
0 commit comments