Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

requests: add support for simple notification in the review process #239

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 81 additions & 8 deletions geo_rdm_records/modules/packages/requests/feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,89 @@

"""GEO RDM Records feed post requests."""

from flask import current_app
from flask_babelex import lazy_gettext as _
from invenio_requests.customizations import RequestType, actions
from sqlalchemy.exc import NoResultFound

from geo_rdm_records.proxies import current_requests_notification_service
from geo_rdm_records.modules.requests.notification.handler import (
BaseNotificationHandler,
)
from geo_rdm_records.proxies import current_geo_packages_service


#
# Notification handling
#
class NotificationHandler(BaseNotificationHandler, actions.RequestAction):
"""Notification handler class for feed requests."""

notification_template = (
"geo_rdm_records/email/feed/request-package-publication.html",
)
"""Notification template."""

def _build_message(self, identity, **kwargs):
"""Message factory.

Args:
identity (flask_principal.Identity): Entity identity.

**kwargs: Extra arguments

Returns:
dict: message metadata.
"""
request_id = str(self.request.id)

# Preparing package data to the notification
package = self.request.topic.resolve()
package_id = package["id"]

record = current_geo_packages_service.read(identity, package_id).to_dict()

package_title = record["metadata"]["title"]
package_url = record["links"]["self_html"]

# Preparing request data
# Generating request ui address as the requests service doesn't generate it
# ToDo: This should be a temporary solution
_ui_url = current_app.config["SITE_UI_URL"]
request_url = f"{_ui_url}/me/requests/{request_id}"

notification_emails = current_app.config[
"GEO_RDM_NOTIFICATION_DEFAULT_RECEIVER_EMAILS"
]

if notification_emails:
return dict(
subject="[GEO Knowledge Hub] New Feed post request",
template_html=self.notification_template,
recipients=notification_emails,
ctx=dict(
record_title=package_title,
record_url=package_url,
request=request_id,
request_url=request_url,
),
)

#
# High-level API
#
def notify(self, identity, uow, **kwargs):
"""Notify users with a given message."""
try:
message = self._build_message(identity)
super().notify(identity, message, uow)
except NoResultFound:
pass # any notification is sent


#
# Actions
#
class SubmitAction(actions.SubmitAction):
class SubmitAction(NotificationHandler, actions.SubmitAction):
"""Submit action.

ToDos:
Expand All @@ -29,21 +102,21 @@ def execute(self, identity, uow):
# Create a custom title for the request
record_title = record["metadata"]["title"]
request_title = f"Feed: {record_title}"

# Defining the custom title for the request
self.request["title"] = request_title

# Use the CMS service to manage the feed post.
self.notify(identity, uow=uow)

super().execute(identity, uow)


class AcceptAction(actions.AcceptAction):
class AcceptAction(NotificationHandler, actions.AcceptAction):
"""Accept action."""

def execute(self, identity, uow):
"""Accept feed post creation."""
# Use the CMS service to manage the feed post.
current_requests_notification_service.notify_creation(
identity, self.request, uow=uow
)

# Finish operation.
super().execute(identity, uow)

Expand Down
89 changes: 81 additions & 8 deletions geo_rdm_records/modules/packages/requests/training.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,89 @@

"""GEO RDM Records Package requests definition."""

from flask import current_app
from flask_babelex import lazy_gettext as _
from invenio_requests.customizations import RequestType, actions
from sqlalchemy.exc import NoResultFound

from geo_rdm_records.proxies import current_requests_notification_service
from geo_rdm_records.modules.requests.notification.handler import (
BaseNotificationHandler,
)
from geo_rdm_records.proxies import current_geo_packages_service


#
# Notification handling
#
class NotificationHandler(BaseNotificationHandler, actions.RequestAction):
"""Notification handler class for training requests."""

notification_template = (
"geo_rdm_records/email/training/request-package-training.html",
)
"""Notification template."""

def _build_message(self, identity, **kwargs):
"""Message factory.

Args:
identity (flask_principal.Identity): Entity identity.

**kwargs: Extra arguments

Returns:
dict: message metadata.
"""
request_id = str(self.request.id)

# Preparing package data to the notification
package = self.request.topic.resolve()
package_id = package["id"]

record = current_geo_packages_service.read(identity, package_id).to_dict()

package_title = record["metadata"]["title"]
package_url = record["links"]["self_html"]

# Preparing request data
# Generating request ui address as the requests service doesn't generate it
# ToDo: This should be a temporary solution
_ui_url = current_app.config["SITE_UI_URL"]
request_url = f"{_ui_url}/me/requests/{request_id}"

notification_emails = current_app.config[
"GEO_RDM_NOTIFICATION_DEFAULT_RECEIVER_EMAILS"
]

if notification_emails:
return dict(
subject="[GEO Knowledge Hub] New Training Session request",
template_html=self.notification_template,
recipients=notification_emails,
ctx=dict(
record_title=package_title,
record_url=package_url,
request=request_id,
request_url=request_url,
),
)

#
# High-level API
#
def notify(self, identity, uow, **kwargs):
"""Notify users with a given message."""
try:
message = self._build_message(identity)
super().notify(identity, message, uow)
except NoResultFound:
pass # any notification is sent


#
# Actions
#
class SubmitAction(actions.SubmitAction):
class SubmitAction(NotificationHandler, actions.SubmitAction):
"""Submit action."""

def execute(self, identity, uow):
Expand All @@ -25,21 +98,21 @@ def execute(self, identity, uow):
# Create a custom title for the request
record_title = record["metadata"]["title"]
request_title = f"Training request: {record_title}"

# Defining the custom title for the request
self.request["title"] = request_title

# Use the Assistance requests service to manage the training session.
self.notify(identity, uow=uow)

super().execute(identity, uow)


class AcceptAction(actions.AcceptAction):
class AcceptAction(NotificationHandler, actions.AcceptAction):
"""Accept action."""

def execute(self, identity, uow):
"""Accept feed post creation."""
# Use the Assistance requests service to manage the training session.
current_requests_notification_service.notify_creation(
identity, self.request, uow=uow
)

# Finish operation.
super().execute(identity, uow)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def _search_record_requests(self, record, identity, exclude=None):

if request.type.type_id in self.request_type:
# ToDo: Assumes that only one request is available per user.
# Remove this on version 1.7.0 with the InvenioRDM requests endpoint.
# Remove this on version 1.8.0 with the InvenioRDM requests endpoint.
is_request_creator = request.created_by.resolve().id == identity.id
is_cancelled = request.status == "cancelled"
is_valid = request.status not in exclude
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,12 @@
"type": "keyword"
}
}
},
"type": {
"type": "keyword"
},
"disclaimer": {
"type": "text"
}
}
},
Expand Down
Loading
Loading