Skip to content

Commit

Permalink
Merge pull request #29 from oarepo/generic-topics-receivers-tests
Browse files Browse the repository at this point in the history
Generic topics receivers tests
  • Loading branch information
mesemus authored Jun 14, 2024
2 parents 1bbed76 + 8e1a01c commit faef957
Show file tree
Hide file tree
Showing 29 changed files with 252 additions and 225 deletions.
3 changes: 2 additions & 1 deletion oarepo_requests/actions/publish_draft.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from ..utils import get_matching_service_for_record
from invenio_requests.customizations import actions

from ..utils import get_matching_service_for_record


def publish_draft(draft, identity, uow, *args, **kwargs):
topic_service = get_matching_service_for_record(draft)
Expand Down
21 changes: 13 additions & 8 deletions oarepo_requests/config.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
from oarepo_requests.resolvers.ui import UserEntityReferenceUIResolver, FallbackEntityReferenceUIResolver, \
GroupEntityReferenceUIResolver
from oarepo_requests.types import DeletePublishedRecordRequestType, EditPublishedRecordRequestType, PublishDraftRequestType
from invenio_users_resources.entity_resolvers import UserResolver, GroupResolver
from invenio_users_resources.entity_resolvers import GroupResolver, UserResolver

from oarepo_requests.resolvers.ui import (
FallbackEntityReferenceUIResolver,
GroupEntityReferenceUIResolver,
UserEntityReferenceUIResolver,
)
from oarepo_requests.types import (
DeletePublishedRecordRequestType,
EditPublishedRecordRequestType,
PublishDraftRequestType,
)

REQUESTS_REGISTERED_TYPES = [
DeletePublishedRecordRequestType(),
EditPublishedRecordRequestType(),
PublishDraftRequestType(),
]

REQUESTS_ALLOWED_RECEIVERS = [
"user", "role"
]
REQUESTS_ALLOWED_RECEIVERS = ["user", "group"]

REQUESTS_ENTITY_RESOLVERS = [
UserResolver(),
Expand All @@ -25,4 +30,4 @@
"group": GroupEntityReferenceUIResolver("group"),
}

REQUESTS_UI_SERIALIZATION_REFERENCED_FIELDS = ["created_by", "receiver", "topic"]
REQUESTS_UI_SERIALIZATION_REFERENCED_FIELDS = ["created_by", "receiver", "topic"]
29 changes: 24 additions & 5 deletions oarepo_requests/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,23 @@ def default_request_receiver(self, identity, request_type, topic, creator, data)
# otherwise use the default receiver
return obj_or_import_string(
self.app.config["OAREPO_REQUESTS_DEFAULT_RECEIVER"]
)(identity=identity, request_type=request_type, topic=topic, creator=creator, data=data)
)(
identity=identity,
request_type=request_type,
topic=topic,
creator=creator,
data=data,
)

@cached_property
def allowed_topic_ref_types(self):
entity_resolvers = self.app.config.get("REQUESTS_ENTITY_RESOLVERS", [])
return {x.type_key for x in entity_resolvers}

@cached_property
def requests_entity_resolvers(self):
return self.app.config.get("REQUESTS_ENTITY_RESOLVERS", [])

@property
def allowed_receiver_ref_types(self):
return self.app.config.get("REQUESTS_ALLOWED_RECEIVERS", [])
Expand Down Expand Up @@ -80,10 +90,19 @@ def init_config(self, app):
"""Initialize configuration."""

from . import config
app.config.setdefault("REQUESTS_REGISTERED_TYPES", []).extend(config.REQUESTS_REGISTERED_TYPES)
app.config.setdefault("REQUESTS_ALLOWED_RECEIVERS", []).extend(config.REQUESTS_ALLOWED_RECEIVERS)
app.config.setdefault("REQUESTS_ENTITY_RESOLVERS", []).extend(config.REQUESTS_ENTITY_RESOLVERS)
app.config.setdefault("ENTITY_REFERENCE_UI_RESOLVERS", {}).update(config.ENTITY_REFERENCE_UI_RESOLVERS)

