Skip to content

Commit

Permalink
Merge pull request #10 from oarepo/krist/be-241-self-link-on-requests…
Browse files Browse the repository at this point in the history
…-goes-to-the-extended-service-but-one

nr docs release issues
  • Loading branch information
SilvyPuzzlewell authored Mar 21, 2024
2 parents c8e7f70 + 52902b4 commit ac60865
Show file tree
Hide file tree
Showing 14 changed files with 271 additions and 62 deletions.
12 changes: 8 additions & 4 deletions oarepo_requests/ext.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from invenio_base.utils import obj_or_import_string
from invenio_requests.proxies import current_events_service

from oarepo_requests.resources.events.config import OARepoRequestsCommentsResourceConfig
from oarepo_requests.resources.events.resource import OARepoRequestsCommentsResource
from oarepo_requests.resources.oarepo.config import OARepoRequestsResourceConfig
from oarepo_requests.resources.oarepo.resource import OARepoRequestsResource
from oarepo_requests.services.oarepo.config import OARepoRequestsServiceConfig
Expand Down Expand Up @@ -48,13 +51,14 @@ def init_services(self, app):
service_configs = self.service_configs(app)
"""Initialize the service and resource for Requests."""
self.requests_service = OARepoRequestsService(config=service_configs.requests)
# self.request_events_service = RequestEventsService(
# config=service_configs.request_events,
# )

def init_resources(self, app):
"""Init resources."""
self.requests_resource = OARepoRequestsResource(
service=self.requests_service,
oarepo_requests_service=self.requests_service,
config=OARepoRequestsResourceConfig.build(app),
)
self.request_events_resource = OARepoRequestsCommentsResource(
service=current_events_service,
config=OARepoRequestsCommentsResourceConfig.build(app),
)
Empty file.
29 changes: 29 additions & 0 deletions oarepo_requests/resources/events/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from flask_resources import ResponseHandler
from invenio_records_resources.services.base.config import ConfiguratorMixin
from invenio_requests.resources.events.config import RequestCommentsResourceConfig

from oarepo_requests.resources.ui import OARepoRequestsUIJSONSerializer


class OARepoRequestsCommentsResourceConfig(
RequestCommentsResourceConfig, ConfiguratorMixin
):
""""""

blueprint_name = "oarepo_request_events"
url_prefix = "/requests"
routes = {
**RequestCommentsResourceConfig.routes,
"list-extended": "/extended/<request_id>/comments",
"item-extended": "/extended/<request_id>/comments/<comment_id>",
"timeline-extended": "/extended/<request_id>/timeline",
}

@property
def response_handlers(self):
return {
"application/vnd.inveniordm.v1+json": ResponseHandler(
OARepoRequestsUIJSONSerializer()
),
**super().response_handlers,
}
36 changes: 36 additions & 0 deletions oarepo_requests/resources/events/resource.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from flask_resources import route
from invenio_records_resources.resources.errors import ErrorHandlersMixin
from invenio_requests.resources.events.resource import RequestCommentsResource


class OARepoRequestsCommentsResource(RequestCommentsResource, ErrorHandlersMixin):

def create_url_rules(self):
"""Create the URL rules for the record resource."""
base_routes = super().create_url_rules()
routes = self.config.routes

url_rules = [
route("POST", routes["list-extended"], self.create_extended),
route("GET", routes["item-extended"], self.read_extended),
route("PUT", routes["item-extended"], self.update_extended),
route("DELETE", routes["item-extended"], self.delete_extended),
route("GET", routes["timeline-extended"], self.search_extended),
]
return url_rules + base_routes

# from parent
def create_extended(self):
return super().create()

def read_extended(self):
return super().read()

def update_extended(self):
return super().update()

def delete_extended(self):
return super().delete()

def search_extended(self):
return super().search()
27 changes: 19 additions & 8 deletions oarepo_requests/resources/oarepo/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@
request_headers,
request_view_args,
)
from invenio_requests.proxies import current_requests_service
from invenio_requests.resources import RequestsResource

from oarepo_requests.utils import stringify_first_val


class OARepoRequestsResource(RequestsResource, ErrorHandlersMixin):
"""
def __init__(self, config, service):
super().__init__(config)
self.service = service
"""

def __init__(
self,
config,
oarepo_requests_service,
invenio_requests_service=current_requests_service,
):
# so super methods can be used with original service
super().__init__(config, invenio_requests_service)
self.oarepo_requests_service = oarepo_requests_service

