[Fixes #13971] Added optional query parameter to return all translated fields in resource APIs#13985
[Fixes #13971] Added optional query parameter to return all translated fields in resource APIs#13985
Conversation
…ed fields in resource APIs
Summary of ChangesHello @nrjadkry, 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 enhances the GeoNode API by providing more flexibility in how multilingual data is exposed. It introduces a new query parameter that allows API consumers to retrieve all available translations for a resource's fields, rather than just the translation for the currently active language. This change improves data accessibility for applications requiring comprehensive multilingual content. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
This pull request introduces an optional include_i18n query parameter to resource APIs, allowing clients to request all translated fields for a resource instead of just the one for the current language. The changes correctly modify the queryset prefetching in MultiLangViewMixin and the serialization logic in MultiLangOutputMixin to support this new functionality.
My review includes suggestions to improve code quality:
- Refactoring a new utility function to use a more Pythonic list comprehension.
- Addressing code duplication for parsing the
include_i18nparameter. - Improving performance and robustness in the serializer by avoiding repeated function calls within a loop and using a more reliable method for string parsing.
| base_field_name = sparse.name[:-13] # name_multilang_xx | ||
| lang_code = sparse.name[-2:] |
There was a problem hiding this comment.
Using slicing with magic numbers (-13, -2) to parse the field name is fragile and hard to maintain. It assumes a fixed length for _multilang_ and the language code. A more robust approach would be to use rsplit('_multilang_', 1).
| base_field_name = sparse.name[:-13] # name_multilang_xx | |
| lang_code = sparse.name[-2:] | |
| base_field_name, lang_code = sparse.name.rsplit('_multilang_', 1) |
| if not include_i18n: | ||
| representation[base_field_name] = sparse.value | ||
| else: | ||
| if lang_code == multi.get_language(request): |
geonode/metadata/multilang/views.py
Outdated
| field_names = multi.get_multilang_fields_for_lang(lang) | ||
|
|
||
| request = getattr(self, "request", None) | ||
| include_i18n = request.query_params.get('include_i18n', 'false').lower() == 'true' if request else False |
There was a problem hiding this comment.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #13985 +/- ##
==========================================
+ Coverage 74.07% 74.32% +0.25%
==========================================
Files 950 950
Lines 56826 57028 +202
Branches 7719 7732 +13
==========================================
+ Hits 42093 42388 +295
+ Misses 13044 12939 -105
- Partials 1689 1701 +12 🚀 New features to boost your workflow:
|
| include_i18n = params.get("include_i18n", "false").lower() == "true" if request else False | ||
|
|
||
| if settings.MULTILANG_FIELDS and hasattr(instance, "_multilang_sparse_prefetch"): | ||
| for sparse in instance._multilang_sparse_prefetch: |
There was a problem hiding this comment.
using get_all_multilang_fields, you may loop on the returned fields instead of _multilang_sparse_prefetch
There was a problem hiding this comment.
@etj
I’ve applied the suggested changes to get_all_multilang_fields (returning a dict) and updated the serializer as well
etj
left a comment
There was a problem hiding this comment.
Logic seems ok.
Pls add some tests for the new option.
Fixes #13971
Checklist
For all pull requests:
The following are required only for core and extension modules (they are welcomed, but not required, for contrib modules):
Submitting the PR does not require you to check all items, but by the time it gets merged, they should be either satisfied or inapplicable.