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

Empty record test & fixes #112

Merged
Merged
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: 5 additions & 2 deletions oarepo_ui/resources/catalog.py
Original file line number Diff line number Diff line change
@@ -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/"
Expand Down Expand Up @@ -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,
Expand Down
16 changes: 6 additions & 10 deletions oarepo_ui/resources/components.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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)
def before_ui_detail(self, **kwargs):
self.before_ui_edit(**kwargs)
2 changes: 1 addition & 1 deletion oarepo_ui/resources/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
3 changes: 2 additions & 1 deletion oarepo_ui/resources/file_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
return ret
4 changes: 1 addition & 3 deletions oarepo_ui/resources/resource.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import copy
import json
from functools import partial

import deepmerge
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
)
Expand Down
2 changes: 1 addition & 1 deletion oarepo_ui/theme/webpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 5 additions & 3 deletions oarepo_ui/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -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
Expand Down
66 changes: 66 additions & 0 deletions tests/test_dump_empty.py
Original file line number Diff line number Diff line change
@@ -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,
}
14 changes: 7 additions & 7 deletions tests/test_ui_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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


Expand Down