Skip to content

Commit

Permalink
Merge pull request #111 from oarepo/miroslavsimek/be-667-create-a-new…
Browse files Browse the repository at this point in the history
…-publish_new_version-request-type

Miroslavsimek/be 667 create a new publish new version request type
  • Loading branch information
mirekys authored Feb 17, 2025
2 parents a608c57 + 945b8ea commit 6936f39
Show file tree
Hide file tree
Showing 9 changed files with 8,431 additions and 4,492 deletions.
7 changes: 1 addition & 6 deletions oarepo_requests/actions/publish_draft.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from invenio_access.permissions import system_identity
from invenio_notifications.services.uow import NotificationOp
from invenio_records_resources.services.uow import RecordCommitOp, UnitOfWork
from invenio_records_resources.services.uow import UnitOfWork
from oarepo_runtime.datastreams.utils import get_record_service_for_record
from oarepo_runtime.i18n import lazy_gettext as _

Expand Down Expand Up @@ -67,7 +67,6 @@ def apply(
**kwargs: Any,
) -> Record:
"""Publish the draft."""

uow.register(
NotificationOp(
PublishDraftRequestSubmitNotificationBuilder.build(request=self.request)
Expand Down Expand Up @@ -101,10 +100,6 @@ def apply(
raise KeyError(f"topic {topic} service not found")
id_ = topic["id"]

if "payload" in self.request and "version" in self.request["payload"]:
topic.metadata["version"] = self.request["payload"]["version"]
uow.register(RecordCommitOp(topic, indexer=topic_service.indexer))

published_topic = topic_service.publish(
identity, id_, *args, uow=uow, expand=False, **kwargs
)
Expand Down
52 changes: 52 additions & 0 deletions oarepo_requests/actions/publish_new_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#
# Copyright (C) 2024 CESNET z.s.p.o.
#
# oarepo-requests is free software; you can redistribute it and/or
# modify it under the terms of the MIT License; see LICENSE file for more
# details.
#
"""Actions for publishing draft requests."""

from __future__ import annotations

from typing import TYPE_CHECKING, Any

from invenio_records_resources.services.uow import RecordCommitOp, UnitOfWork
from oarepo_runtime.datastreams.utils import get_record_service_for_record
from oarepo_runtime.i18n import lazy_gettext as _

if TYPE_CHECKING:
from flask_principal import Identity
from invenio_drafts_resources.records import Record
from invenio_requests.customizations import RequestType

from .publish_draft import PublishDraftAcceptAction


class PublishNewVersionAcceptAction(PublishDraftAcceptAction):
"""Accept action for publishing draft requests."""

self_link = "published_record:links:self"
self_html_link = "published_record:links:self_html"

name = _("Publish")

def apply(
self,
identity: Identity,
request_type: RequestType,
topic: Record,
uow: UnitOfWork,
*args: Any,
**kwargs: Any,
) -> Record:
"""Publish the draft."""
topic_service = get_record_service_for_record(topic)
if not topic_service:
raise KeyError(f"topic {topic} service not found")

if "payload" in self.request and "version" in self.request["payload"]:
topic.metadata["version"] = self.request["payload"]["version"]
uow.register(RecordCommitOp(topic, indexer=topic_service.indexer))

return super().apply(identity, request_type, topic, uow, *args, **kwargs)
11 changes: 0 additions & 11 deletions oarepo_requests/types/publish_draft.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,6 @@ class PublishDraftRequestType(NonDuplicableOARepoRequestType):
attribute="published_record:links:self_html",
data_key="published_record:links:self_html",
),
"version": ma.fields.Str(),
}

form = {
"field": "version",
"ui_widget": "Input",
"props": {
"label": _("Resource version"),
"placeholder": _("Write down the version (first, second…)."),
"required": False,
},
}

@classproperty
Expand Down
66 changes: 66 additions & 0 deletions oarepo_requests/types/publish_new_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#
# Copyright (C) 2024 CESNET z.s.p.o.
#
# oarepo-requests is free software; you can redistribute it and/or
# modify it under the terms of the MIT License; see LICENSE file for more
# details.
#
"""Publish draft request type."""

from __future__ import annotations

from typing import TYPE_CHECKING

import marshmallow as ma
from oarepo_runtime.i18n import lazy_gettext as _

from ..actions.publish_draft import (
PublishDraftDeclineAction,
PublishDraftSubmitAction,
)
from ..actions.publish_new_version import PublishNewVersionAcceptAction
from ..utils import classproperty
from .ref_types import ModelRefTypes

if TYPE_CHECKING:
from invenio_requests.customizations.actions import RequestAction


from .publish_draft import PublishDraftRequestType


class PublishNewVersionRequestType(PublishDraftRequestType):
"""Publish draft request type."""

type_id = "publish_new_version"
name = _("Publish new version")
payload_schema = {
**PublishDraftRequestType.payload_schema,
"version": ma.fields.Str(),
}

form = {
"field": "version",
"ui_widget": "Input",
"props": {
"label": _("Resource version"),
"placeholder": _("Write down the version (first, second…)."),
"required": False,
},
}

@classproperty
def available_actions(cls) -> dict[str, type[RequestAction]]:
"""Return available actions for the request type."""
return {
**super().available_actions,
"submit": PublishDraftSubmitAction,
"accept": PublishNewVersionAcceptAction,
"decline": PublishDraftDeclineAction,
}

description = _("Request publishing of a draft")
receiver_can_be_none = True
allowed_topic_ref_types = ModelRefTypes(published=True, draft=True)

editable = False # type: ignore
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = oarepo-requests
version = 2.3.16
version = 2.3.17
description =
authors = Ronald Krist <[email protected]>
readme = README.md
Expand Down Expand Up @@ -57,6 +57,7 @@ invenio_requests.types =
edit_published_record = oarepo_requests.types.edit_record:EditPublishedRecordRequestType
publish_draft = oarepo_requests.types.publish_draft:PublishDraftRequestType
new_version = oarepo_requests.types.new_version:NewVersionRequestType
publish_new_version = oarepo_requests.types.publish_new_version:PublishNewVersionRequestType
oarepo_workflows.state_changed_notifiers =
auto-requester = oarepo_requests.services.permissions.requester:auto_request_state_change_notifier
invenio_base.finalize_app =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def test_ui_serialization(

sorted_draft_list = allowed_request_types_draft.json["hits"]["hits"]
sorted_draft_list.sort(key=lambda serialized_rt: serialized_rt["type_id"])

print(sorted_draft_list)
assert sorted_draft_list == [
{
"dangerous": True,
Expand Down Expand Up @@ -205,7 +205,7 @@ def test_ui_serialization(
"type_id": "publish_draft",
"dangerous": False,
"editable": False,
"has_form": True,
"has_form": False,
"stateful_description": "By submitting the draft for review you are "
"requesting the publication of the draft. The draft "
"will become locked and no further changes will be "
Expand Down
3 changes: 2 additions & 1 deletion tests/test_requests/test_publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ def test_publish(

published_record = receiver_client.get(f"{urls['BASE_URL']}{draft1_id}?expand=true")

assert "version" in published_record.json["metadata"]
# version is no more on "publish_draft" request
# assert "version" in published_record.json["metadata"]

ThesisRecord.index.refresh()
ThesisDraft.index.refresh()
Expand Down
Loading

0 comments on commit 6936f39

Please sign in to comment.