Skip to content
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

SXDEDPCXZIC-397 / fix additional filesize issues #368

Open
wants to merge 2 commits into
base: SXDEDPCXZIC-315
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion ckanext/datavicmain/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion ckanext/datavicmain/iar_ckan_dataset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 16 additions & 10 deletions ckanext/datavicmain/logic/action.py
Original file line number Diff line number Diff line change
@@ -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_
Expand Down Expand Up @@ -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
Expand All @@ -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"]}
)
Expand All @@ -236,18 +242,18 @@ 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
)
for field, error in resource_error.items()
}
)
if errors:
raise ValidationError(errors)
if error_dict:
raise ValidationError(error_dict)


@toolkit.side_effect_free
Expand Down
21 changes: 21 additions & 0 deletions ckanext/datavicmain/logic/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 23 additions & 0 deletions ckanext/datavicmain/templates/scheming/package/resource_read.html
Original file line number Diff line number Diff line change
@@ -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 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
and field.display_snippet is not none %}
<tr>
<th scope="row">
{{ h.scheming_language_text(field.label) }}
</th>
<td>
{% snippet 'scheming/snippets/display_field.html',
field=field, data=res, entity_type='dataset',
object_type=dataset_type %}
</td>
</tr>
{% endif %}
{% endif %}
{% endfor %}
{% endblock %}