From a9c2f7fcfc89975b04abd2adc8a62e1daea3d5f6 Mon Sep 17 00:00:00 2001 From: alexmorev Date: Thu, 14 Nov 2024 03:18:59 +0200 Subject: [PATCH 1/2] SXDEDPCXZIC-397 / fix additional filesize issues --- ckanext/datavicmain/helpers.py | 7 ++++- ckanext/datavicmain/iar_ckan_dataset.yaml | 2 +- ckanext/datavicmain/logic/action.py | 26 ++++++++++++------- ckanext/datavicmain/logic/validators.py | 21 +++++++++++++++ .../scheming/package/resource_read.html | 23 ++++++++++++++++ 5 files changed, 67 insertions(+), 12 deletions(-) create mode 100644 ckanext/datavicmain/templates/scheming/package/resource_read.html diff --git a/ckanext/datavicmain/helpers.py b/ckanext/datavicmain/helpers.py index 464c085..d2b8227 100644 --- a/ckanext/datavicmain/helpers.py +++ b/ckanext/datavicmain/helpers.py @@ -609,7 +609,12 @@ def localized_filesize(size_bytes: Any) -> str: smaller than 1000. """ - if not isinstance(size_bytes, int) or size_bytes < 0: + if isinstance(size_bytes, str) and not size_bytes.isdecimal(): + return size_bytes + + size_bytes = int(size_bytes) + + if size_bytes < 0: return "" size_name = ("bytes", "KB", "MB", "GB", "TB") diff --git a/ckanext/datavicmain/iar_ckan_dataset.yaml b/ckanext/datavicmain/iar_ckan_dataset.yaml index b9f9313..ec46ffd 100644 --- a/ckanext/datavicmain/iar_ckan_dataset.yaml +++ b/ckanext/datavicmain/iar_ckan_dataset.yaml @@ -396,7 +396,7 @@ resource_fields: - field_name: filesize label: Filesize display_snippet: filesize.html - validators: int_validator + validators: ignore_missing datavic_filesize_validator help_text: File size in bytes. Leave blank if autocalculation is required. - field_name: release_date diff --git a/ckanext/datavicmain/logic/action.py b/ckanext/datavicmain/logic/action.py index 8f1319d..4c5a908 100644 --- a/ckanext/datavicmain/logic/action.py +++ b/ckanext/datavicmain/logic/action.py @@ -1,7 +1,7 @@ from __future__ import annotations import logging -from typing import Any +from typing import Any, cast import ckanapi from sqlalchemy import or_ @@ -200,8 +200,8 @@ def resource_update( result = next_(context, data_dict) return result - except ValidationError: - _show_errors_in_sibling_resources(context, data_dict) + except ValidationError as valid_errors: + _show_errors_in_sibling_resources(context, data_dict, valid_errors) @toolkit.chained_action @@ -211,14 +211,20 @@ def resource_create( try: result = next_(context, data_dict) return result - except ValidationError: - _show_errors_in_sibling_resources(context, data_dict) + except ValidationError as valid_errors: + _show_errors_in_sibling_resources(context, data_dict, valid_errors) def _show_errors_in_sibling_resources( - context: Context, data_dict: DataDict + context: Context, data_dict: DataDict, valid_errors: DataDict ) -> Any: """Retrieves and raises validation errors for resources within the same package.""" + try: + error_dict = cast( + "list[type.ErrorDict]", valid_errors.error_dict['resources'])[-1] + except (KeyError, IndexError): + error_dict = valid_errors.error_dict + pkg_dict = toolkit.get_action("package_show")( context, {"id": data_dict["package_id"]} ) @@ -236,9 +242,9 @@ def _show_errors_in_sibling_resources( resources_errors = errors.pop("resources", []) for i, resource_error in enumerate(resources_errors): - if not resource_error: + if not resource_error or data_dict.get("id") == pkg_dict["resources"][i]["id"]: continue - errors.update( + error_dict.update( { f"Field '{field}' in the resource '{pkg_dict['resources'][i]['name']}'": ( error @@ -246,8 +252,8 @@ def _show_errors_in_sibling_resources( for field, error in resource_error.items() } ) - if errors: - raise ValidationError(errors) + if error_dict: + raise ValidationError(error_dict) @toolkit.side_effect_free diff --git a/ckanext/datavicmain/logic/validators.py b/ckanext/datavicmain/logic/validators.py index 7a52af1..5dc09f1 100644 --- a/ckanext/datavicmain/logic/validators.py +++ b/ckanext/datavicmain/logic/validators.py @@ -178,3 +178,24 @@ def datavic_private_validator( not suitable for public release""" ) return + + +def datavic_filesize_validator( + key: types.FlattenKey, + data: types.FlattenDataDict, + errors: types.FlattenErrorDict, + context: types.Context, +) -> Any: + """ + Check if the field value is an integer + """ + value = data.get(key) + + if value: + try: + return int(value) + except (TypeError, ValueError): + errors[key].append( + "Enter file size in bytes (numeric values only), or leave blank" + ) + return diff --git a/ckanext/datavicmain/templates/scheming/package/resource_read.html b/ckanext/datavicmain/templates/scheming/package/resource_read.html new file mode 100644 index 0000000..a4e0502 --- /dev/null +++ b/ckanext/datavicmain/templates/scheming/package/resource_read.html @@ -0,0 +1,23 @@ +{% ckan_extends %} + +{% block resource_fields %} + {% for field in schema.resource_fields %} + {% if field.field_name == "filesize" and res.filesize and res.filesize.startswith("eg.") or not res.filesize %} + {% elif field.field_name == "size" %} + {% else %} + {% if field.field_name not in exclude_fields + and field.display_snippet is not none %} + + + {{ h.scheming_language_text(field.label) }} + + + {% snippet 'scheming/snippets/display_field.html', + field=field, data=res, entity_type='dataset', + object_type=dataset_type %} + + + {% endif %} + {% endif %} + {% endfor %} +{% endblock %} From ad5349cc96a4486d29662303b39b9f533afd61d2 Mon Sep 17 00:00:00 2001 From: alexmorev Date: Thu, 14 Nov 2024 04:20:44 +0200 Subject: [PATCH 2/2] SXDEDPCXZIC-397 / fix string variable displaying --- .../datavicmain/templates/scheming/package/resource_read.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ckanext/datavicmain/templates/scheming/package/resource_read.html b/ckanext/datavicmain/templates/scheming/package/resource_read.html index a4e0502..0167b68 100644 --- a/ckanext/datavicmain/templates/scheming/package/resource_read.html +++ b/ckanext/datavicmain/templates/scheming/package/resource_read.html @@ -2,7 +2,7 @@ {% block resource_fields %} {% for field in schema.resource_fields %} - {% if field.field_name == "filesize" and res.filesize and res.filesize.startswith("eg.") or not res.filesize %} + {% if field.field_name == "filesize" and res.filesize and res.filesize is string and res.filesize.startswith("eg.") or not res.filesize %} {% elif field.field_name == "size" %} {% else %} {% if field.field_name not in exclude_fields