Skip to content

Commit

Permalink
dev savepoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Krist committed Jan 11, 2024
1 parent 6530247 commit 10acf8b
Show file tree
Hide file tree
Showing 22 changed files with 511 additions and 116 deletions.
16 changes: 7 additions & 9 deletions oarepo_requests/actions/delete_topic.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
from invenio_records_resources.proxies import current_service_registry
from invenio_records_resources.services.uow import RecordDeleteOp
from invenio_requests.customizations import SubmitAction
from invenio_requests.resolvers.registry import ResolverRegistry

from ..utils import get_matching_service
# from .generic import AcceptAction
from invenio_requests.customizations import actions

class DeleteTopicSubmitAction(SubmitAction):

class DeleteTopicAcceptAction(actions.AcceptAction):
def execute(self, identity, uow):
topic = self.request.topic.resolve()
for resolver in ResolverRegistry.get_registered_resolvers():
if resolver.matches_entity(topic):
topic_service = current_service_registry.get(resolver._service_id)
break
else:
topic_service = get_matching_service(topic)
if not topic_service:
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)
Expand Down
13 changes: 8 additions & 5 deletions oarepo_requests/components/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,12 @@ def form_config(self, identity, data=None, record=None, errors=None, **kwargs):


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
self.delete_request_type = delete_request_type

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(
Expand All @@ -87,8 +84,14 @@ def create(self, identity, data=None, record=None, **kwargs):
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))
current_requests_service.execute_action(
identity, request_item["id"], "submit", uow=self.uow
)
setattr(record, "parent", self.uow._operations[2]._record.parent)
# record = self.uow._operations[2]._record
# record = self.uow._operations[1]._record
# 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 Down
20 changes: 20 additions & 0 deletions oarepo_requests/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class OpenRequestAlreadyExists(Exception):
"""An open request already exists."""

def __init__(self, request, record):
self.request = request
self.record = record

@property
def description(self):
"""Exception's description."""
return f"There is already an open request of {self.request.name} on {self.record.id}."

class UnknownRequestType(Exception):
def __init__(self, request_type):
self.request_type = request_type

@property
def description(self):
"""Exception's description."""
return f"Unknown request type {self.request_type}."
7 changes: 6 additions & 1 deletion oarepo_requests/ext.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from invenio_requests.proxies import current_requests_service

from oarepo_requests.resources.config import OARepoRequestsResourceConfig
from oarepo_requests.resources.resource import OARepoRequestsResource
from oarepo_requests.services.create import CreateRequestsService


class OARepoRequests:
Expand All @@ -16,6 +19,8 @@ def init_app(self, app):

def init_resources(self):
"""Init resources."""
self.service = CreateRequestsService(current_requests_service)
self.requests_resource = OARepoRequestsResource(
service=self.service,
config=OARepoRequestsResourceConfig(),
)
)
6 changes: 4 additions & 2 deletions oarepo_requests/resources/config.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from invenio_records_resources.resources.records.config import RecordResourceConfig
from invenio_records_resources.services.base.config import ConfiguratorMixin


class OARepoRequestsResourceConfig(RecordResourceConfig, ConfiguratorMixin):
""""""

blueprint_name = "oarepo-requests"
url_prefix = "/requests"
routes = {
"create": "/create",
}
"list": "/",
}
45 changes: 20 additions & 25 deletions oarepo_requests/resources/resource.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@

from flask import g
from flask_resources import (
Resource,
resource_requestctx,
response_handler,
route,
)

from flask_resources import Resource, resource_requestctx, response_handler, route
from invenio_records_resources.resources.errors import ErrorHandlersMixin
from invenio_records_resources.resources.records.resource import (
request_data,
request_extra_args,
request_search_args,
request_view_args, request_headers, request_data,
request_headers,
request_view_args,
)
from invenio_requests import current_requests_service, current_request_type_registry
class OARepoRequestsResource(Resource, ErrorHandlersMixin):
from invenio_requests import current_request_type_registry


def __init__(self, config, service=current_requests_service):
class OARepoRequestsResource(Resource, ErrorHandlersMixin):
def __init__(self, config, service):
"""Constructor."""
super().__init__(config)
self.service = service
Expand All @@ -25,36 +20,36 @@ def create_url_rules(self):
"""Create the URL rules for the record resource."""
routes = self.config.routes
url_rules = [
route("POST", routes["create"], self.create),
route("POST", routes["list"], self.create),
]
return url_rules


@request_extra_args
@request_view_args
@request_headers
@request_data
@response_handler()
def create(self):
request_type_str = resource_requestctx.data.get("request_type", None)
type_ = current_request_type_registry.lookup(
request_type_str, quiet=True
)
def stringify_first_val(dct):
if isinstance(dct,dict):
if isinstance(dct, dict):
for k, v in dct.items():
dct[k] = str(v)
return dct

