Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Krist committed Jan 31, 2024
1 parent 70b4ac9 commit d1d8ab7
Show file tree
Hide file tree
Showing 18 changed files with 175 additions and 105 deletions.
2 changes: 0 additions & 2 deletions oarepo_requests/components/requests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


"""
class RecordCommunitiesAction(CommunityRoles):
Expand Down
1 change: 0 additions & 1 deletion oarepo_requests/ext.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

from oarepo_requests.resources.config import OARepoRequestsResourceConfig
from oarepo_requests.resources.resource import OARepoRequestsResource
from oarepo_requests.services.config import OARepoRequestsServiceConfig
Expand Down
11 changes: 5 additions & 6 deletions oarepo_requests/resolvers/ui.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from invenio_users_resources.proxies import current_users_service
from invenio_records_resources.resources.errors import PermissionDeniedError
from invenio_users_resources.proxies import current_users_service

from oarepo_requests.utils import get_matching_service_for_refdict


def fallback_result(reference):
id = list(reference.values())[0]
return {
"reference": reference,
"type": "user",
"label": f"id: {id}"
}
return {"reference": reference, "type": "user", "label": f"id: {id}"}


def user_entity_reference_ui_resolver(identity, data):
reference = data["reference"]
Expand Down
2 changes: 1 addition & 1 deletion oarepo_requests/resources/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from flask_resources import ResponseHandler
from invenio_records_resources.resources.records.config import RecordResourceConfig
from invenio_requests.resources.requests.config import RequestsResourceConfig
from invenio_records_resources.services.base.config import ConfiguratorMixin
from invenio_requests.resources.requests.config import RequestsResourceConfig

from .ui import OARepoRequestsUIJSONSerializer

Expand Down
1 change: 1 addition & 0 deletions oarepo_requests/resources/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def search_requests_for_draft(self):
)
return hits.to_dict(), 200


class OARepoRequestsResource(RequestsResource, ErrorHandlersMixin):
"""
def __init__(self, config, service):
Expand Down
4 changes: 1 addition & 3 deletions oarepo_requests/resources/ui.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from flask import g
from flask_resources import BaseListSchema
from flask_resources.serializers import JSONSerializer
from invenio_requests.proxies import current_request_type_registry
from oarepo_runtime.i18n import get_locale
from oarepo_runtime.resources import LocalizedUIJSONSerializer

from ..services.ui_schema import UIBaseRequestSchema, get_request_ui_schema
from ..services.ui_schema import UIBaseRequestSchema


class OARepoRequestsUIJSONSerializer(LocalizedUIJSONSerializer):
Expand Down
7 changes: 5 additions & 2 deletions oarepo_requests/services/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from oarepo_requests.services.ui_schema import UIRequestTypeSchema
from oarepo_requests.utils import (
allowed_request_types_for_record_cls,
get_matching_service_for_record, get_requests_service_for_records_service,
get_matching_service_for_record,
get_requests_service_for_records_service,
)


Expand Down Expand Up @@ -45,7 +46,9 @@ def update_data(self, identity, record, projection):

