Skip to content

Commit

Permalink
Merge branch 'main' into stojanovic/fe-267-redesign-feed-ui
Browse files Browse the repository at this point in the history
  • Loading branch information
mirekys authored Nov 29, 2024
2 parents 87764b5 + 97c1448 commit d2af909
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
27 changes: 14 additions & 13 deletions oarepo_requests/actions/publish_draft.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

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

Expand All @@ -29,35 +28,37 @@
from flask_principal import Identity
from invenio_drafts_resources.records import Record
from invenio_requests.customizations import RequestType
from invenio_requests.customizations.actions import RequestAction

class PublishMixin:
"""Mixin for publish actions."""

class PublishDraftSubmitAction(OARepoSubmitAction):
"""Submit action for publishing draft requests."""

def can_execute(self) -> bool:
def can_execute(self: RequestAction) -> bool:
"""Check if the action can be executed."""
if not super().can_execute():
if not super().can_execute(): # type: ignore
return False

try:
from ..types.publish_draft import PublishDraftRequestType
topic = self.request.topic.resolve()
except: # noqa E722: used for displaying buttons, so ignore errors here
return False
topic_service = get_record_service_for_record(topic)
try:
topic_service.validate_draft(system_identity, topic["id"])
PublishDraftRequestType.validate_topic(system_identity, topic)
return True
except ValidationError:
except: # noqa E722: used for displaying buttons, so ignore errors here
return False

class PublishDraftSubmitAction(PublishMixin, OARepoSubmitAction):
"""Submit action for publishing draft requests."""


class PublishDraftAcceptAction(AddTopicLinksOnPayloadMixin, OARepoAcceptAction):
class PublishDraftAcceptAction(PublishMixin, AddTopicLinksOnPayloadMixin, OARepoAcceptAction):
"""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,
Expand Down
30 changes: 28 additions & 2 deletions oarepo_requests/types/publish_draft.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from typing import TYPE_CHECKING, Any

import marshmallow as ma
from invenio_access.permissions import system_identity
from invenio_records_resources.services.uow import RecordCommitOp, UnitOfWork
from invenio_requests.proxies import current_requests_service
from oarepo_runtime.datastreams.utils import get_record_service_for_record
Expand Down Expand Up @@ -96,8 +95,35 @@ def can_create(
if not topic.is_draft:
raise ValueError("Trying to create publish request on published record")
super().can_create(identity, data, receiver, topic, creator, *args, **kwargs)
self.validate_topic(identity, topic)

@classmethod
def validate_topic(cls, identity: Identity, topic: Record) -> None:
"""Validate the topic.
:param: identity: identity of the caller
:param: topic: topic of the request
:raises: ValidationError: if the topic is not valid
"""
topic_service = get_record_service_for_record(topic)
topic_service.validate_draft(system_identity, topic["id"])
topic_service.validate_draft(identity, topic["id"])

# if files support is enabled for this topic, check if there are any files
if hasattr(topic, "files"):
can_toggle_files = topic_service.check_permission(
identity, "manage_files", record=topic
)
draft_files = topic.files # type: ignore
if draft_files.enabled and not draft_files.items():
if can_toggle_files:
my_message = _(
"Missing uploaded files. To disable files for this record please mark it as metadata-only."
)
else:
my_message = _("Missing uploaded files.")

raise ma.ValidationError({"files.enabled": [my_message]})

@classmethod
def is_applicable_to(
Expand Down

0 comments on commit d2af909

Please sign in to comment.