app.config.setdefault("REQUESTS_REGISTERED_TYPES", []).extend(
config.REQUESTS_REGISTERED_TYPES
)
app.config.setdefault("REQUESTS_ALLOWED_RECEIVERS", []).extend(
config.REQUESTS_ALLOWED_RECEIVERS
)
app.config.setdefault("REQUESTS_ENTITY_RESOLVERS", []).extend(
config.REQUESTS_ENTITY_RESOLVERS
)
app.config.setdefault("ENTITY_REFERENCE_UI_RESOLVERS", {}).update(
config.ENTITY_REFERENCE_UI_RESOLVERS
)
app.config.setdefault("REQUESTS_UI_SERIALIZATION_REFERENCED_FIELDS", []).extend(
config.REQUESTS_UI_SERIALIZATION_REFERENCED_FIELDS
)
6 changes: 3 additions & 3 deletions oarepo_requests/invenio_patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def apply(self, identity, search, params):
class RequestReceiverFilterParam(FilterParam):
def apply(self, identity, search, params):
value = params.pop(self.param_name, None)
my_groups = [n.value for n in identity.provides if n.method == "role"]
my_roles = [n.value for n in identity.provides if n.method == "role"]
if value is not None:
search = search.filter(
Bool(
Expand All @@ -37,8 +37,8 @@ def apply(self, identity, search, params):
Term(**{f"{self.field_name}.user": identity.id}),
# my roles
*[
Term(**{f"{self.field_name}.group": group_id})
for group_id in my_groups
Term(**{f"{self.field_name}.group": role_id})
for role_id in my_roles
],
# TODO: add my communities where I have a role to accept requests
],
Expand Down
34 changes: 19 additions & 15 deletions oarepo_requests/services/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

class RequestTypesComponent(ResultsComponent):
def update_data(self, identity, record, projection, expand):
if not expand:
return
request_types_list = []
allowed_request_types = allowed_request_types_for_record(record)
for request_name, request_type in allowed_request_types.items():
Expand All @@ -28,22 +30,24 @@ def update_data(self, identity, record, projection, expand):
)
request_type_link = data
request_types_list.append(request_type_link)
projection["request_types"] = request_types_list
projection["expanded"]["request_types"] = request_types_list


class RequestsComponent(ResultsComponent):
def update_data(self, identity, record, projection, expand):
if expand:
service = get_requests_service_for_records_service(
get_matching_service_for_record(record)
)
reader = (
service.search_requests_for_draft
if getattr(record, "is_draft", False)
else service.search_requests_for_record
)
try:
requests = list(reader(identity, record["id"]).hits)
except PermissionDeniedError:
requests = []
projection["requests"] = requests
if not expand:
return

service = get_requests_service_for_records_service(
get_matching_service_for_record(record)
)
reader = (
service.search_requests_for_draft
if getattr(record, "is_draft", False)
else service.search_requests_for_record
)
try:
requests = list(reader(identity, record["id"]).hits)
except PermissionDeniedError:
requests = []
projection["expanded"]["requests"] = requests
20 changes: 16 additions & 4 deletions oarepo_requests/services/ui_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from oarepo_requests.resolvers.ui import resolve
from oarepo_requests.services.schema import (
NoneReceiverGenericRequestSchema,
RequestsSchemaMixin,
RequestTypeSchema,
get_links_schema,
)
Expand Down Expand Up @@ -98,9 +97,22 @@ def add_type_details(self, data, **kwargs):
return data


class UIRequestsSerializationMixin(RequestsSchemaMixin):
requests = ma.fields.List(ma.fields.Nested(UIBaseRequestSchema))
request_types = ma.fields.List(ma.fields.Nested(UIRequestTypeSchema))
class UIRequestsSerializationMixin(ma.Schema):

@ma.post_dump()
def add_request_types(self, data, **kwargs):
expanded = data.get("expanded", {})
if not expanded:
return data
if "request_types" in expanded:
expanded["request_types"] = UIRequestTypeSchema(context=self.context).dump(
expanded["request_types"], many=True
)
if "requests" in expanded:
expanded["requests"] = UIBaseRequestSchema(context=self.context).dump(
expanded["requests"], many=True
)
return data


class UIBaseRequestEventSchema(BaseRecordSchema):
Expand Down
18 changes: 9 additions & 9 deletions oarepo_requests/types/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from .ref_types import ModelRefTypes, ReceiverRefTypes
from .delete_record import DeletePublishedRecordRequestType
from .edit_record import EditPublishedRecordRequestType
from .publish_draft import PublishDraftRequestType
from .generic import NonDuplicableOARepoRequestType
from .publish_draft import PublishDraftRequestType
from .ref_types import ModelRefTypes, ReceiverRefTypes

__all__ = [
'ModelRefTypes',
'ReceiverRefTypes',
'DeletePublishedRecordRequestType',
'EditPublishedRecordRequestType',
'PublishDraftRequestType',
'NonDuplicableOARepoRequestType',
]
"ModelRefTypes",
"ReceiverRefTypes",
"DeletePublishedRecordRequestType",
"EditPublishedRecordRequestType",
"PublishDraftRequestType",
"NonDuplicableOARepoRequestType",
]
3 changes: 3 additions & 0 deletions oarepo_requests/types/delete_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@

from oarepo_requests.actions.delete_topic import DeleteTopicAcceptAction

from . import ModelRefTypes
from .generic import NonDuplicableOARepoRequestType


class DeletePublishedRecordRequestType(NonDuplicableOARepoRequestType):
type_id = "delete-published-record"
name = _("Delete record")

available_actions = {
**RequestType.available_actions,
"accept": DeleteTopicAcceptAction,
}
description = _("Request deletion of published record")
receiver_can_be_none = True
allowed_topic_ref_types = ModelRefTypes(published=True, draft=False)
3 changes: 3 additions & 0 deletions oarepo_requests/types/edit_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
from oarepo_requests.actions.edit_topic import EditTopicAcceptAction
from oarepo_requests.actions.generic import AutoAcceptSubmitAction

from . import ModelRefTypes
from .generic import NonDuplicableOARepoRequestType


class EditPublishedRecordRequestType(NonDuplicableOARepoRequestType):
type_id = "edit-published-record"
name = _("Edit record")

available_actions = {
**RequestType.available_actions,
Expand All @@ -17,3 +19,4 @@ class EditPublishedRecordRequestType(NonDuplicableOARepoRequestType):
}
description = _("Request re-opening of published record")
receiver_can_be_none = True
allowed_topic_ref_types = ModelRefTypes(published=True, draft=False)
3 changes: 3 additions & 0 deletions oarepo_requests/types/publish_draft.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@

from oarepo_requests.actions.publish_draft import PublishDraftAcceptAction

from . import ModelRefTypes
from .generic import NonDuplicableOARepoRequestType


class PublishDraftRequestType(NonDuplicableOARepoRequestType):
type_id = "publish-draft"
name = _("Publish draft")

available_actions = {
**RequestType.available_actions,
"accept": PublishDraftAcceptAction,
}
description = _("Request publishing of a draft")
receiver_can_be_none = True
allowed_topic_ref_types = ModelRefTypes(published=False, draft=True)
16 changes: 15 additions & 1 deletion oarepo_requests/types/ref_types.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from invenio_records_resources.references import RecordResolver

from oarepo_requests.proxies import current_oarepo_requests


Expand All @@ -7,9 +9,21 @@ class ModelRefTypes:
The list of ref types is taken from the configuration (configuration key REQUESTS_ALLOWED_TOPICS).
"""

def __init__(self, published=False, draft=False):
self.published = published
self.draft = draft

def __get__(self, obj, owner):
"""Property getter, returns the list of allowed reference types."""
return current_oarepo_requests.allowed_topic_ref_types
ret = []
for ref_type in current_oarepo_requests.requests_entity_resolvers:
if not isinstance(ref_type, RecordResolver):
continue
if self.published and not ref_type.record_cls.is_draft:
ret.append(ref_type.type_key)
elif self.draft and ref_type.record_cls.is_draft:
ret.append(ref_type.type_key)
return ret


class ReceiverRefTypes:
Expand Down
Empty file.
59 changes: 0 additions & 59 deletions oarepo_requests/ui/resources/components.py

This file was deleted.

1 change: 1 addition & 0 deletions run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pip install -U setuptools pip wheel
pip install "oarepo[tests]==$OAREPO_VERSION.*"
pip install -e "./$BUILD_TEST_DIR/${MODEL}"
pip install oarepo-ui
pip install deepdiff
pip install -e .

# local override
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-requests
version = 1.1.19
version = 1.2.0
description =
authors = Ronald Krist <[email protected]>
readme = README.md
Expand Down
Loading

0 comments on commit faef957

Please sign in to comment.