Skip to content

Commit

Permalink
Merge pull request #5 from oarepo/krist/be-217-add-links-to-requests-…
Browse files Browse the repository at this point in the history
…on-main-record

Krist/be 217 add links to requests on main record
  • Loading branch information
SilvyPuzzlewell authored Feb 23, 2024
2 parents fb37e69 + 79daea3 commit 85675f8
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 104 deletions.
48 changes: 30 additions & 18 deletions oarepo_requests/resources/oarepo/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,21 @@ def stringify_first_val(dct):
identity=g.identity,
data=resource_requestctx.data,
request_type=resource_requestctx.data.pop("request_type", None),
receiver=stringify_first_val(resource_requestctx.data.pop("receiver", None))
if resource_requestctx.data
else None,
creator=stringify_first_val(resource_requestctx.data.pop("creator", None))
if resource_requestctx.data
else None,
topic=stringify_first_val(resource_requestctx.data.pop("topic", None))
if resource_requestctx.data
else None,
receiver=(
stringify_first_val(resource_requestctx.data.pop("receiver", None))
if resource_requestctx.data
else None
),
creator=(
stringify_first_val(resource_requestctx.data.pop("creator", None))
if resource_requestctx.data
else None
),
topic=(
stringify_first_val(resource_requestctx.data.pop("topic", None))
if resource_requestctx.data
else None
),
expand=resource_requestctx.args.get("expand", False),
)

Expand All @@ -83,15 +89,21 @@ def stringify_first_val(dct):
identity=g.identity,
data=resource_requestctx.data,
request_type=resource_requestctx.data.pop("request_type", None),
receiver=stringify_first_val(resource_requestctx.data.pop("receiver", None))
if resource_requestctx.data
else None,
creator=stringify_first_val(resource_requestctx.data.pop("creator", None))
if resource_requestctx.data
else None,
topic=stringify_first_val(resource_requestctx.data.pop("topic", None))
if resource_requestctx.data
else None,
receiver=(
stringify_first_val(resource_requestctx.data.pop("receiver", None))
if resource_requestctx.data
else None
),
creator=(
stringify_first_val(resource_requestctx.data.pop("creator", None))
if resource_requestctx.data
else None
),
topic=(
stringify_first_val(resource_requestctx.data.pop("topic", None))
if resource_requestctx.data
else None
),
expand=resource_requestctx.args.get("expand", False),
)

Expand Down
9 changes: 2 additions & 7 deletions oarepo_requests/services/results.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from invenio_records_resources.services.errors import PermissionDeniedError
from oarepo_runtime.services.results import RecordItem

