Skip to content

Commit

Permalink
notifications: split classes into respective files
Browse files Browse the repository at this point in the history
  • Loading branch information
rekt-hard authored and kpsherva committed May 5, 2023
1 parent 0d4f412 commit 075d4c8
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 23 deletions.
9 changes: 9 additions & 0 deletions invenio_users_resources/notifications/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2023 Graz University of Technology.
#
# Invenio-Users-Resources is free software; you can redistribute it and/or
# modify it under the terms of the MIT License; see LICENSE file for more
# details.

"""Notification related files for user purposes."""
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@
# modify it under the terms of the MIT License; see LICENSE file for more
# details.

"""User specific resources for notifications."""
"""User specific filters for notifications."""


from invenio_notifications.models import Recipient
from invenio_notifications.services.builders import RecipientGenerator
from invenio_notifications.services.filters import RecipientFilter
from invenio_records.dictutils import dict_lookup


class UserPreferencesRecipientFilter(RecipientFilter):
Expand All @@ -30,17 +26,3 @@ def __call__(self, notification, recipients):
del recipients[key]

return recipients


class UserRecipient(RecipientGenerator):
"""User recipient generator for a notification."""

def __init__(self, key):
"""Ctor."""
self.key = key

def __call__(self, notification, recipients):
"""Update required recipient information and add backend id."""
user = dict_lookup(notification.context, self.key)
recipients[user["id"]] = Recipient(data=user)
return recipients
28 changes: 28 additions & 0 deletions invenio_users_resources/notifications/generators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2023 Graz University of Technology.
#
# Invenio-Users-Resources is free software; you can redistribute it and/or
# modify it under the terms of the MIT License; see LICENSE file for more
# details.

"""User specific generators for notifications."""


from invenio_notifications.models import Recipient
from invenio_notifications.services.generators import RecipientGenerator
from invenio_records.dictutils import dict_lookup


class UserRecipient(RecipientGenerator):
"""User recipient generator for a notification."""

def __init__(self, key):
"""Ctor."""
self.key = key

def __call__(self, notification, recipients):
"""Update required recipient information and add backend id."""
user = dict_lookup(notification.context, self.key)
recipients[user["id"]] = Recipient(data=user)
return recipients
6 changes: 2 additions & 4 deletions tests/test_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@

from invenio_notifications.models import Notification, Recipient

from invenio_users_resources.notifications import (
UserPreferencesRecipientFilter,
UserRecipient,
)
from invenio_users_resources.notifications.filters import UserPreferencesRecipientFilter
from invenio_users_resources.notifications.generators import UserRecipient
from invenio_users_resources.records.api import UserAggregate


Expand Down

0 comments on commit 075d4c8

Please sign in to comment.