Skip to content

Conversation

hcvdwerf
Copy link
Contributor

@hcvdwerf hcvdwerf commented Sep 23, 2025

…onses

Summary by Sourcery

Implement comprehensive multilingual support by extending scheming schemas with translated fields, updating translation utilities to honor the Accept-Language header, integrating template helpers, and adding related tests

New Features:

  • Extend dataset and resource scheming schemas with GDI-specific fields and multilingual support
  • Honor the Accept-Language header in enhanced package search and show actions to return translated package and facet properties

Enhancements:

  • Refactor translation utilities to apply translated properties with language fallback
  • Add template helpers and register them via the plugin’s ITemplateHelpers interface

Tests:

  • Introduce unit tests for translation_utils verifying language preference and fallback behavior

Copy link
Contributor

sourcery-ai bot commented Sep 23, 2025

Reviewer's Guide

This PR adds full multilingual support to enhanced package responses by honoring the Accept-Language header, extends the scheming schema with GDI-specific and translated fields, and updates core translation utilities, action handlers, plugin hooks, and corresponding tests.

File-Level Changes

Change Details Files
Overhauled scheming schema for multilingual GDI-specific metadata
  • Added form_languages and new translated fields (“*_translated”) for dataset and resource metadata
  • Introduced dozens of new GDI-specific fields (e.g., health_category, provenance_activity, access_services)
  • Updated labels, presets, validators, help_text, and snippets across schema
ckanext/gdi_userportal/scheming/schemas/gdi_userportal.yaml
Enhanced translation utilities with language selection and fallback
  • Added _apply_translated_properties and _select_translated_value to recursively pick preferred or fallback translations
  • Defined TRANSLATED_SUFFIX and LANGUAGE_VALUE_FIELDS constants
  • Extended replace_package to accept lang and apply translated properties before field replacements
ckanext/gdi_userportal/logic/action/translation_utils.py
Propagated Accept-Language header through action handlers
  • Read Accept-Language in enhanced_package_search and enhanced_package_show
  • Pass lang parameter to get_translations, replace_package, and replace_search_facets
ckanext/gdi_userportal/logic/action/post.py
ckanext/gdi_userportal/logic/action/get.py
Added template helpers and plugin integration for scheming validation
  • Implemented ITemplateHelpers in plugin and added get_helpers
  • Introduced helpers.py with scheming_missing_required_fields and data extraction helpers
  • Covered translation utilities with new unit tests
ckanext/gdi_userportal/plugin.py
ckanext/gdi_userportal/helpers.py
ckanext/gdi_userportal/tests/test_translation_utils.py

Possibly linked issues

  • Setup CKAN extension for GDI - User Portal #1: The PR adds a language field to distributions and implements logic to handle translated content for package responses, directly addressing the issue's goal of adding language to distributions.
  • Setup CKAN extension for GDI - User Portal #1: The PR updates the GDI User Portal schema and adds logic to load and display language labels based on the user's Accept-Language header, directly fulfilling the issue's requirement.
  • #237: The PR implements the refactoring of Value Label mapping and handles localization of these labels for enhanced package search and show endpoints, directly addressing the issue's requirements.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes - here's some feedback:

  • Instead of passing the raw Accept-Language header through, use a proper parser to extract and prioritize supported language codes (e.g. handling q-values and language subtags) before calling replace_package.
  • The massive scheming YAML could be refactored using YAML anchors, includes, or split into smaller modular files to avoid repetition and improve maintainability of the field definitions.
  • Consider adding unit tests for replace_search_facets (including translations and fallback logic) to ensure facet titles are correctly localized and fallback as expected.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Instead of passing the raw Accept-Language header through, use a proper parser to extract and prioritize supported language codes (e.g. handling q-values and language subtags) before calling replace_package.
- The massive scheming YAML could be refactored using YAML anchors, includes, or split into smaller modular files to avoid repetition and improve maintainability of the field definitions.
- Consider adding unit tests for replace_search_facets (including translations and fallback logic) to ensure facet titles are correctly localized and fallback as expected.

## Individual Comments

### Comment 1
<location> `ckanext/gdi_userportal/tests/test_translation_utils.py:68-84` </location>
<code_context>
+    }
+
+
+def test_replace_package_prefers_requested_language():
+    package = deepcopy(_base_package())
+
+    result = replace_package(package, translation_dict={}, lang="nl")
+
+    assert result["title"] == "Nederlandse titel"
+    assert result["notes"] == "Nederlandse toelichting"
+    assert result["provenance"] == "Nederlandse herkomst"
+    assert result["population_coverage"] == "Nederlandse dekking"
+    assert result["publisher_note"] == "Nederlandse uitgeversnotitie"
+
+    resource = result["resources"][0]
+    assert resource["name"] == "Nederlandse resource"
+    assert resource["rights"] == "Nederlandse rechten"
+
+    attribution_agent = result["qualified_attribution"][0]["agent"][0]
+    assert attribution_agent["name"] == "Nederlandse agent"
+
</code_context>

