Skip to content

Commit

Permalink
Krist/be 503 new version request user is not redirected to the new (#283
Browse files Browse the repository at this point in the history
)

* links conditioned by permissions and search item links

* test fix

* configurable links template for search item links

* format

* version bump

* format

* dependency refactor

* paginations html link

* test fix

* test fix

* version bump

---------

Co-authored-by: Ronald Krist <[email protected]>
  • Loading branch information
SilvyPuzzlewell and Ronald Krist authored Dec 4, 2024
1 parent 633cac4 commit b0b4ff6
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 9 deletions.
8 changes: 8 additions & 0 deletions oarepo_model_builder/datatypes/components/model/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ class Meta:
"doc": "List of additional arguments that will be passed to the service config constructor"
},
)
search_item_links_template_cls = ma.fields.String(
attribute="search-item-links-template-cls",
data_key="search-item-links-template-cls",
)
skip = ma.fields.Boolean()


Expand Down Expand Up @@ -149,6 +153,10 @@ def before_model_prepare(self, datatype, *, context, **kwargs):
)
config.setdefault("components", [])
config.setdefault("additional-args", [])
config.setdefault(
"search-item-links-template-cls",
"invenio_records_resources.services.LinksTemplate",
)
convert_config_to_qualified_name(config)

service = set_default(datatype, "service", {})
Expand Down
50 changes: 47 additions & 3 deletions oarepo_model_builder/datatypes/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,52 @@ def links(self):
Link(
name="self",
link_class="RecordLink",
link_args=[f'"{{+api}}{url_prefix}{{id}}"'],
imports=[Import("invenio_records_resources.services.RecordLink")],
link_args=[
f'"{{+api}}{url_prefix}{{id}}"',
'when=has_permission("read")',
],
imports=[
Import("invenio_records_resources.services.RecordLink"),
Import("oarepo_runtime.services.config.has_permission"),
],
),
Link(
name="self_html",
link_class="RecordLink",
link_args=[
f'"{{+ui}}{html_url_prefix}{{id}}"',
'when=has_permission("read")',
],
imports=[
Import("invenio_records_resources.services.RecordLink"),
Import("oarepo_runtime.services.config.has_permission"),
],
),
],
"links_search_item": [
Link(
name="self",
link_class="RecordLink",
link_args=[
f'"{{+api}}{url_prefix}{{id}}"',
'when=has_permission("read")',
],
imports=[
Import("invenio_records_resources.services.RecordLink"),
Import("oarepo_runtime.services.config.has_permission"),
],
),
Link(
name="self_html",
link_class="RecordLink",
link_args=[
f'"{{+ui}}{html_url_prefix}{{id}}"',
'when=has_permission("read")',
],
imports=[
Import("invenio_records_resources.services.RecordLink"),
Import("oarepo_runtime.services.config.has_permission"),
],
imports=[Import("invenio_records_resources.services.RecordLink")],
),
],
"links_search": [
Expand All @@ -79,6 +115,14 @@ def links(self):
Import("invenio_records_resources.services.pagination_links")
],
),
Link(
name=None,
link_class="pagination_links_html",
link_args=[f'"{{+ui}}{url_prefix}{{?args*}}"'],
imports=[
Import("oarepo_runtime.services.records.pagination_links_html")
],
),
],
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
{{ vars.search_options.class|imports }}
{{ vars.record_item.class|imports }}
{{ vars.record_list.class|imports }}
{{ vars.service_config.search_item_links_template_cls|imports }}
{% for c in vars.service_config.components %}
{{ c|code_imports }}
{% endfor %}
Expand Down Expand Up @@ -36,6 +37,9 @@ class {{ vars.service_config|class_header }}:
{% if not vars.service_config.skip %}
service_id = "{{ vars.service_config.service_id }}"
{% endif %}
{% if not vars.service_config.skip %}
search_item_links_template = {{ vars.service_config.search_item_links_template_cls|base_name }}
{% endif %}

components = [ {% for b in vars.service_config.base_classes %}*{{ b|base_name }}.components{% if not loop.last %}, {% endif %}{% endfor %}{% for c in vars.service_config.components %}, {{ c|extra_code }} {% endfor %}]

