Skip to content

Fix resource operations corrupting package-level multilang translations#49

Merged
etj merged 4 commits into
masterfrom
copilot/fix-resource-updates-translation-corruption
May 4, 2026
Merged

Fix resource operations corrupting package-level multilang translations#49
etj merged 4 commits into
masterfrom
copilot/fix-resource-updates-translation-corruption

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 4, 2026

When a resource is created, updated, or deleted, CKAN internally calls package_update, triggering after_dataset_update with a pkg_dict sourced from the core package table — which holds the last-saved language's text, not the current session language's translation. The multilang extension blindly overwrites the current-language package_multilang entries with this stale data.

Changes

ckanext/multilang/plugin.py

  • Added before_resource_create/update/delete hooks that set a '__multilang_skip_pkg_update' flag on the shared CKAN context dict before the internal package_update is called
  • after_dataset_update now skips the package multilang update when this flag is present
  • after_resource_create/update and new after_resource_delete clear the flag after the resource operation completes
def before_resource_update(self, context, current, resource):
    context[self._RESOURCE_OP_FLAG] = True

def after_dataset_update(self, context, obj_dict):
    if context.get(self._RESOURCE_OP_FLAG):
        log.debug('Skipping package multilang update: triggered by a resource operation')
        return
    # ... normal package multilang update

def after_resource_update(self, context, obj_dict):
    context.pop(self._RESOURCE_OP_FLAG, None)
    # ... normal resource multilang update

The flag is passed cleanly through the context (the same dict CKAN threads through before_resource_*package_updateafter_dataset_updateafter_resource_*), requiring no thread-local storage.

ckanext/multilang/tests/test_resource_update_fix.py

  • New test module with 13 unit tests covering flag set/clear behaviour for each hook and full lifecycle simulations for resource create, update, and delete flows

Copilot AI assigned Copilot and etj May 4, 2026
Copilot AI linked an issue May 4, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Fix resource updates causing package-level translation corruption Fix resource operations corrupting package-level multilang translations May 4, 2026
Copilot AI requested a review from etj May 4, 2026 11:16
Copilot finished work on behalf of etj May 4, 2026 11:16
@etj etj marked this pull request as ready for review May 4, 2026 16:13
Copilot AI review requested due to automatic review settings May 4, 2026 16:13
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a multilang regression in the CKAN extension where resource CRUD operations could overwrite package-level translations with stale package-table values during CKAN’s internal package_update flow.

Changes:

  • Added a resource-operation context flag so after_dataset_update skips package multilang writes during resource create/update/delete.
  • Cleared that flag in the corresponding resource after-hooks.
  • Added regression-oriented tests for flag lifecycle behavior and simulated resource hook flows.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
ckanext/multilang/plugin.py Adds the context-flag guard around package multilang updates during resource operations.
ckanext/multilang/tests/test_resource_update_fix.py Adds unit tests covering flag set/clear behavior and simulated create/update/delete sequences.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +99 to +103
with patch('ckanext.multilang.plugin.helpers.getLanguage', return_value='de'), \
patch('ckanext.multilang.plugin.after_update_dataset',
side_effect=fake_after_dataset_update), \
patch('ckanext.multilang.plugin.after_update_resource'):

Comment on lines +128 to +131
with patch('ckanext.multilang.plugin.helpers.getLanguage', return_value='de'), \
patch('ckanext.multilang.plugin.after_update_dataset',
side_effect=lambda *a: pkg_update_calls.append(a)), \
patch('ckanext.multilang.plugin.after_create_resource'):
Comment thread ckanext/multilang/tests/test_resource_update_fix.py Outdated
Comment on lines +168 to +173
"""after_resource_update must clear the flag regardless of language."""
plugin = self._make_plugin()
context = {plugin._RESOURCE_OP_FLAG: True}
with patch('ckanext.multilang.plugin.helpers.getLanguage', return_value=None):
plugin.after_resource_update(context, {})
self.assertNotIn(plugin._RESOURCE_OP_FLAG, context)
Comment thread ckanext/multilang/tests/test_resource_update_fix.py Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@etj etj merged commit 276f9e1 into master May 4, 2026
4 checks passed
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.

Resource updates cause package-level translation corruption

3 participants