<issue_to_address>
**suggestion (testing):** Missing test for edge cases where the requested language is present but the value is empty or None.

Add a test where the requested language key exists but its value is empty or None to verify correct fallback behavior.

```suggestion
def test_replace_package_prefers_requested_language():
    package = deepcopy(_base_package())

    result = replace_package(package, translation_dict={}, lang="nl")

    assert result["title"] == "Nederlandse titel"
    assert result["notes"] == "Nederlandse toelichting"
    assert result["provenance"] == "Nederlandse herkomst"
    assert result["population_coverage"] == "Nederlandse dekking"
    assert result["publisher_note"] == "Nederlandse uitgeversnotitie"

    resource = result["resources"][0]
    assert resource["name"] == "Nederlandse resource"
    assert resource["rights"] == "Nederlandse rechten"

    attribution_agent = result["qualified_attribution"][0]["agent"][0]
    assert attribution_agent["name"] == "Nederlandse agent"


def test_replace_package_requested_language_empty_or_none():
    package = deepcopy(_base_package())

    # Set the requested language values to empty string and None
    package["title"] = {"en": "English title", "nl": ""}
    package["notes"] = {"en": "English notes", "nl": None}
    package["provenance"] = {"en": "English provenance", "nl": ""}
    package["population_coverage"] = {"en": "English coverage", "nl": None}
    package["publisher_note"] = {"en": "English publisher note", "nl": ""}

    package["resources"][0]["name"] = {"en": "English resource", "nl": ""}
    package["resources"][0]["rights"] = {"en": "English rights", "nl": None}

    package["qualified_attribution"][0]["agent"][0]["name"] = {"en": "English agent", "nl": ""}

    result = replace_package(package, translation_dict={}, lang="nl")

    # Should fallback to English when nl is empty or None
    assert result["title"] == "English title"
    assert result["notes"] == "English notes"
    assert result["provenance"] == "English provenance"
    assert result["population_coverage"] == "English coverage"
    assert result["publisher_note"] == "English publisher note"

    resource = result["resources"][0]
    assert resource["name"] == "English resource"
    assert resource["rights"] == "English rights"

    attribution_agent = result["qualified_attribution"][0]["agent"][0]
    assert attribution_agent["name"] == "English agent"
```
</issue_to_address>

### Comment 2
<location> `ckanext/gdi_userportal/tests/test_translation_utils.py:87-83` </location>
<code_context>
+    assert attribution_agent["name"] == "Nederlandse agent"
+
+
+def test_replace_package_falls_back_to_default_language():
+    package = deepcopy(_base_package())
+
+    result = replace_package(package, translation_dict={}, lang="fr")
+
+    assert result["title"] == "English Title"
+    assert result["notes"] == "English Notes"
+    assert result["provenance"] == "English provenance"
+    assert result["population_coverage"] == "English coverage"
+    assert result["publisher_note"] == "English publisher note"
+
+    resource = result["resources"][0]
+    assert resource["name"] == "English resource"
+    assert resource["rights"] == "English rights"
+
+    attribution_agent = result["qualified_attribution"][0]["agent"][0]
+    assert attribution_agent["name"] == "English agent"
</code_context>

<issue_to_address>
**suggestion (testing):** Missing test for scenario where neither requested nor default language is present in the translation dict.

Please add a test where both the requested and default languages are absent, but another language exists, to confirm correct fallback behavior.
</issue_to_address>

### Comment 3
<location> `ckanext/gdi_userportal/helpers.py:64-70` </location>
<code_context>
def _is_missing_value(value: Any) -> bool:
    if value is None:
        return True

    if isinstance(value, str):
        return value.strip() == ""

    if isinstance(value, dict):
        if not value:
            return True
        return all(_is_missing_value(v) for v in value.values())

    if isinstance(value, (list, tuple, set)):
        if not value:
            return True
        return all(_is_missing_value(v) for v in value)

    return False

</code_context>

<issue_to_address>
**issue (code-quality):** We've found these issues:

