-
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 #107 from oarepo/krist/be-443-configure-notifications
Krist/be 443 configure notifications
- Loading branch information
Showing
52 changed files
with
1,216 additions
and
1,438 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
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
Empty file.
Empty file.
18 changes: 18 additions & 0 deletions
18
oarepo_requests/notifications/builders/delete_published_record.py
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 ..generators import EntityRecipient | ||
from .oarepo import OARepoRequestActionNotificationBuilder | ||
|
||
|
||
class DeletePublishedRecordRequestSubmitNotificationBuilder( | ||
OARepoRequestActionNotificationBuilder | ||
): | ||
type = "delete-published-record-request-event.submit" | ||
|
||
recipients = [EntityRecipient(key="request.receiver")] # email only | ||
|
||
|
||
class DeletePublishedRecordRequestAcceptNotificationBuilder( | ||
OARepoRequestActionNotificationBuilder | ||
): | ||
type = "delete-published-record-request-event.accept" | ||
|
||
recipients = [EntityRecipient(key="request.created_by")] |
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,38 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from invenio_notifications.backends import EmailNotificationBackend | ||
from invenio_notifications.models import Notification | ||
from invenio_notifications.registry import EntityResolverRegistry | ||
from invenio_notifications.services.builders import NotificationBuilder | ||
from invenio_notifications.services.generators import EntityResolve, UserEmailBackend | ||
|
||
if TYPE_CHECKING: | ||
from invenio_requests.records.api import Request | ||
|
||
|
||
class OARepoUserEmailBackend(UserEmailBackend): | ||
backend_id = EmailNotificationBackend.id | ||
|
||
|
||
class OARepoRequestActionNotificationBuilder(NotificationBuilder): | ||
|
||
@classmethod | ||
def build(cls, request: Request): | ||
"""Build notification with context.""" | ||
return Notification( | ||
type=cls.type, | ||
context={ | ||
"request": EntityResolverRegistry.reference_entity(request), | ||
"backend_ids": [ | ||
backend.backend_id for backend in cls.recipient_backends | ||
], | ||
}, | ||
) | ||
|
||
context = [ | ||
EntityResolve(key="request"), | ||
] | ||
|
||
recipient_backends = [OARepoUserEmailBackend()] |
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 ..generators import EntityRecipient | ||
from .oarepo import OARepoRequestActionNotificationBuilder | ||
|
||
|
||
class PublishDraftRequestSubmitNotificationBuilder( | ||
OARepoRequestActionNotificationBuilder | ||
): | ||
type = "publish-draft-request-event.submit" | ||
|
||
recipients = [EntityRecipient(key="request.receiver")] # email only | ||
|
||
|
||
class PublishDraftRequestAcceptNotificationBuilder( | ||
OARepoRequestActionNotificationBuilder | ||
): | ||
type = "publish-draft-request-event.accept" | ||
|
||
recipients = [EntityRecipient(key="request.created_by")] |
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,77 @@ | ||
from __future__ import annotations | ||
|
||
from abc import abstractmethod | ||
from typing import TYPE_CHECKING | ||
|
||
from invenio_notifications.models import Recipient | ||
from invenio_notifications.services.generators import RecipientGenerator | ||
from invenio_records.dictutils import dict_lookup | ||
from invenio_requests.proxies import current_requests | ||
|
||
from oarepo_requests.proxies import current_notification_recipients_resolvers_registry | ||
|
||
if TYPE_CHECKING: | ||
from typing import Any | ||
|
||
from invenio_notifications.models import Notification | ||
|
||
|
||
class EntityRecipient(RecipientGenerator): | ||
"""Recipient generator working as handler for generic entity.""" | ||
|
||
def __init__(self, key: str): | ||
self.key = key | ||
|
||
def __call__(self, notification: Notification, recipients: dict[str, Recipient]): | ||
"""""" | ||
backend_ids = notification.context["backend_ids"] | ||
entity_ref = dict_lookup(notification.context, self.key) | ||
entity_type = list(entity_ref.keys())[0] | ||
for backend_id in backend_ids: | ||
generator = current_notification_recipients_resolvers_registry[entity_type][ | ||
backend_id | ||
](entity_ref) | ||
generator(notification, recipients) | ||
|
||
|
||
class SpecificEntityRecipient(RecipientGenerator): | ||
"""Superclass for implementations of recipient generators for specific entities.""" | ||
|
||
def __init__(self, key): | ||
self.key = key # todo this is entity_reference, not path to entity as EntityRecipient, might be confusing | ||
|
||
def __call__(self, notification: Notification, recipients: dict[str, Recipient]): | ||
entity = self._resolve_entity() | ||
recipients.update(self._get_recipients(entity)) | ||
return recipients | ||
|
||
@abstractmethod | ||
def _get_recipients(self, entity: Any) -> dict[str, Recipient]: | ||
raise NotImplementedError() | ||
|
||
def _resolve_entity(self) -> Any: | ||
entity_type = list(self.key)[0] | ||
registry = current_requests.entity_resolvers_registry | ||
|
||
registered_resolvers = registry._registered_types | ||
resolver = registered_resolvers.get(entity_type) | ||
proxy = resolver.get_entity_proxy(self.key) | ||
entity = proxy.resolve() | ||
return entity | ||
|
||
|
||
class UserEmailRecipient(SpecificEntityRecipient): | ||
"""User email recipient generator for a notification.""" | ||
|
||
def _get_recipients(self, entity: Any) -> dict[str, Recipient]: | ||
return {entity.email: Recipient(data={"email": entity.email})} | ||
|
||
|
||
class GroupEmailRecipient(SpecificEntityRecipient): | ||
"""Recipient generator returning emails of the members of the recipient group""" | ||
|
||
def _get_recipients(self, entity: Any) -> dict[str, Recipient]: | ||
return { | ||
user.email: Recipient(data={"email": user.email}) | ||
for user in entity.users.all() | ||
} |
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
Oops, something went wrong.