Skip to content

fix(spp_area_hdx): derive ISO3 code from hdx_dataset_id#14

Open
emjay0921 wants to merge 3 commits into19.0from
fix/spp-area-hdx-country-iso3
Open

fix(spp_area_hdx): derive ISO3 code from hdx_dataset_id#14
emjay0921 wants to merge 3 commits into19.0from
fix/spp-area-hdx-country-iso3

Conversation

@emjay0921
Copy link
Contributor

Why is this change needed?

country_iso3 computed field used country_id.code which returns 2-letter ISO codes (PH, LK) instead of 3-letter ISO3 codes (PHL, LKA). This caused action_sync_from_hdx to construct incorrect dataset IDs like cod-ab-ph instead of cod-ab-phl, making the HDX sync fail for any country without a pre-configured hdx_dataset_id.

How was the change implemented?

  • _compute_country_iso3 now derives ISO3 from hdx_dataset_id when available (e.g., cod-ab-phlPHL), falling back to country_id.code for new records
  • Added hdx_dataset_id to the @api.depends decorator so the field recomputes when the dataset ID is set
  • action_sync_from_hdx now uses get_dataset() directly when hdx_dataset_id is already set, avoiding the ISO3 code path entirely

New unit tests

Unit tests executed by the author

How to test manually

  1. Navigate to HDX Integration > COD Sources
  2. Verify all 10 pre-configured countries show 3-letter ISO3 codes (PHL, LKA, AFG, etc.)
  3. Click "Sync from HDX" on any country — should fetch resources successfully
  4. Create a new COD source without hdx_dataset_id — ISO3 falls back to 2-letter code until dataset ID is set

Related links

@gemini-code-assist
Copy link

Summary of Changes

Hello @emjay0921, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical issue where the HDX synchronization process failed for countries without a pre-configured hdx_dataset_id due to the generation of incorrect 2-letter ISO codes instead of the required 3-letter codes. The changes ensure that the correct 3-letter ISO codes are consistently used for HDX dataset identification, either by deriving them from the hdx_dataset_id itself or by directly using the existing dataset ID for synchronization, thereby improving the reliability and robustness of the HDX integration.

Highlights

  • Corrected ISO3 Code Derivation: The _compute_country_iso3 method now prioritizes extracting the 3-letter ISO code directly from the hdx_dataset_id (e.g., cod-ab-phl -> PHL) when available and following the expected pattern. It falls back to the 2-letter country_id.code only if hdx_dataset_id is not set or does not match the 'cod-ab-' prefix.
  • Enhanced Dependency Tracking: The @api.depends decorator for _compute_country_iso3 has been updated to include hdx_dataset_id, ensuring that the ISO3 code is recomputed whenever the HDX dataset ID changes, maintaining data consistency.
  • Improved HDX Synchronization Logic: The action_sync_from_hdx method now directly uses client.get_dataset(self.hdx_dataset_id) if an hdx_dataset_id is already configured. This bypasses the need to search by the computed country_iso3, preventing sync failures caused by incorrect ISO3 codes and streamlining the synchronization process.
Changelog
  • spp_area_hdx/models/hdx_cod_source.py
    • Modified the @api.depends decorator for _compute_country_iso3 to include hdx_dataset_id.
    • Updated _compute_country_iso3 to first attempt to derive the 3-letter ISO code from hdx_dataset_id if it starts with "cod-ab-", otherwise falling back to country_id.code.
    • Modified action_sync_from_hdx to call client.get_dataset() directly using hdx_dataset_id if it's set, otherwise it proceeds with client.search_cod_datasets() using country_iso3.
Activity
  • No specific activity (comments, reviews, progress updates) has been recorded for this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request effectively fixes a bug where incorrect 2-letter ISO codes were used to construct HDX dataset IDs, leading to synchronization failures. The solution is well-implemented: it correctly derives the 3-letter ISO code from hdx_dataset_id when available and smartly updates the sync logic to use the dataset ID directly, bypassing the problematic code path. The changes are clear and directly address the root cause. I have one minor suggestion to improve code maintainability.

for record in self:
if record.country_id:
if record.hdx_dataset_id and record.hdx_dataset_id.startswith("cod-ab-"):
record.country_iso3 = record.hdx_dataset_id[7:].upper()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using the magic number 7 for slicing is brittle. It's better to derive the length from the prefix string itself. This makes the code more maintainable, as changing the prefix string in the startswith check would automatically update the slice length.

Suggested change
record.country_iso3 = record.hdx_dataset_id[7:].upper()
record.country_iso3 = record.hdx_dataset_id[len("cod-ab-"):].upper()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant