Skip to content

Commit

Permalink
Merge pull request #1 from oarepo/krist/be-95-vyzkouseni-communities
Browse files Browse the repository at this point in the history
Krist/be 95 vyzkouseni communities
  • Loading branch information
SilvyPuzzlewell authored Aug 30, 2023
2 parents f54f68e + a558e25 commit 5428617
Show file tree
Hide file tree
Showing 19 changed files with 1,743 additions and 95 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ on: [ push ]
jobs:
build:
runs-on: ubuntu-latest
services:
redis:
# Docker Hub image
image: redis
# Set health checks to wait until redis has started
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 6379:6379
strategy:
matrix:
python-version: [ "3.9", "3.10" ]
Expand Down
6 changes: 3 additions & 3 deletions oarepo_requests/actions/delete_topic.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from invenio_records_resources.proxies import current_service_registry
from invenio_records_resources.services.uow import RecordCommitOp, RecordDeleteOp
from invenio_records_resources.services.uow import RecordDeleteOp
from invenio_requests.customizations import SubmitAction
from invenio_requests.resolvers.registry import ResolverRegistry

Expand All @@ -14,5 +14,5 @@ def execute(self, identity, uow):
else:
raise KeyError(f"topic {topic} service not found")
uow.register(RecordDeleteOp(topic, topic_service.indexer, index_refresh=True))
#topic_service.delete(identity, id_, revision_id=None, uow=None)
super().execute(identity, uow)
# topic_service.delete(identity, id_, revision_id=None, uow=None)
super().execute(identity, uow)
13 changes: 6 additions & 7 deletions oarepo_requests/actions/publish_draft.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
from invenio_records_resources.proxies import current_service_registry
from invenio_records_resources.services.uow import RecordCommitOp
from invenio_requests import current_request_type_registry, current_requests_service
from invenio_requests.customizations import SubmitAction, AcceptAction
from invenio_requests.customizations import SubmitAction
from invenio_requests.resolvers.registry import ResolverRegistry

from oarepo_requests.types.delete_record import DeleteRecordRequestType
from oarepo_requests.utils.utils import get_allowed_request_types

"""
def create_delete_request(topic_service, topic_id, identity):
record = topic_service.record_cls.pid.resolve(topic_id)
allowed_types = get_allowed_request_types(type(record))
type_ = next(x for x in allowed_types.values() if issubclass(x, DeleteRecordRequestType))
current_requests_service.create(identity=identity, data={}, request_type=type_, receiver=None, topic=record)
"""


def publish_draft(draft, identity, uow):
for resolver in ResolverRegistry.get_registered_resolvers():
if resolver.matches_entity(draft):
Expand All @@ -26,17 +24,18 @@ def publish_draft(draft, identity, uow):
topic_service.publish(identity, id_, uow=uow, expand=False)



class PublishDraftSubmitAction(SubmitAction):
def execute(self, identity, uow):
topic = self.request.topic.resolve()
setattr(topic.parent, self.request.type.type_id, None)
uow.register(RecordCommitOp(topic.parent))
publish_draft(topic, identity, uow)
super().execute(identity, uow)


"""
class PublishDraftAcceptAction(AcceptAction):
def execute(self, identity, uow):
publish_draft(self, identity, uow)
super().execute(identity, uow)
"""
"""
8 changes: 5 additions & 3 deletions oarepo_requests/components/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from .requests import AllowedRequestsComponent, PublishDraftComponent, OAICreateRequestsComponent
from .requests import (
AllowedRequestsComponent,
OAICreateRequestsComponent,
PublishDraftComponent,
)

__all__ = [
"AllowedRequestsComponent",
"PublishDraftComponent",
"OAICreateRequestsComponent",
]


83 changes: 57 additions & 26 deletions oarepo_requests/components/requests.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,47 @@
import functools

from invenio_records_resources.services.uow import RecordCommitOp
from invenio_requests.customizations import LogEventType

from oarepo_requests.utils.utils import get_allowed_request_types
from invenio_records_resources.services.records.components import ServiceComponent
from invenio_requests.proxies import current_requests_service, current_request_type_registry, current_events_service
from invenio_records_resources.services.errors import PermissionDeniedError
from invenio_records_resources.services.records.components import ServiceComponent
from invenio_records_resources.services.uow import RecordCommitOp
from invenio_requests.customizations import LogEventType, RequestActions
from invenio_requests.proxies import (
current_events_service,
current_request_type_registry,
current_requests_service,
)


class AllowedRequestsComponent(ServiceComponent):
"""Service component which sets all data in the record."""

def before_ui_detail(self, identity, data=None, record=None, errors=None, **kwargs):
# todo discriminate requests from other stuff which can be on parent in the future
# todo what to throw if parent doesn't exist
requests = record["parent"]
requests.pop("id")
ret = {}
for request_name, request in requests.items():
# todo what about other types of actions?
# request_type = current_request_type_registry.lookup(request["type"])
try:
current_requests_service.require_permission(identity, f"action_submit_{request['type']}")
except PermissionDeniedError:
continue
ret[request_name] = request
available_requests = {}

for request_name, request_dict in requests.items():
request = current_requests_service.record_cls.get_record(request_dict["id"])
request_type = current_request_type_registry.lookup(request_dict["type"])
for action_name, action in request_type.available_actions.items():
try:
current_requests_service.require_permission(
identity, f"action_{action_name}", request=request
)
except PermissionDeniedError:
continue
action = RequestActions.get_action(request, action_name)
if not action.can_execute():
continue
available_requests[request_name] = request_dict
extra_context = kwargs["extra_context"]
extra_context["allowed_requests"] = ret
extra_context["allowed_requests"] = available_requests


class PublishDraftComponentPrivate(ServiceComponent):
"""Service component for request integration."""

def __init__(self, publish_request_type, delete_request_type, *args, **kwargs):
super().__init__(*args, **kwargs)
self.publish_request_type = publish_request_type
Expand All @@ -40,10 +51,15 @@ def create(self, identity, data=None, record=None, **kwargs):
"""Create the review if requested."""
# topic and request_type in kwargs
if self.publish_request_type:
type_ = current_request_type_registry.lookup(self.publish_request_type, quiet=True)
request_item = current_requests_service.create(identity, {}, type_, receiver=None, topic=record, uow=self.uow)
type_ = current_request_type_registry.lookup(
self.publish_request_type, quiet=True
)
request_item = current_requests_service.create(
identity, {}, type_, receiver=None, topic=record, uow=self.uow
)
setattr(record.parent, self.publish_request_type, request_item._request)
self.uow.register(RecordCommitOp(record.parent))

def publish(self, identity, data=None, record=None, **kwargs):
publish_request = getattr(record.parent, self.publish_request_type)

Expand All @@ -52,34 +68,49 @@ def publish(self, identity, data=None, record=None, **kwargs):
request_status = "accepted"
request.status = request_status
setattr(record.parent, self.publish_request_type, None)
event = LogEventType(payload={"event": request_status, "content": "request was published through direct call without request"})
event = LogEventType(
payload={
"event": request_status,
"content": "record was published through direct call without request",
}
)
_data = dict(payload=event.payload)
current_events_service.create(
identity, request.id, _data, event, uow=self.uow
)

if self.delete_request_type:
type_ = current_request_type_registry.lookup(self.delete_request_type, quiet=True)
request_item = current_requests_service.create(identity, {}, type_, receiver=None, topic=record, uow=self.uow)
type_ = current_request_type_registry.lookup(
self.delete_request_type, quiet=True
)
request_item = current_requests_service.create(
identity, {}, type_, receiver=None, topic=record, uow=self.uow
)
setattr(record.parent, self.delete_request_type, request_item._request)
self.uow.register(RecordCommitOp(record.parent))



class OAICreateRequestsComponentPrivate(ServiceComponent):
def __init__(self, delete_request_type, *args, **kwargs):
super().__init__(*args, **kwargs)
self.delete_request_type = delete_request_type

def create(self, identity, data=None, record=None, **kwargs):
type_ = current_request_type_registry.lookup(self.delete_request_type, quiet=True)
request_item = current_requests_service.create(identity, {}, type_, receiver=None, topic=record, uow=self.uow)
type_ = current_request_type_registry.lookup(
self.delete_request_type, quiet=True
)
request_item = current_requests_service.create(
identity, {}, type_, receiver=None, topic=record, uow=self.uow
)
setattr(record.parent, self.delete_request_type, request_item._request)
self.uow.register(RecordCommitOp(record.parent))


def PublishDraftComponent(publish_request_type, delete_request_type):
return functools.partial(PublishDraftComponentPrivate, publish_request_type, delete_request_type)
return functools.partial(
PublishDraftComponentPrivate, publish_request_type, delete_request_type
)


def OAICreateRequestsComponent(delete_request_type):
return functools.partial(OAICreateRequestsComponentPrivate, delete_request_type)

3 changes: 2 additions & 1 deletion oarepo_requests/schemas/marshmallow.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from invenio_requests.services.schemas import GenericRequestSchema
from marshmallow import fields


class NoneReceiverGenericRequestSchema(GenericRequestSchema):
receiver = fields.Dict(allow_none=True)
receiver = fields.Dict(allow_none=True)
3 changes: 1 addition & 2 deletions oarepo_requests/types/delete_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@


class DeleteRecordRequestType(RequestType):

available_actions = {
**RequestType.available_actions,
"submit": DeleteTopicSubmitAction,
}

receiver_can_be_none = True
receiver_can_be_none = True
1 change: 0 additions & 1 deletion oarepo_requests/types/publish_draft.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


class PublishDraftRequestType(RequestType):

available_actions = {
**RequestType.available_actions,
"submit": PublishDraftSubmitAction,
Expand Down
11 changes: 8 additions & 3 deletions oarepo_requests/utils/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
from invenio_requests.resolvers.registry import ResolverRegistry
from invenio_requests.proxies import current_request_type_registry
from invenio_requests.resolvers.registry import ResolverRegistry


def get_allowed_request_types(queryied_record_cls):
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")}
type_key2record_cls = {
resolver.type_key: resolver.record_cls
for resolver in resolvers
if hasattr(resolver, "type_key")
}
ret = {}
for request_name, request_type in request_types.items():
allowed_type_keys = set(request_type.allowed_topic_ref_types)
Expand All @@ -16,4 +21,4 @@ def get_allowed_request_types(queryied_record_cls):
if record_cls == queryied_record_cls:
ret[request_name] = request_type
break
return ret
return ret
4 changes: 3 additions & 1 deletion run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ python3 -m venv $MODEL_VENV
pip install -U setuptools pip wheel
pip install "./$BUILD_TEST_DIR/$MODEL[tests]"
pip install .
pip install oarepo-ui

pytest $BUILD_TEST_DIR/test_requests
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.0.0
version = 1.0.1
description =
authors = Ronald Krist <[email protected]>
readme = README.md
Expand Down
Loading

0 comments on commit 5428617

Please sign in to comment.