@@ -40,11 +40,18 @@ class HdxCodSource(models.Model):
4040
4141 _country_unique = models .Constraint ("UNIQUE(country_id)" , "Only one COD source per country is allowed" )
4242
43- @api .depends ("country_id" )
43+ @api .depends ("country_id" , "hdx_dataset_id" )
4444 def _compute_country_iso3 (self ):
45- """Compute ISO3 code from country."""
45+ """Compute ISO3 code from HDX dataset ID or country.
46+
47+ HDX dataset IDs follow the pattern ``cod-ab-{iso3}`` (e.g., ``cod-ab-phl``),
48+ which contains the correct 3-letter ISO code. Falls back to the 2-letter
49+ ``res.country.code`` when the dataset ID is not set.
50+ """
4651 for record in self :
47- if record .country_id :
52+ if record .hdx_dataset_id and record .hdx_dataset_id .startswith ("cod-ab-" ):
53+ record .country_iso3 = record .hdx_dataset_id [7 :].upper ()
54+ elif record .country_id :
4855 record .country_iso3 = record .country_id .code
4956 else :
5057 record .country_iso3 = False
@@ -63,7 +70,12 @@ def action_sync_from_hdx(self):
6370
6471 try :
6572 client = HdxClient ()
66- dataset = client .search_cod_datasets (self .country_iso3 )
73+
74+ # Use existing dataset ID if available, otherwise search by ISO3
75+ if self .hdx_dataset_id :
76+ dataset = client .get_dataset (self .hdx_dataset_id )
77+ else :
78+ dataset = client .search_cod_datasets (self .country_iso3 )
6779
6880 if not dataset :
6981 raise UserError (_ ("No COD dataset found for country %s" ) % self .country_id .name )
0 commit comments