- Lift code into else after jump in control flow [×2] ([`reintroduce-else`](https://docs.sourcery.ai/Reference/Default-Rules/refactorings/reintroduce-else/))
- Replace if statement with if expression [×2] ([`assign-if-exp`](https://docs.sourcery.ai/Reference/Default-Rules/refactorings/assign-if-exp/))
</issue_to_address>

### Comment 4
<location> `ckanext/gdi_userportal/helpers.py:109-110` </location>
<code_context>
def scheming_missing_required_fields(
    pages: list[dict[str, Any]],
    data: dict[str, Any] | None = None,
    package_id: str | None = None,
) -> list[list[str]]:
    """Return a list of missing required fields grouped per form page.

    This helper acts as the base implementation expected by
    ``ckanext-fluent``.  It mirrors the behaviour from the forked
    ckanext-scheming version previously used in this project and makes sure
    chained helpers can extend the result again.
    """
    data_dict = _ensure_data_dict(data, package_id)

    missing_per_page: list[list[str]] = []

    for page in pages or []:
        page_missing: list[str] = []
        for field in page.get("fields", []):
            # Ignore non-required fields early.
            if not tk.h.scheming_field_required(field):
                continue

            value = _extract_field_value(field, data_dict)

            # Repeating subfields can contain a list of child values; treat the
            # field as present when at least one entry contains data.
            if field.get("repeating_subfields") and isinstance(value, list):
                if any(not _is_missing_value(item) for item in value):
                    continue
            elif not _is_missing_value(value):
                continue

            field_name = field.get("field_name")
            if field_name:
                page_missing.append(field_name)

        missing_per_page.append(page_missing)

    return missing_per_page

</code_context>

<issue_to_address>
**suggestion (code-quality):** Use named expression to simplify assignment and conditional ([`use-named-expression`](https://docs.sourcery.ai/Reference/Default-Rules/refactorings/use-named-expression/))

```suggestion
            if field_name := field.get("field_name"):
```
</issue_to_address>

### Comment 5
<location> `ckanext/gdi_userportal/logic/action/translation_utils.py:184` </location>
<code_context>
def _apply_translated_properties(data: Any, preferred_lang: str, fallback_lang: str = DEFAULT_FALLBACK_LANG):
    if isinstance(data, dict):
        for key, value in list(data.items()):
            if isinstance(value, dict):
                _apply_translated_properties(value, preferred_lang, fallback_lang)
            elif isinstance(value, list):
                data[key] = [
                    _apply_translated_properties(item, preferred_lang, fallback_lang)
                    if isinstance(item, (dict, list))
                    else item
                    for item in value
                ]

        for key, value in list(data.items()):
            if key.endswith(TRANSLATED_SUFFIX) and isinstance(value, dict):
                base_key = key[:-len(TRANSLATED_SUFFIX)]
                data[base_key] = _select_translated_value(value, preferred_lang, fallback_lang)
            elif key in LANGUAGE_VALUE_FIELDS and isinstance(value, dict):
                data[key] = _select_translated_value(value, preferred_lang, fallback_lang)
        return data

    if isinstance(data, list):
        return [
            _apply_translated_properties(item, preferred_lang, fallback_lang)
            if isinstance(item, (dict, list))
            else item
            for item in data
        ]

    return data

</code_context>

<issue_to_address>
**issue (code-quality):** We've found these issues:

- Add guard clause ([`last-if-guard`](https://docs.sourcery.ai/Reference/Default-Rules/refactorings/last-if-guard/))
- Lift code into else after jump in control flow ([`reintroduce-else`](https://docs.sourcery.ai/Reference/Default-Rules/refactorings/reintroduce-else/))
- Replace if statement with if expression ([`assign-if-exp`](https://docs.sourcery.ai/Reference/Default-Rules/refactorings/assign-if-exp/))
</issue_to_address>

### Comment 6
<location> `ckanext/gdi_userportal/logic/action/translation_utils.py:237-239` </location>
<code_context>
def _has_content(value: Any) -> bool:
    if value is None:
        return False
    if isinstance(value, str):
        return bool(value.strip())
    if isinstance(value, (list, dict)):
        return bool(value)
    return True

</code_context>

<issue_to_address>
**suggestion (code-quality):** We've found these issues:

- Lift code into else after jump in control flow ([`reintroduce-else`](https://docs.sourcery.ai/Reference/Default-Rules/refactorings/reintroduce-else/))
- Replace if statement with if expression ([`assign-if-exp`](https://docs.sourcery.ai/Reference/Default-Rules/refactorings/assign-if-exp/))

```suggestion
    return bool(value) if isinstance(value, (list, dict)) else True
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

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