class RequestsComponent:
def update_data(self, identity, record, projection):
service = get_requests_service_for_records_service(get_matching_service_for_record(record))
service = get_requests_service_for_records_service(
get_matching_service_for_record(record)
)
if hasattr(record, "is_draft") and record.is_draft:
requests = list(
service.search_requests_for_draft(identity, record["id"]).hits
Expand Down
6 changes: 3 additions & 3 deletions oarepo_requests/services/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
class RequestTypeSchema(ma.Schema):
type = ma.fields.String()
links = Links()


class NoneReceiverGenericRequestSchema(GenericRequestSchema):
receiver = fields.Dict(allow_none=True)


class RequestsSchemaMixin:
requests = ma.fields.List(
ma.fields.Nested(NoneReceiverGenericRequestSchema)
) # TODO consider what happens due to different request types having their own schema; should the projection be universal here?
request_types = ma.fields.List(ma.fields.Nested(RequestTypeSchema))



10 changes: 4 additions & 6 deletions oarepo_requests/services/service.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from invenio_records_resources.services.base.service import Service
from invenio_records_resources.services.uow import unit_of_work
from invenio_requests.proxies import (
current_request_type_registry,
)
from invenio_requests.proxies import current_request_type_registry
from invenio_requests.services.requests.service import RequestsService
from invenio_search.engine import dsl
from invenio_records_resources.services.base.service import Service

from oarepo_requests.errors import UnknownRequestType
from oarepo_requests.proxies import current_oarepo_requests
Expand Down Expand Up @@ -66,6 +64,7 @@ class DraftRecordRequestsService(RecordRequestsService):
def draft_cls(self):
"""Factory for creating a record class."""
return self.config.draft_cls

# from invenio_rdm_records.services.requests.service.RecordRequestsService
def search_requests_for_draft(
self,
Expand Down Expand Up @@ -138,7 +137,6 @@ def create(
uow=uow,
)


def read(self, identity, id_, expand=False):
api_request = super().read(identity, id_, expand)
return api_request
return api_request
13 changes: 9 additions & 4 deletions oarepo_requests/services/ui_schema.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import marshmallow as ma
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_requests.proxies import current_oarepo_requests
from oarepo_requests.resolvers.ui import fallback_entity_reference_ui_resolver
from oarepo_requests.services.schema import RequestsSchemaMixin, RequestTypeSchema, NoneReceiverGenericRequestSchema
from oarepo_runtime.i18n import lazy_gettext as _
from oarepo_requests.services.schema import (
NoneReceiverGenericRequestSchema,
RequestsSchemaMixin,
RequestTypeSchema,
)


class UIReferenceSchema(ma.Schema):
Expand Down Expand Up @@ -50,7 +54,6 @@ class UIRequestSchemaMixin:

status_code = ma.fields.String()


@ma.pre_dump
def add_type_details(self, data, **kwargs):
type = data["type"]
Expand All @@ -73,7 +76,9 @@ class UIBaseRequestSchema(UIRequestSchemaMixin, NoneReceiverGenericRequestSchema


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


class UIRequestTypeSchema(RequestTypeSchema):
Expand Down
3 changes: 3 additions & 0 deletions oarepo_requests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ def get_type_id_for_record_cls(record_cls):
return resolver.type_id
return None


from invenio_records_resources.proxies import current_service_registry


def get_requests_service_for_records_service(records_service):
return current_service_registry.get(f"{records_service.config.service_id}_requests")
17 changes: 11 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
from invenio_requests.customizations import CommentEventType, LogEventType
from invenio_requests.proxies import current_request_type_registry, current_requests
from invenio_requests.records.api import Request, RequestEventFormat
from thesis.records.api import ThesisRecord, ThesisDraft
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):
"""Application factory fixture."""
Expand Down Expand Up @@ -83,7 +84,11 @@ def ui_serialization_result():
def _result(topic_id, request_id):
return {
# 'created': '2024-01-26T10:06:17.945916',
"created_by": {'label': 'id: 1', 'reference': {'user': '1'}, 'type': 'user'},
"created_by": {
"label": "id: 1",
"reference": {"user": "1"},
"type": "user",
},
"description": "request publishing of a draft",
"expires_at": None,
"id": request_id,
Expand Down Expand Up @@ -232,7 +237,6 @@ def _submit_request(identity, data, **kwargs):

@pytest.fixture()
def users(app, db):

with db.session.begin_nested():
datastore = app.extensions["security"].datastore
user1 = datastore.create_user(
Expand Down Expand Up @@ -294,13 +298,15 @@ def log_user(user_email):
return client

return log_user


@pytest.fixture()
def logged_client_post(client_logged_as):
def _logged_client_post(user, method, *args, **kwargs):
applied_client = client_logged_as(user.email)
return getattr(applied_client, method)(*args, **kwargs)
return _logged_client_post

return _logged_client_post


@pytest.fixture()
Expand Down Expand Up @@ -340,6 +346,7 @@ def example_topic_draft(record_service, identity_simple):
draft = record_service.create(identity_simple, {})
return draft._obj


@pytest.fixture()
def example_draft_service_bypass(app, db):
record = ThesisDraft.create({})
Expand All @@ -366,8 +373,6 @@ def example_topic(record_service, identity_simple):
return record




@pytest.fixture(scope="module")
def identity_creator(identity_simple): # for readability
return identity_simple
Expand Down
70 changes: 45 additions & 25 deletions tests/test_requests/test_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from .utils import link_api2testclient



def test_delete(
logged_client_post,
record_factory,
Expand All @@ -22,11 +21,16 @@ def test_delete(
lst = logged_client_post(creator, "get", urls["BASE_URL"])
assert len(lst.json["hits"]["hits"]) == 3

resp_request_create = logged_client_post(creator, "post",
urls["BASE_URL_REQUESTS"], json=delete_record_data_function(receiver.id, record1["id"])
resp_request_create = logged_client_post(
creator,
"post",
urls["BASE_URL_REQUESTS"],
json=delete_record_data_function(receiver.id, record1["id"]),
)
resp_request_submit = logged_client_post(creator, "post",
link_api2testclient(resp_request_create.json["links"]["actions"]["submit"])
resp_request_submit = logged_client_post(
creator,
"post",
link_api2testclient(resp_request_create.json["links"]["actions"]["submit"]),
)
print()

Expand All @@ -35,42 +39,58 @@ def test_delete(
"accept",
"decline",
}
delete = logged_client_post(receiver, "post",
link_api2testclient(record.json["requests"][0]["links"]["actions"]["accept"])
delete = logged_client_post(
receiver,
"post",
link_api2testclient(record.json["requests"][0]["links"]["actions"]["accept"]),
)

ThesisRecord.index.refresh()
ThesisDraft.index.refresh()
lst = logged_client_post(creator, "get", urls["BASE_URL"])
assert len(lst.json["hits"]["hits"]) == 2

resp_request_create = logged_client_post(creator, "post",
urls["BASE_URL_REQUESTS"], json=delete_record_data_function(receiver.id, record2["id"])
resp_request_create = logged_client_post(
creator,
"post",
urls["BASE_URL_REQUESTS"],
json=delete_record_data_function(receiver.id, record2["id"]),
)
resp_request_submit = logged_client_post(creator, "post",
link_api2testclient(resp_request_create.json["links"]["actions"]["submit"])
resp_request_submit = logged_client_post(
creator,
"post",
link_api2testclient(resp_request_create.json["links"]["actions"]["submit"]),
)
record = logged_client_post(receiver, "get",f"{urls['BASE_URL']}{record2['id']}")
decline = logged_client_post(receiver, "post",
link_api2testclient(record.json["requests"][0]["links"]["actions"]["decline"])
record = logged_client_post(receiver, "get", f"{urls['BASE_URL']}{record2['id']}")
decline = logged_client_post(
receiver,
"post",
link_api2testclient(record.json["requests"][0]["links"]["actions"]["decline"]),
)
declined_request = logged_client_post(creator, "get",
f"{urls['BASE_URL_REQUESTS']}{resp_request_create.json['id']}"
declined_request = logged_client_post(
creator, "get", f"{urls['BASE_URL_REQUESTS']}{resp_request_create.json['id']}"
)
assert declined_request.json["status"] == "declined"

resp_request_create = logged_client_post(creator, "post",
urls["BASE_URL_REQUESTS"], json=delete_record_data_function(receiver.id, record3["id"])
resp_request_create = logged_client_post(
creator,
"post",
urls["BASE_URL_REQUESTS"],
json=delete_record_data_function(receiver.id, record3["id"]),
)
resp_request_submit = logged_client_post(creator, "post",
link_api2testclient(resp_request_create.json["links"]["actions"]["submit"])
resp_request_submit = logged_client_post(
creator,
"post",
link_api2testclient(resp_request_create.json["links"]["actions"]["submit"]),
)
record = logged_client_post(creator, "get",f"{urls['BASE_URL']}{record3['id']}")
record = logged_client_post(creator, "get", f"{urls['BASE_URL']}{record3['id']}")
assert record.json["requests"][0]["links"]["actions"].keys() == {"cancel"}
cancel = logged_client_post(creator, "post",
link_api2testclient(record.json["requests"][0]["links"]["actions"]["cancel"])
cancel = logged_client_post(
creator,
"post",
link_api2testclient(record.json["requests"][0]["links"]["actions"]["cancel"]),
)
canceled_request = logged_client_post(creator, "get",
f"{urls['BASE_URL_REQUESTS']}{resp_request_create.json['id']}"
canceled_request = logged_client_post(
creator, "get", f"{urls['BASE_URL_REQUESTS']}{resp_request_create.json['id']}"
)
assert canceled_request.json["status"] == "cancelled"
Loading

0 comments on commit d1d8ab7

Please sign in to comment.