diff --git a/oarepo_ui/resources/catalog.py b/oarepo_ui/resources/catalog.py index a3decb8d..4560e7a3 100644 --- a/oarepo_ui/resources/catalog.py +++ b/oarepo_ui/resources/catalog.py @@ -1,9 +1,10 @@ import os import re from pathlib import Path -from jinjax.component import Component + import jinja2 from jinjax import Catalog +from jinjax.component import Component from jinjax.exceptions import ComponentNotFound DEFAULT_URL_ROOT = "/static/components/" @@ -75,7 +76,9 @@ def _get_component_path( f"or one following the pattern {name_dot}*{file_ext}" ) - def _get_from_file(self, *, prefix: str, name: str, url_prefix: str, file_ext: str) -> "Component": + def _get_from_file( + self, *, prefix: str, name: str, url_prefix: str, file_ext: str + ) -> "Component": root_path, path = self._get_component_path(prefix, name, file_ext=file_ext) component = Component( name=name, diff --git a/oarepo_ui/resources/components.py b/oarepo_ui/resources/components.py index 9ec1db46..3e02a0c1 100644 --- a/oarepo_ui/resources/components.py +++ b/oarepo_ui/resources/components.py @@ -1,7 +1,6 @@ from flask import current_app from invenio_i18n.ext import current_i18n from invenio_records_resources.services.records.components import ServiceComponent -from invenio_records_resources.proxies import current_service_registry from oarepo_runtime.datastreams.utils import get_file_service_for_record_service @@ -75,15 +74,12 @@ def fill_permissions(self, resource, record, extra_context, identity): class FilesComponent(ServiceComponent): - def before_ui_edit( - self, *, record, resource, extra_context, identity, **kwargs - ): + def before_ui_edit(self, *, record, resource, extra_context, identity, **kwargs): file_service = get_file_service_for_record_service( - resource.api_service, record=record) - files = file_service.list_files(identity, record['id']) + resource.api_service, record=record + ) + files = file_service.list_files(identity, record["id"]) extra_context["files"] = files.to_dict() - def before_ui_detail( - self, **kwargs - ): - self.before_ui_edit(**kwargs) \ No newline at end of file + def before_ui_detail(self, **kwargs): + self.before_ui_edit(**kwargs) diff --git a/oarepo_ui/resources/config.py b/oarepo_ui/resources/config.py index 925ca84c..233a1dce 100644 --- a/oarepo_ui/resources/config.py +++ b/oarepo_ui/resources/config.py @@ -148,7 +148,7 @@ def search_app_config(self, identity, api_config, overrides={}, **kwargs): ), selected_options=self.search_active_sort_options(api_config, identity), default_option=api_config.search.sort_default, - no_query_option=api_config.search.sort_default_no_query + no_query_option=api_config.search.sort_default_no_query, ), facets=self.search_facets_config( available_facets=self.search_available_facets(api_config, identity), diff --git a/oarepo_ui/resources/file_resource.py b/oarepo_ui/resources/file_resource.py index 6dc0abd1..780a47d1 100644 --- a/oarepo_ui/resources/file_resource.py +++ b/oarepo_ui/resources/file_resource.py @@ -6,9 +6,10 @@ class S3RedirectFileResource(FileResource): A workaround for the fact that the file resource with S3 backend does not return HTTP 302 for pre-signed URLs. """ + def read_content(self): ret = super().read_content() if ret[0].status_code == 302: return ret[0], 302 else: - return ret \ No newline at end of file + return ret diff --git a/oarepo_ui/resources/resource.py b/oarepo_ui/resources/resource.py index 559b8f7a..52a91789 100644 --- a/oarepo_ui/resources/resource.py +++ b/oarepo_ui/resources/resource.py @@ -1,5 +1,4 @@ import copy -import json from functools import partial import deepmerge @@ -29,7 +28,7 @@ # Resource # from ..proxies import current_oarepo_ui -from .catalog import get_jinja_template, lazy_string_encoder +from .catalog import get_jinja_template from .config import RecordsUIResourceConfig, UIResourceConfig request_export_args = request_parser( @@ -104,7 +103,6 @@ def empty_record(self, resource_requestctx, **kwargs): record = deepmerge.always_merger.merge( record, copy.deepcopy(self.config.empty_record) ) - record['metadata']={} self.run_components( "empty_record", resource_requestctx=resource_requestctx, record=record ) diff --git a/oarepo_ui/theme/webpack.py b/oarepo_ui/theme/webpack.py index eb7b4b0d..791b864a 100644 --- a/oarepo_ui/theme/webpack.py +++ b/oarepo_ui/theme/webpack.py @@ -29,7 +29,7 @@ "oarepo_ui": "./js/oarepo_ui/index.js", "oarepo_ui_search": "./js/oarepo_ui/search/index.js", "oarepo_ui_forms": "./js/oarepo_ui/forms/index.js", - "oarepo_ui_theme": "./js/oarepo_ui/theme.js" + "oarepo_ui_theme": "./js/oarepo_ui/theme.js", }, dependencies={ "@tanstack/react-query": "^4.32.0", diff --git a/oarepo_ui/utils.py b/oarepo_ui/utils.py index 3d276821..8be6f1a8 100644 --- a/oarepo_ui/utils.py +++ b/oarepo_ui/utils.py @@ -56,10 +56,12 @@ def dump_empty(schema_or_field): schema = schema_or_field() return {k: dump_empty(v) for (k, v) in schema.fields.items()} if isinstance(schema_or_field, fields.List): - field = schema_or_field - return [dump_empty(field.inner)] - if isinstance(schema_or_field, NestedAttribute): + # return [dump_empty(schema_or_field.inner)] + return [] + if isinstance(schema_or_field, (NestedAttribute, fields.Nested)): field = schema_or_field return dump_empty(field.nested) + if isinstance(schema_or_field, fields.Str): + return "" return None diff --git a/setup.cfg b/setup.cfg index eec14f93..49c9c70d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = oarepo-ui -version = 5.0.84 +version = 5.0.85 description = UI module for invenio 3.5+ long_description = file: README.md long_description_content_type = text/markdown diff --git a/tests/test_dump_empty.py b/tests/test_dump_empty.py new file mode 100644 index 00000000..a53a99de --- /dev/null +++ b/tests/test_dump_empty.py @@ -0,0 +1,66 @@ +import marshmallow as ma +from invenio_records_resources.services.records.schema import ( + BaseRecordSchema as InvenioBaseRecordSchema, +) + +from oarepo_ui.utils import dump_empty + + +class ModelSchemaWithNoMetadata(InvenioBaseRecordSchema): + title = ma.fields.String() + + class Meta: + unknown = ma.INCLUDE + + +class NestedSchema(ma.Schema): + title = ma.fields.String() + count = ma.fields.Integer() + valid = ma.fields.Boolean() + + class Meta: + unknown = ma.INCLUDE + + +class SimpleMetadataSchema(ma.Schema): + title = ma.fields.String() + simple_arr = ma.fields.List(ma.fields.String()) + object_arr = ma.fields.List(ma.fields.Nested(NestedSchema)) + nested_obj = ma.fields.Nested(NestedSchema) + + class Meta: + unknown = ma.INCLUDE + + +class ModelSchemaWithSimpleMetadata(InvenioBaseRecordSchema): + metadata = ma.fields.Nested(SimpleMetadataSchema) + + class Meta: + unknown = ma.INCLUDE + + +def test_empty_dump_no_metadata(): + assert dump_empty(ModelSchemaWithNoMetadata) == { + "created": None, + "id": "", + "links": None, + "revision_id": None, + "title": "", + "updated": None, + } + + +def test_empty_dump(): + assert dump_empty(ModelSchemaWithSimpleMetadata) == { + "created": None, + "id": "", + "links": None, + "metadata": { + "nested_obj": {"count": None, "title": "", "valid": None}, + "object_arr": [], + "simple_arr": [], + "title": "", + }, + "revision_id": None, + "updated": None, + } diff --git a/tests/test_ui_resource.py b/tests/test_ui_resource.py index dc31063d..1751c7f2 100644 --- a/tests/test_ui_resource.py +++ b/tests/test_ui_resource.py @@ -2,7 +2,7 @@ def test_ui_resource_create_new(app, record_ui_resource, record_service): - assert record_ui_resource.empty_record(None) == {'metadata': {}, 'title': None} + assert record_ui_resource.empty_record(None) == {"title": ''} def test_ui_resource_form_config(app, record_ui_resource): @@ -16,12 +16,12 @@ def test_permissions_on_detail( with client.get(f"/simple-model/{simple_record.id}") as c: assert c.status_code == 200 assert ( - 'permissions={'can_edit': False, 'can_new_version': False, ' - ''can_manage': False, 'can_update_draft': False, ' - ''can_read_files': True, 'can_review': False, ' - ''can_view': False, 'can_delete_draft': False, ' - ''can_manage_files': False, 'can_manage_record_access': ' - 'False}' + "permissions={'can_edit': False, 'can_new_version': False, " + "'can_manage': False, 'can_update_draft': False, " + "'can_read_files': True, 'can_review': False, " + "'can_view': False, 'can_delete_draft': False, " + "'can_manage_files': False, 'can_manage_record_access': " + "False}" ) in c.text