-
Notifications
You must be signed in to change notification settings - Fork 1
feat(gdi_userportal): honor accept-language for enhanced package resp… #214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hcvdwerf
wants to merge
7
commits into
main
Choose a base branch
from
miltilingual-suuport
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
653c3cc
feat(gdi_userportal): honor accept-language for enhanced package resp…
d51ddab
add missing copyright
b630983
feat(multilingual) Add multi lingual support for english and dutch
4210002
added license header
88ceaf2
Apply suggestions from code review
hcvdwerf b905a22
apply sourcery comments
b36da53
Merge commit 'ad398f5e582647653539daf342fa9e4454e22ea1' into miltilin…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
"""Template helpers for ckanext-gdi-userportal.""" | ||
|
||
from __future__ import annotations | ||
|
||
from collections.abc import Iterable | ||
from typing import Any | ||
|
||
import ckan.plugins.toolkit as tk | ||
|
||
|
||
def _ensure_data_dict(data: dict[str, Any] | None, package_id: str | None) -> dict[str, Any]: | ||
"""Return a dataset dict for the current helper call. | ||
When a dataset dict is not provided but an id is, fetch it via the | ||
``package_show`` action. Failures are ignored so the helper still returns | ||
a sensible default instead of aborting rendering. | ||
""" | ||
if data is not None: | ||
return data | ||
|
||
if not package_id: | ||
return {} | ||
|
||
try: | ||
return tk.get_action("package_show")( | ||
{"ignore_auth": True}, {"id": package_id} | ||
) | ||
except (tk.ObjectNotFound, tk.NotAuthorized): | ||
return {} | ||
|
||
|
||
def _value_from_extras(data_dict: dict[str, Any], field_name: str) -> Any: | ||
extras = data_dict.get("extras") | ||
if isinstance(extras, dict): | ||
return extras.get(field_name) | ||
if isinstance(extras, Iterable): | ||
for extra in extras: | ||
if not isinstance(extra, dict): | ||
continue | ||
if extra.get("key") == field_name: | ||
return extra.get("value") | ||
return None | ||
|
||
|
||
def _extract_field_value(field: dict[str, Any], data_dict: dict[str, Any]) -> Any: | ||
field_name = field.get("field_name") | ||
if not field_name or not data_dict: | ||
return None | ||
|
||
if field_name in data_dict: | ||
return data_dict[field_name] | ||
|
||
return _value_from_extras(data_dict, field_name) | ||
|
||
|
||
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 | ||
|
||
|
||
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: | ||
sourcery-ai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
page_missing.append(field_name) | ||
|
||
missing_per_page.append(page_missing) | ||
|
||
return missing_per_page | ||
|
||
|
||
def get_helpers() -> dict[str, Any]: | ||
return {"scheming_missing_required_fields": scheming_missing_required_fields} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.