Expand Down
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-model-builder
version = 4.0.95
version = 4.0.96
description = A utility library that generates OARepo required data model files from a JSON specification file
authors = Miroslav Bauer <[email protected]>, Miroslav Simek <[email protected]>
readme = README.md
Expand Down
4 changes: 2 additions & 2 deletions tests-model/test_running_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def test_running_server():
break
for hit in data["hits"]["hits"]:
requests.delete(hit["links"]["self"], verify=False) # NOSONAR

assert data == {
"hits": {"hits": [], "total": 0},
"aggregations": {
Expand All @@ -28,7 +27,8 @@ def test_running_server():
},
"sortBy": "newest",
"links": {
"self": "https://127.0.0.1:5000/api/complex-model/?page=1&size=25&sort=newest"
"self": "https://127.0.0.1:5000/api/complex-model/?page=1&size=25&sort=newest",
"self_html": "https://127.0.0.1:5000/complex-model/?page=1&size=25&sort=newest"
},
}

Expand Down
1 change: 1 addition & 0 deletions tests/test_datatype_prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ def test_prepare_datatype():
"oarepo_runtime.services.config.service.PermissionsPresetsConfigMixin",
"invenio_records_resources.services.RecordServiceConfig{InvenioRecordServiceConfig}",
],
"search-item-links-template-cls": "invenio_records_resources.services.LinksTemplate",
"additional-args": [],
"components": [],
},
Expand Down
2 changes: 2 additions & 0 deletions tests/test_model_saver.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ def test_model_saver():
],
"additional-args": [],
"components": [],
"search-item-links-template-cls": "invenio_records_resources.services.LinksTemplate",
},
"service": {
"generate": True,
Expand Down Expand Up @@ -583,6 +584,7 @@ def test_model_saver_invenio():
],
"additional-args": [],
"components": [],
"search-item-links-template-cls": "invenio_records_resources.services.LinksTemplate",
},
"service": {
"generate": True,
Expand Down
44 changes: 41 additions & 3 deletions tests/test_simple_builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,44 +382,82 @@ def test_service_config():
from oarepo_runtime.services.config.service import PermissionsPresetsConfigMixin
from invenio_records_resources.services import RecordLink
from invenio_records_resources.services import pagination_links
from oarepo_runtime.services.config import has_permission
from oarepo_runtime.services.records import pagination_links_html
from test.records.api import TestRecord
from test.services.records.permissions import TestPermissionPolicy
from test.services.records.schema import TestSchema
from test.services.records.search import TestSearchOptions
from test.services.records.results import TestRecordItem
from test.services.records.results import TestRecordList
from invenio_records_resources.services import LinksTemplate
class TestServiceConfig(PermissionsPresetsConfigMixin, InvenioRecordServiceConfig):
"""TestRecord service config."""
result_item_cls = TestRecordItem
result_list_cls = TestRecordList
PERMISSIONS_PRESETS = ["everyone"]
PERMISSIONS_PRESETS = ["everyone" ]
url_prefix = "/test/"
base_permission_policy_cls = TestPermissionPolicy
schema = TestSchema
search = TestSearchOptions
record_cls = TestRecord
service_id = "test"
search_item_links_template = LinksTemplate
components = [ *PermissionsPresetsConfigMixin.components, *InvenioRecordServiceConfig.components]
model = "test"
@property
def links_item(self):
return {
"self":RecordLink("{+api}/test/{id}"),
"self":RecordLink("{+api}/test/{id}", when=has_permission("read")),
"self_html":RecordLink("{+ui}/test/{id}"),
"self_html":RecordLink("{+ui}/test/{id}", when=has_permission("read")),
}
@property
def links_search_item(self):
return {
"self":RecordLink("{+api}/test/{id}", when=has_permission("read")),
"self_html":RecordLink("{+ui}/test/{id}", when=has_permission("read")),
}
@property
def links_search(self):
return {
**pagination_links("{+api}/test/{?args*}"),
**pagination_links_html("{+ui}/test/{?args*}"),
}
'''
)

Expand Down

0 comments on commit b0b4ff6

Please sign in to comment.