from oarepo_requests.services.schema import RequestTypeSchema
from oarepo_requests.utils import (
allowed_request_types_for_record_cls,
allowed_request_types_for_record,
get_matching_service_for_record,
get_requests_service_for_records_service,
)
Expand All @@ -12,7 +11,7 @@
class RequestTypesComponent:
def update_data(self, identity, record, projection):
request_types_list = []
allowed_request_types = allowed_request_types_for_record_cls(type(record))
allowed_request_types = allowed_request_types_for_record(record)
for request_name, request_type in allowed_request_types.items():
if hasattr(
request_type, "can_possibly_create"
Expand Down Expand Up @@ -48,7 +47,3 @@ def update_data(self, identity, record, projection):
requests = []
if requests:
projection["requests"] = requests


class RequestsAwareResultItem(RecordItem):
components = [RequestsComponent(), RequestTypesComponent()]
35 changes: 1 addition & 34 deletions oarepo_requests/services/ui_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from invenio_requests.proxies import current_request_type_registry
from marshmallow import validate
from oarepo_runtime.i18n import lazy_gettext as _
from oarepo_runtime.services.schema.ui import InvenioUISchema, LocalizedDateTime
from oarepo_runtime.services.schema.ui import LocalizedDateTime

from oarepo_requests.proxies import current_oarepo_requests
from oarepo_requests.resolvers.ui import fallback_entity_reference_ui_resolver
Expand Down Expand Up @@ -76,12 +76,6 @@ class UIBaseRequestSchema(UIRequestSchemaMixin, NoneReceiverGenericRequestSchema
""""""


def get_request_ui_schema(request_type_schema):
return type(
"CustomUIRequestSchema", (UIRequestSchemaMixin, request_type_schema), {}
)


class UIRequestTypeSchema(RequestTypeSchema):
name = ma.fields.String()
description = ma.fields.String()
Expand All @@ -101,30 +95,3 @@ def add_type_details(self, data, **kwargs):
class UIRequestsSerializationMixin(RequestsSchemaMixin):
requests = ma.fields.List(ma.fields.Nested(UIBaseRequestSchema))
request_types = ma.fields.List(ma.fields.Nested(UIRequestTypeSchema))


class RequestsUISchema(InvenioUISchema, UIRequestsSerializationMixin):
"""
@ma.pre_dump
def expand_references(self, data, **kwargs):
def one_element_dict_key(dct):
return list(dct.keys())[0]
if "requests" in data:
for request in data["requests"]:
if "created_by" in request:
key = one_element_dict_key(request["created_by"])
if key in ENTITY_REFERENCE_UI_RESOLVERS:
extended_reference = ENTITY_REFERENCE_UI_RESOLVERS[key](system_identity, request["created_by"])
request["created_by"] = extended_reference
if "receiver" in request:
key = one_element_dict_key(request["receiver"])
if key in ENTITY_REFERENCE_UI_RESOLVERS:
extended_reference = ENTITY_REFERENCE_UI_RESOLVERS[key](system_identity, request["receiver"])
request["receiver"] = extended_reference
if "topic" in request:
key = one_element_dict_key(request["topic"])
extended_reference = ENTITY_REFERENCE_UI_RESOLVERS[key](system_identity, request["topic"])
return data
"""
20 changes: 19 additions & 1 deletion oarepo_requests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,32 @@
from oarepo_requests.errors import OpenRequestAlreadyExists


def allowed_request_types_for_record(record):
request_types = current_request_type_registry._registered_types
ret = {}
try:
record_ref = list(ResolverRegistry.reference_entity(record).keys())[0]
except:
# log?
return ret
for request_name, request_type in request_types.items():
allowed_type_keys = set(request_type.allowed_topic_ref_types)
if record_ref in allowed_type_keys:
ret[request_name] = request_type
return ret


def allowed_request_types_for_record_cls(queryied_record_cls):
# works only for record resolvers which have record_cls and type_key
# and assumes 1:1 type_key - record_cls mapping; type key is the serialized type name in the ref dict

request_types = current_request_type_registry._registered_types
resolvers = list(ResolverRegistry.get_registered_resolvers())
# possibly the mapping doesn't have to be 1:1
type_key2record_cls = {
resolver.type_key: resolver.record_cls
for resolver in resolvers
if hasattr(resolver, "type_key")
if hasattr(resolver, "type_key") and hasattr(resolver, "record_cls")
}
ret = {}
for request_name, request_type in request_types.items():
Expand Down
9 changes: 4 additions & 5 deletions run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ python3 -m venv $BUILDER_VENV
pip install -U setuptools pip wheel
pip install -U oarepo-model-builder-tests oarepo-model-builder-requests oarepo-model-builder-drafts


if test -d ./$BUILD_TEST_DIR/$MODEL; then
rm -rf ./$BUILD_TEST_DIR/$MODEL
fi

#oarepo-compile-model ./$CODE_TEST_DIR/$MODEL.yaml --output-directory ./$BUILD_TEST_DIR/$MODEL -vvv
oarepo-compile-model ./$CODE_TEST_DIR/$MODEL.yaml --output-directory ./$BUILD_TEST_DIR/$MODEL -vvv

MODEL_VENV=".venv-tests"

Expand All @@ -34,10 +33,10 @@ python3 -m venv $MODEL_VENV
. $MODEL_VENV/bin/activate
pip install -U setuptools pip wheel
pip install "oarepo[tests]==$OAREPO_VERSION.*"
#pip install -e "./$BUILD_TEST_DIR/${MODEL}"
pip install -e "./$BUILD_TEST_DIR/${MODEL}"
pip install oarepo-ui
pip install -e .
pip install -e ./tests/mock_requests

#pytest $BUILD_TEST_DIR/test_requests
#pytest $BUILD_TEST_DIR/test_ui
pytest $BUILD_TEST_DIR/test_requests
pytest $BUILD_TEST_DIR/test_ui
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-requests
version = 1.1.0
version = 1.1.1
description =
authors = Ronald Krist <[email protected]>
readme = README.md
Expand Down
38 changes: 19 additions & 19 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
from invenio_requests.records.api import Request, RequestEventFormat
from thesis.records.api import ThesisDraft, ThesisRecord

from oarepo_requests.resolvers.ui import (
draft_record_entity_reference_ui_resolver,
user_entity_reference_ui_resolver,
)


@pytest.fixture(scope="module")
def create_app(instance_path, entry_points):
Expand All @@ -28,7 +23,7 @@ def publish_request_data_function():
def ret_data(receiver_id, record_id):
return {
"receiver": {"user": receiver_id},
"request_type": "publish_draft",
"request_type": "thesis_draft_publish_draft",
"topic": {"thesis_draft": record_id},
}

Expand All @@ -40,7 +35,7 @@ def delete_record_data_function():
def ret_data(receiver_id, record_id):
return {
"receiver": {"user": receiver_id},
"request_type": "delete_record",
"request_type": "thesis_delete_record",
"topic": {"thesis": record_id},
}

Expand All @@ -62,7 +57,7 @@ def _result(topic_id, request_id):
"timeline": f"https://127.0.0.1:5000/api/requests/extended/{request_id}/timeline",
},
"revision_id": 3,
"type": "publish_draft",
"type": "thesis_draft_publish_draft",
"title": "",
"number": "1",
"status": "submitted",
Expand Down Expand Up @@ -119,7 +114,7 @@ def _result(topic_id, request_id):
"reference": {"thesis_draft": topic_id},
"type": "thesis_draft",
},
"type": "publish_draft",
"type": "thesis_draft_publish_draft",
# 'updated': '2024-01-26T10:06:18.084317'
}