def create_url_rules(self):
"""Create the URL rules for the record resource."""
Expand All @@ -36,6 +42,7 @@ def s(route):
route("POST", p(routes["list"]), self.create),
route("POST", p(routes["list-extended"]), self.create_extended),
route("GET", p(routes["item-extended"]), self.read_extended),
route("PUT", p(routes["item-extended"]), self.update_extended),
]
return url_rules + base_routes

Expand All @@ -46,7 +53,7 @@ def s(route):
@response_handler()
def create(self):

items = self.service.create(
items = self.oarepo_requests_service.create(
identity=g.identity,
data=resource_requestctx.data,
request_type=resource_requestctx.data.pop("request_type", None),
Expand All @@ -72,7 +79,7 @@ def stringify_first_val(dct):
dct[k] = str(v)
return dct

items = self.service.create(
items = self.oarepo_requests_service.create(
identity=g.identity,
data=resource_requestctx.data,
type_id=resource_requestctx.data.pop("request_type", None),
Expand All @@ -91,9 +98,13 @@ def stringify_first_val(dct):
@response_handler()
def read_extended(self):
"""Read an item."""
item = self.service.read(
item = self.oarepo_requests_service.read(
id_=resource_requestctx.view_args["id"],
identity=g.identity,
expand=resource_requestctx.args.get("expand", False),
)
return item.to_dict(), 200

# from parent
def update_extended(self):
return super().update()
3 changes: 1 addition & 2 deletions oarepo_requests/services/schema.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import marshmallow as ma
from invenio_drafts_resources.services.records.config import is_record
from invenio_records_resources.services import ConditionalLink
from invenio_records_resources.services.base.links import Link, LinksTemplate
from invenio_requests.proxies import current_request_type_registry
from invenio_requests.services.schemas import GenericRequestSchema
from marshmallow import fields

from oarepo_requests.utils import get_matching_service_for_record
from oarepo_requests.utils import get_matching_service_for_record, is_record


def get_links_schema():
Expand Down
7 changes: 7 additions & 0 deletions oarepo_requests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,10 @@ def stringify_first_val(dct):
for k, v in dct.items():
dct[k] = str(v)
return dct


def is_record(record, ctx):
"""Shortcut for links to determine if record is a record."""
if not hasattr(record, "is_draft"):
return True
return not record.is_draft
7 changes: 7 additions & 0 deletions oarepo_requests/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@ def create_oarepo_requests(app):
ext = app.extensions["oarepo-requests"]
blueprint = ext.requests_resource.as_blueprint()
return blueprint


def create_oarepo_requests_events(app):
"""Create requests blueprint."""
ext = app.extensions["oarepo-requests"]
blueprint = ext.request_events_resource.as_blueprint()
return blueprint
7 changes: 7 additions & 0 deletions oarepo_requests/views/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@
def create_app_blueprint(app):
blueprint = Blueprint("oarepo_requests_app", __name__, url_prefix="/requests/")
return blueprint


def create_app_events_blueprint(app):
blueprint = Blueprint(
"oarepo_requests_events_app", __name__, url_prefix="/requests/"
)
return blueprint
6 changes: 3 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = oarepo-requests
version = 1.1.4
version = 1.1.5
description =
authors = Ronald Krist <[email protected]>
readme = README.md
Expand Down Expand Up @@ -31,14 +31,14 @@ invenio_base.api_apps =
oarepo_requests = oarepo_requests.ext:OARepoRequests
invenio_base.apps =
thesis = oarepo_requests.ext:OARepoRequests
#invenio_base.blueprints =
# oarepo_requests = oarepo_requests.views.api:create_oarepo_requests

invenio_base.api_blueprints =
oarepo_requests = oarepo_requests.views.api:create_oarepo_requests
oarepo_requests_events = oarepo_requests.views.api:create_oarepo_requests_events

invenio_base.blueprints =
oarepo_requests = oarepo_requests.views.app:create_app_blueprint
oarepo_requests_events = oarepo_requests.views.app:create_app_events_blueprint



11 changes: 11 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,17 @@ def request_with_receiver_user(
return request_item


@pytest.fixture()
def events_resource_data():
"""Input data for the Request Events Resource (REST body)."""
return {
"payload": {
"content": "This is a comment.",
"format": RequestEventFormat.HTML.value,
}
}


# -------
import os

Expand Down
142 changes: 142 additions & 0 deletions tests/test_requests/test_extended.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
from invenio_requests.records.api import RequestEvent

from .utils import is_valid_subdict, link_api2testclient


def test_read_extended(
example_topic_draft,
client_logged_as,
users,
urls,
publish_request_data_function,
serialization_result,
ui_serialization_result,
search_clear,
):
receiver = users[1]
creator_client = client_logged_as(users[0].email)
resp_request_create = creator_client.post(
urls["BASE_URL_REQUESTS"],
json=publish_request_data_function(example_topic_draft["id"]),
)
resp_request_submit = creator_client.post(
link_api2testclient(resp_request_create.json["links"]["actions"]["submit"])
)
receiver_client = client_logged_as(users[1].email)
old_call = receiver_client.get(
f"{urls['BASE_URL_REQUESTS']}{resp_request_create.json['id']}"
)
new_call = receiver_client.get(
f"{urls['BASE_URL_REQUESTS']}extended/{resp_request_create.json['id']}"
)
new_call2 = receiver_client.get(
f"{urls['BASE_URL_REQUESTS']}extended/{resp_request_create.json['id']}",
headers={"Accept": "application/vnd.inveniordm.v1+json"},
)

assert is_valid_subdict(
serialization_result(example_topic_draft["id"], resp_request_create.json["id"]),
new_call.json,
)
assert is_valid_subdict(
ui_serialization_result(
example_topic_draft["id"], resp_request_create.json["id"]
),
new_call2.json,
)


def test_update_self_link(
example_topic_draft,
client_logged_as,
users,
urls,
publish_request_data_function,
serialization_result,
ui_serialization_result,
search_clear,
):
receiver = users[1]
creator_client = client_logged_as(users[0].email)
resp_request_create = creator_client.post(
urls["BASE_URL_REQUESTS"],
json=publish_request_data_function(example_topic_draft["id"]),
)
resp_request_submit = creator_client.post(
link_api2testclient(resp_request_create.json["links"]["actions"]["submit"])
)
read_before = creator_client.get(
link_api2testclient(resp_request_submit.json["links"]["self"]),
)
read_from_record = creator_client.get(
f"{urls['BASE_URL']}{example_topic_draft['id']}/draft",
)
link_to_extended = link_api2testclient(
read_from_record.json["requests"][0]["links"]["self"]
)

assert link_to_extended.startswith(f"{urls['BASE_URL_REQUESTS']}extended")
update_extended = creator_client.put(
link_to_extended,
json={"title": "lalala"},
)
assert update_extended.status_code == 200
read_after = creator_client.get(
link_api2testclient(resp_request_submit.json["links"]["self"]),
)
assert read_before.json["title"] == ""
assert read_after.json["title"] == "lalala"


def test_events_resource(
example_topic_draft,
client_logged_as,
users,
urls,
publish_request_data_function,
serialization_result,
ui_serialization_result,
events_resource_data,
search_clear,
):
creator_client = client_logged_as(users[0].email)
resp_request_create = creator_client.post(
urls["BASE_URL_REQUESTS"],
json=publish_request_data_function(example_topic_draft["id"]),
)
resp_request_submit = creator_client.post(
link_api2testclient(resp_request_create.json["links"]["actions"]["submit"])
)
read_before = creator_client.get(
link_api2testclient(resp_request_submit.json["links"]["self"]),
headers={"Accept": "application/vnd.inveniordm.v1+json"},
)
read_from_record = creator_client.get(
f"{urls['BASE_URL']}{example_topic_draft['id']}/draft",
)

comments_link = link_api2testclient(
read_from_record.json["requests"][0]["links"]["comments"]
)
timeline_link = link_api2testclient(
read_from_record.json["requests"][0]["links"]["timeline"]
)

assert comments_link.startswith("/requests/extended")
assert timeline_link.startswith("/requests/extended")

comments_extended = creator_client.post(
comments_link,
json=events_resource_data,
)
assert comments_extended.status_code == 201
comment = creator_client.get(
f"{comments_link}/{comments_extended.json['id']}",
)
assert comment.status_code == 200
RequestEvent.index.refresh()
comments_extended_timeline = creator_client.get(
timeline_link,
)
assert comments_extended_timeline.status_code == 200
assert len(comments_extended_timeline.json["hits"]["hits"]) == 1
Loading

0 comments on commit ac60865

Please sign in to comment.