items = self.service.create(
identity=g.identity,
data=resource_requestctx.data,
request_type=type_,
receiver=stringify_first_val(resource_requestctx.data.get("receiver", None)) if resource_requestctx.data else None,
creator=stringify_first_val(resource_requestctx.data.get("creator", None)) if resource_requestctx.data else None,
topic=stringify_first_val(resource_requestctx.data.get("topic", None)) if resource_requestctx.data else None,
request_type=resource_requestctx.data.get("request_type", None),
receiver=stringify_first_val(resource_requestctx.data.get("receiver", None))
if resource_requestctx.data
else None,
creator=stringify_first_val(resource_requestctx.data.get("creator", None))
if resource_requestctx.data
else None,
topic=stringify_first_val(resource_requestctx.data.get("topic", None))
if resource_requestctx.data
else None,
expand=resource_requestctx.args.get("expand", False),
)

return items.to_dict(), 201

40 changes: 40 additions & 0 deletions oarepo_requests/services/create.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from invenio_records_resources.services.uow import unit_of_work

from oarepo_requests.errors import UnknownRequestType
from invenio_requests.proxies import current_request_type_registry


class CreateRequestsService:
def __init__(self, requests_service):
self.requests_service = requests_service

@unit_of_work()
def create(
self,
identity,
data,
request_type,
receiver,
creator=None,
topic=None,
expires_at=None,
uow=None,
expand=False,
):
type_ = current_request_type_registry.lookup(request_type, quiet=True)
if not type_:
raise UnknownRequestType(request_type)
if hasattr(type_, "can_create"):
error = type_.can_create(identity, data, receiver, topic, creator)
else:
error = None
if not error:
return self.requests_service.create(
identity=identity,
data=data,
request_type=type_,
receiver=receiver,
creator=creator,
topic=topic,
expand=expand,
)
12 changes: 0 additions & 12 deletions oarepo_requests/types/community_submission.py

This file was deleted.

12 changes: 9 additions & 3 deletions oarepo_requests/types/delete_record.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
from invenio_requests.customizations import RequestType
from invenio_requests.proxies import current_requests_service

from oarepo_requests.actions.delete_topic import DeleteTopicSubmitAction
from oarepo_requests.actions.delete_topic import DeleteTopicAcceptAction
from oarepo_requests.errors import OpenRequestAlreadyExists
from .generic import OARepoRequestType
from oarepo_requests.utils import open_request_exists, resolve_reference_dict
from invenio_records_resources.services.errors import PermissionDeniedError


class DeleteRecordRequestType(RequestType):
class DeleteRecordRequestType(OARepoRequestType):
available_actions = {
**RequestType.available_actions,
"submit": DeleteTopicSubmitAction,
"accept": DeleteTopicAcceptAction,
}

receiver_can_be_none = True

14 changes: 13 additions & 1 deletion oarepo_requests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,19 @@ def allowed_request_types_for_record_cls(queryied_record_cls):
break
return ret


"""
{'minimum_should_match': '0<1',
'filter': [Bool(minimum_should_match=1,
must=[QueryString(query='swf1h-psj10')],
should=[Bool
(must=[Terms(status=['created']),
Terms(grants=['created_by.system_role.any_user',
'created_by.community.a26e6dfa-86ef-4a3b-9cbb-d6f034533a45.owner',
'created_by.id.1', 'created_by.system_role.authenticated_user'])])
,
Bool(minimum_should_match=1, must=[Terms(status=['submitted', 'deleted', 'cancelled', 'expired', 'accepted', 'declined'])],
should=[Terms(grants=['created_by.system_role.any_user', 'created_by.community.a26e6dfa-86ef-4a3b-9cbb-d6f034533a45.owner', 'created_by.id.1', 'created_by.system_role.authenticated_user']), Terms(grants=['receiver.system_role.any_user', 'receiver.community.a26e6dfa-86ef-4a3b-9cbb-d6f034533a45.owner', 'receiver.id.1', 'receiver.system_role.authenticated_user'])])])], 'should': []}
"""
def request_exists(
identity,
topic,
Expand Down
2 changes: 1 addition & 1 deletion oarepo_requests/views/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def create_oarepo_requests(app):
"""Create requests blueprint."""
ext = app.extensions["oarepo-requests"]
return ext.requests_resource.as_blueprint()
return ext.requests_resource.as_blueprint()
6 changes: 2 additions & 4 deletions oarepo_requests/views/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@


def create_app_blueprint(app):
blueprint = Blueprint(
"oarepo_requests_app", __name__, url_prefix="/requests"
)
return blueprint
blueprint = Blueprint("oarepo_requests_app", __name__, url_prefix="/requests")
return blueprint
3 changes: 3 additions & 0 deletions run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ python3 -m venv $BUILDER_VENV
. $BUILDER_VENV/bin/activate
pip install -U setuptools pip wheel
pip install -U oarepo-model-builder-tests oarepo-model-builder-requests oarepo-model-builder-drafts
editable_install /home/ron/prace/oarepo-model-builder-new
editable_install /home/ron/prace/oarepo-model-builder-requests

if test -d $BUILD_TEST_DIR/$MODEL; then
rm -rf $MODEL
Expand All @@ -37,6 +39,7 @@ pip install "oarepo>=$OAREPO_VERSION,<$OAREPO_VERSION_MAX"
pip install "./$BUILD_TEST_DIR/$MODEL[tests]"
pip install .
pip install oarepo-ui
editable_install /home/ron/prace/oarepo-runtime

pytest $BUILD_TEST_DIR/test_requests
pytest $BUILD_TEST_DIR/test_ui
Loading

0 comments on commit 10acf8b

Please sign in to comment.