-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from oarepo/krist/be-415-new-version-request
Krist/be 415 new version request
- Loading branch information
Showing
12 changed files
with
164 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,16 @@ | ||
from oarepo_runtime.datastreams.utils import get_record_service_for_record | ||
|
||
from .generic import OARepoAcceptAction | ||
from .generic import AddTopicLinksOnPayloadMixin, OARepoAcceptAction | ||
|
||
|
||
class EditTopicAcceptAction(OARepoAcceptAction): | ||
class EditTopicAcceptAction(AddTopicLinksOnPayloadMixin, OARepoAcceptAction): | ||
self_link = "draft_record:links:self" | ||
self_html_link = "draft_record:links:self_html" | ||
|
||
def apply(self, identity, request_type, topic, uow, *args, **kwargs): | ||
topic_service = get_record_service_for_record(topic) | ||
if not topic_service: | ||
raise KeyError(f"topic {topic} service not found") | ||
edit_topic = topic_service.edit(identity, topic["id"], uow=uow) | ||
|
||
# add links to the draft (edited) record | ||
edit_topic_dict = edit_topic.to_dict() | ||
|
||
if "payload" not in self.request: | ||
self.request["payload"] = {} | ||
|
||
# invenio does not allow non-string values in the payload, so using colon notation here | ||
# client will need to handle this and convert to links structure | ||
# can not use dot notation as marshmallow tries to be too smart and does not serialize dotted keys | ||
self.request["payload"]["draft_record:links:self"] = edit_topic_dict[ | ||
"links" | ||
]["self"] | ||
self.request["payload"]["draft_record:links:self_html"] = ( | ||
edit_topic_dict["links"]["self_html"] | ||
) | ||
|
||
return edit_topic._record | ||
|
||
return super().apply(identity, request_type, edit_topic, uow, *args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from oarepo_runtime.datastreams.utils import get_record_service_for_record | ||
|
||
from .generic import AddTopicLinksOnPayloadMixin, OARepoAcceptAction | ||
|
||
|
||
class NewVersionAcceptAction(AddTopicLinksOnPayloadMixin, OARepoAcceptAction): | ||
self_link = "draft_record:links:self" | ||
self_html_link = "draft_record:links:self_html" | ||
|
||
def apply(self, identity, request_type, topic, uow, *args, **kwargs): | ||
topic_service = get_record_service_for_record(topic) | ||
if not topic_service: | ||
raise KeyError(f"topic {topic} service not found") | ||
new_version_topic = topic_service.new_version(identity, topic["id"], uow=uow) | ||
|
||
return super().apply( | ||
identity, request_type, new_version_topic, uow, *args, **kwargs | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import marshmallow as ma | ||
from oarepo_runtime.i18n import lazy_gettext as _ | ||
|
||
from ..actions.new_version import NewVersionAcceptAction | ||
from .generic import NonDuplicableOARepoRequestType | ||
from .ref_types import ModelRefTypes | ||
|
||
|
||
class NewVersionRequestType( | ||
NonDuplicableOARepoRequestType | ||
): # NewVersionFromPublishedRecord? or just new_version | ||
type_id = "new_version" | ||
name = _("New Version") | ||
payload_schema = { | ||
"draft_record.links.self": ma.fields.Str( | ||
attribute="draft_record:links:self", | ||
data_key="draft_record:links:self", | ||
), | ||
"draft_record.links.self_html": ma.fields.Str( | ||
attribute="draft_record:links:self_html", | ||
data_key="draft_record:links:self_html", | ||
), | ||
} | ||
|
||
@classmethod | ||
@property | ||
def available_actions(cls): | ||
return { | ||
**super().available_actions, | ||
"accept": NewVersionAcceptAction, | ||
} | ||
|
||
description = _("Request requesting creation of new version of a published record.") | ||
allowed_topic_ref_types = ModelRefTypes(published=True, draft=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from thesis.records.api import ThesisDraft, ThesisRecord | ||
|
||
from tests.test_requests.utils import link_api2testclient | ||
|
||
|
||
def test_new_version_autoaccept( | ||
vocab_cf, | ||
logged_client, | ||
users, | ||
urls, | ||
new_version_data_function, | ||
record_factory, | ||
search_clear, | ||
): | ||
creator = users[0] | ||
creator_client = logged_client(creator) | ||
|
||
record1 = record_factory(creator.identity) | ||
|
||
resp_request_create = creator_client.post( | ||
urls["BASE_URL_REQUESTS"], | ||
json=new_version_data_function(record1["id"]), | ||
) | ||
resp_request_submit = creator_client.post( | ||
link_api2testclient(resp_request_create.json["links"]["actions"]["submit"]), | ||
) | ||
# is request accepted and closed? | ||
request = creator_client.get( | ||
f'{urls["BASE_URL_REQUESTS"]}{resp_request_create.json["id"]}', | ||
).json | ||
|
||
assert request["status"] == "accepted" | ||
assert not request["is_open"] | ||
assert request["is_closed"] | ||
|
||
assert "draft_record:links:self" in request["payload"] | ||
assert "draft_record:links:self_html" in request["payload"] | ||
|
||
ThesisRecord.index.refresh() | ||
ThesisDraft.index.refresh() | ||
# new_version action worked? | ||
search = creator_client.get( | ||
f'user{urls["BASE_URL"]}', | ||
).json[ | ||
"hits" | ||
]["hits"] | ||
assert len(search) == 2 | ||
assert search[0]["id"] != search[1]["id"] | ||
assert search[0]["parent"]["id"] == search[1]["parent"]["id"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters