Skip to content

Commit

Permalink
standard requests non-duplicable; autoapprove for edit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Krist committed Mar 27, 2024
1 parent d893e66 commit 837424b
Show file tree
Hide file tree
Showing 16 changed files with 221 additions and 104 deletions.
3 changes: 1 addition & 2 deletions oarepo_requests/actions/edit_topic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# from .generic import AcceptAction
from invenio_requests.customizations import actions

from ..utils import get_matching_service_for_record
Expand All @@ -12,5 +11,5 @@ def execute(self, identity, uow):
topic_service = get_matching_service_for_record(topic)
if not topic_service:
raise KeyError(f"topic {topic} service not found")
topic_service.edit(identity, topic.id, uow=uow)
topic_service.edit(identity, topic["id"], uow=uow)
super().execute(identity, uow)
16 changes: 16 additions & 0 deletions oarepo_requests/actions/generic.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
from invenio_requests.customizations import actions
from invenio_requests.customizations.actions import RequestActions
from invenio_requests.errors import CannotExecuteActionError


class AutoAcceptSubmitAction(actions.SubmitAction):
log_event = True

def execute(self, identity, uow):
super().execute(identity, uow)
action_obj = RequestActions.get_action(self.request, "accept")
if not action_obj.can_execute():
raise CannotExecuteActionError("accept")
action_obj.execute(identity, uow)


"""
not needed for now
Expand Down
4 changes: 2 additions & 2 deletions oarepo_requests/resources/record/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ class RecordRequestsResourceConfig:
request_view_args = RecordResourceConfig.request_view_args | {
"request_type": ma.fields.Str()
}

"""
@property
def response_handlers(self):
return {
**RequestsResourceConfig.routes,
**RecordResourceConfig.response_handlers,
"application/vnd.inveniordm.v1+json": ResponseHandler(
OARepoRequestsUIJSONSerializer()
),
**super().response_handlers,
}
"""
4 changes: 2 additions & 2 deletions oarepo_requests/types/delete_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

from oarepo_requests.actions.delete_topic import DeleteTopicAcceptAction

from .generic import OARepoRequestType
from .generic import NonDuplicableOARepoRequestType


class DeleteRecordRequestType(OARepoRequestType):
class DeleteRecordRequestType(NonDuplicableOARepoRequestType):
available_actions = {
**RequestType.available_actions,
"accept": DeleteTopicAcceptAction,
Expand Down
6 changes: 4 additions & 2 deletions oarepo_requests/types/edit_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
from oarepo_runtime.i18n import lazy_gettext as _

from oarepo_requests.actions.edit_topic import EditTopicAcceptAction
from oarepo_requests.actions.generic import AutoAcceptSubmitAction

from .generic import OARepoRequestType
from .generic import NonDuplicableOARepoRequestType


class EditRecordRequestType(OARepoRequestType):
class EditRecordRequestType(NonDuplicableOARepoRequestType):
available_actions = {
**RequestType.available_actions,
"submit": AutoAcceptSubmitAction,
"accept": EditTopicAcceptAction,
}
description = _("Request re-opening of published record")
Expand Down
29 changes: 29 additions & 0 deletions oarepo_requests/types/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
from invenio_requests.customizations import RequestType
from invenio_requests.proxies import current_requests_service

from oarepo_requests.errors import OpenRequestAlreadyExists
from oarepo_requests.utils import open_request_exists


class OARepoRequestType(RequestType):
def can_create(self, identity, data, receiver, topic, creator, *args, **kwargs):
Expand All @@ -21,3 +24,29 @@ def can_possibly_create(self, identity, topic, *args, **kwargs):
except PermissionDeniedError:
return False
return True


class NonDuplicableOARepoRequestType(OARepoRequestType):

def can_create(self, identity, data, receiver, topic, creator, *args, **kwargs):
if open_request_exists(topic, self.type_id):
raise OpenRequestAlreadyExists(self, topic)
current_requests_service.require_permission(identity, "create")

@classmethod
def can_possibly_create(self, identity, topic, *args, **kwargs):
"""
used for checking whether there is any situation where the client can create a request of this type
it's different to just using can create with no receiver and data because that checks specifically
for situation without them while this method is used to check whether there is a possible situation
a user might create this request
eg. for the purpose of serializing a link on associated record
"""

if open_request_exists(topic, self.type_id):
return False
try:
current_requests_service.require_permission(identity, "create")
except PermissionDeniedError:
return False
return True
4 changes: 2 additions & 2 deletions oarepo_requests/types/publish_draft.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

from oarepo_requests.actions.publish_draft import PublishDraftAcceptAction

from .generic import OARepoRequestType
from .generic import NonDuplicableOARepoRequestType


class PublishDraftRequestType(OARepoRequestType):
class PublishDraftRequestType(NonDuplicableOARepoRequestType):
available_actions = {
**RequestType.available_actions,
"accept": PublishDraftAcceptAction,
Expand Down
11 changes: 4 additions & 7 deletions oarepo_requests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
from invenio_requests.resolvers.registry import ResolverRegistry
from invenio_search.engine import dsl

from oarepo_requests.errors import OpenRequestAlreadyExists


def allowed_request_types_for_record(record):
request_types = current_request_type_registry._registered_types
Expand Down Expand Up @@ -53,7 +51,7 @@ def _reference_query_term(term, reference):
)


def request_exists(
def search_requests(
identity,
type_id,
topic_reference=None,
Expand Down Expand Up @@ -83,16 +81,15 @@ def request_exists(
must=must,
),
)
return next(results.hits)["id"] if results.total > 0 else None
return results.hits


def open_request_exists(topic_or_reference, type_id):
topic_reference = ResolverRegistry.reference_entity(topic_or_reference, raise_=True)
existing_request = request_exists(
existing_requests = search_requests(
system_identity, type_id, topic_reference=topic_reference, is_open=True
)
if existing_request:
raise OpenRequestAlreadyExists(existing_request, topic_or_reference)
return bool(list(existing_requests))


# TODO these things are related and possibly could be approached in a less convoluted manner? For example, global model->services map would help
Expand Down
1 change: 0 additions & 1 deletion run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ pip install "oarepo[tests]==$OAREPO_VERSION.*"
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
15 changes: 15 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ def ret_data(record_id):
return ret_data


@pytest.fixture()
def edit_record_data_function():
def ret_data(record_id):
return {
"request_type": "thesis_edit_record",
"topic": {"thesis": record_id},
}

return ret_data


@pytest.fixture()
def delete_record_data_function():
def ret_data(record_id):
Expand Down Expand Up @@ -140,10 +151,14 @@ def app_config(app_config):
def default_receiver(*args, **kwargs):
return {"user": "2"}

def none_receiver(*args, **kwargs):
return None

app_config["OAREPO_REQUESTS_DEFAULT_RECEIVER"] = {
"thesis_publish_draft": default_receiver,
"thesis_delete_record": default_receiver,
"thesis_non_duplicable": default_receiver,
"thesis_edit_record": none_receiver,
}

"""
Expand Down
Empty file.
33 changes: 0 additions & 33 deletions tests/mock_requests/mock_requests/type.py

This file was deleted.

10 changes: 0 additions & 10 deletions tests/mock_requests/setup.py

This file was deleted.

Loading

0 comments on commit 837424b

Please sign in to comment.