Expand All @@ -136,18 +131,21 @@ def app_config(app_config):
}
]
app_config["JSONSCHEMAS_HOST"] = "localhost"
app_config[
"RECORDS_REFRESOLVER_CLS"
] = "invenio_records.resolver.InvenioRefResolver"
app_config[
"RECORDS_REFRESOLVER_STORE"
] = "invenio_jsonschemas.proxies.current_refresolver_store"
app_config["RECORDS_REFRESOLVER_CLS"] = (
"invenio_records.resolver.InvenioRefResolver"
)
app_config["RECORDS_REFRESOLVER_STORE"] = (
"invenio_jsonschemas.proxies.current_refresolver_store"
)
app_config["CACHE_TYPE"] = "SimpleCache"

app_config["ENTITY_REFERENCE_UI_RESOLVERS"] = {
"user": user_entity_reference_ui_resolver,
"thesis_draft": draft_record_entity_reference_ui_resolver,
}
"""
app_config.setdefault("ENTITY_REFERENCE_UI_RESOLVERS", {}).update({
#"user": user_entity_reference_ui_resolver,
#"thesis_draft": draft_record_entity_reference_ui_resolver,
"thesis": record_entity_reference_ui_resolver
})
"""

return app_config

Expand Down Expand Up @@ -201,6 +199,7 @@ def _create_request(identity, input_data, receiver, request_type, **kwargs):
return _create_request


"""
@pytest.fixture
def request_data_factory():
def create_data(community, topic, data):
Expand All @@ -220,6 +219,7 @@ def create_data(community, topic, data):
return input_data
return create_data
"""


@pytest.fixture()
Expand Down
4 changes: 2 additions & 2 deletions tests/test_requests/test_create_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
def data(receiver_id, record_id):
return {
"receiver": {"user": receiver_id},
"request_type": "non_duplicable",
"request_type": "thesis_draft_non_duplicable",
"topic": {"thesis_draft": record_id},
}

Expand Down Expand Up @@ -62,7 +62,7 @@ def find_request_type(requests, type):
f"{urls['BASE_URL']}{draft1.json['id']}/draft"
)
assert find_request_type(
record_resp_no_request.json["request_types"], "non_duplicable"
record_resp_no_request.json["request_types"], "thesis_draft_non_duplicable"
)
assert (
find_request_type(record_resp_request.json["request_types"], "non_duplicable")
Expand Down
12 changes: 6 additions & 6 deletions tests/test_ui/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ def app_config(app_config):
"port": os.environ.get("OPENSEARCH_PORT", "9200"),
}
]
app_config[
"RECORDS_REFRESOLVER_CLS"
] = "invenio_records.resolver.InvenioRefResolver"
app_config[
"RECORDS_REFRESOLVER_STORE"
] = "invenio_jsonschemas.proxies.current_refresolver_store"
app_config["RECORDS_REFRESOLVER_CLS"] = (
"invenio_records.resolver.InvenioRefResolver"
)
app_config["RECORDS_REFRESOLVER_STORE"] = (
"invenio_jsonschemas.proxies.current_refresolver_store"
)

# for ui tests
app_config["APP_THEME"] = ["semantic-ui"]
Expand Down
8 changes: 4 additions & 4 deletions tests/test_ui/test_ui_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ def test_draft_publish_request_present(
with client_with_login.get(f"/thesis/{example_topic_draft['id']}/edit") as c:
assert c.status_code == 200
data = json.loads(c.text)
assert data["creatable_request_types"]["non_duplicable"] == {
assert data["creatable_request_types"]["thesis_draft_non_duplicable"] == {
"description": "",
"links": {"actions": {"create": "https://127.0.0.1:5000/api/requests"}},
"name": "Non-duplicable",
}
assert data["creatable_request_types"]["publish_draft"] == {
assert data["creatable_request_types"]["thesis_draft_publish_draft"] == {
"description": "request publishing of a draft",
"links": {"actions": {"create": "https://127.0.0.1:5000/api/requests"}},
"name": "Publish-draft",
Expand All @@ -42,12 +42,12 @@ def test_record_delete_request_present(
assert c.status_code == 200
data = json.loads(c.text)
assert len(data["creatable_request_types"]) == 2
assert data["creatable_request_types"]["generic_request"] == {
assert data["creatable_request_types"]["thesis_generic_request"] == {
"description": "",
"links": {"actions": {"create": "https://127.0.0.1:5000/api/requests"}},
"name": "Generic-request",
}
assert data["creatable_request_types"]["delete_record"] == {
assert data["creatable_request_types"]["thesis_delete_record"] == {
"description": "request deletion of published record",
"links": {"actions": {"create": "https://127.0.0.1:5000/api/requests"}},
"name": "Delete-record",
Expand Down
Loading

0 comments on commit 85675f8

Please sign in to comment.