Skip to content

Commit 9686ae4

Browse files
committed
update notification subscription get or create to create the notification types.
1 parent a6cc270 commit 9686ae4

10 files changed

Lines changed: 75 additions & 106 deletions

File tree

admin/common_auth/views.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from django.contrib.contenttypes.models import ContentType
12
from django.urls import reverse, reverse_lazy
23
from django.http import Http404
34
from django.shortcuts import redirect
@@ -8,6 +9,7 @@
89
from django.contrib import messages
910
from django.contrib.auth.mixins import PermissionRequiredMixin
1011
from django.contrib.auth import login, REDIRECT_FIELD_NAME, authenticate, logout
12+
from osf.models.notification_subscription import NotificationSubscription
1113

1214
from osf.models.user import OSFUser
1315
from osf.models import AdminProfile, AbstractProvider
@@ -77,7 +79,20 @@ def form_valid(self, form):
7779
if group_type == 'reviews':
7880
provider_id = split[2]
7981
provider = AbstractProvider.objects.get(id=provider_id)
80-
provider.notification_subscriptions.get(event_name='new_pending_submissions').add_user_to_subscription(osf_user, 'email_transactional')
82+
83+
data = {}
84+
if subscribed_object := provider:
85+
data = {
86+
'object_id': subscribed_object.id,
87+
'content_type_id': ContentType.objects.get_for_model(subscribed_object).id,
88+
}
89+
90+
notification, created = NotificationSubscription.objects.get_or_create(
91+
user=osf_user,
92+
notification_type=self,
93+
**data,
94+
)
95+
return notification
8196

8297
osf_user.save()
8398

osf/apps.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
update_waffle_flags,
1616
update_default_providers
1717
)
18+
from osf.management.commands.populate_notification_types import populate_notification_types
1819

1920
logger = logging.getLogger(__file__)
2021

@@ -68,3 +69,7 @@ def ready(self):
6869
update_storage_regions,
6970
dispatch_uid='osf.apps.update_storage_regions'
7071
)
72+
post_migrate.connect(
73+
populate_notification_types,
74+
dispatch_uid='osf.apps.populate_notification_types'
75+
)

osf/management/commands/populate_notification_types.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import yaml
22
from django.apps import apps
3+
34
from website import settings
45

56
import logging
6-
from django.contrib.contenttypes.models import ContentType
7-
from osf.models.notification_type import NotificationType
87
from django.core.management.base import BaseCommand
98
from django.db import transaction
109

@@ -17,6 +16,8 @@
1716
}
1817

1918
def populate_notification_types(*args, **kwargs):
19+
from django.contrib.contenttypes.models import ContentType
20+
from osf.models.notification_type import NotificationType
2021

2122
with open(settings.NOTIFICATION_TYPES_YAML) as stream:
2223
notification_types = yaml.safe_load(stream)
@@ -41,6 +42,9 @@ def populate_notification_types(*args, **kwargs):
4142
elif object_content_type_model_name == 'osfuser':
4243
OSFUser = apps.get_model('osf', 'OSFUser')
4344
content_type = ContentType.objects.get_for_model(OSFUser)
45+
elif object_content_type_model_name == 'draftregistration':
46+
DraftRegistration = apps.get_model('osf', 'DraftRegistration')
47+
content_type = ContentType.objects.get_for_model(DraftRegistration)
4448
else:
4549
try:
4650
content_type = ContentType.objects.get(

osf/models/mixins.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from django.apps import apps
77
from django.contrib.auth.models import Group, AnonymousUser
8+
from django.contrib.contenttypes.models import ContentType
89
from django.core.exceptions import ObjectDoesNotExist, ValidationError
910
from django.db import models, transaction
1011
from django.utils import timezone
@@ -1079,6 +1080,8 @@ def add_to_group(self, user, group):
10791080

10801081
NotificationSubscription.objects.get_or_create(
10811082
user=user,
1083+
content_type=ContentType.objects.get_for_model(self),
1084+
object_id=self.id,
10821085
notification_type=NotificationType.objects.get(
10831086
name=NotificationType.Type.PROVIDER_NEW_PENDING_SUBMISSIONS
10841087
)
@@ -1096,11 +1099,6 @@ def remove_from_group(self, user, group, unsubscribe=True):
10961099

10971100
return _group.user_set.remove(user)
10981101

1099-
def add_user_to_subscription(self, user, subscription):
1100-
subscription.objects.get_or_create(
1101-
user=user,
1102-
)
1103-
11041102
def remove_user_from_subscription(self, user, subscription):
11051103
notification_type = NotificationType.objects.get(
11061104
name=subscription,

osf/models/notification_subscription.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from django.core.exceptions import ValidationError
77
from osf.models.notification_type import get_default_frequency_choices
88
from osf.models.notification import Notification
9+
from api.base import settings
910

1011
from .base import BaseModel
1112

@@ -70,15 +71,16 @@ def emit(
7071
to a test address or OSF desk support'
7172
email_context (dict, optional): Context for sending the email bcc, reply_to header etc
7273
"""
73-
logging.info(
74-
f"Attempting to create Notification:"
75-
f"\nto={getattr(self.user, 'username', destination_address)}"
76-
f"\ntype={self.notification_type.name}"
77-
f"\nmessage_frequency={self.message_frequency}"
78-
f"\ncontext={event_context}"
79-
f"\nemail={email_context}"
74+
if not settings.CI_ENV:
75+
logging.info(
76+
f"Attempting to create Notification:"
77+
f"\nto={getattr(self.user, 'username', destination_address)}"
78+
f"\ntype={self.notification_type.name}"
79+
f"\nmessage_frequency={self.message_frequency}"
80+
f"\nevent_context={event_context}"
81+
f"\nemail_context={email_context}"
8082

81-
)
83+
)
8284
if self.message_frequency == 'instantly':
8385
notification = Notification.objects.create(
8486
subscription=self,

osf/models/notification_type.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ class Type(str, Enum):
115115
PROVIDER_NEW_PENDING_WITHDRAW_REQUESTS = 'provider_new_pending_withdraw_requests'
116116
PROVIDER_REVIEWS_SUBMISSION_CONFIRMATION = 'provider_reviews_submission_confirmation'
117117
PROVIDER_REVIEWS_MODERATOR_SUBMISSION_CONFIRMATION = 'provider_reviews_moderator_submission_confirmation'
118-
PROVIDER_REVIEWS_WITHDRAWAL_REQUESTED = 'preprint_request_withdrawal_requested'
119118
PROVIDER_REVIEWS_REJECT_CONFIRMATION = 'provider_reviews_reject_confirmation'
120119
PROVIDER_REVIEWS_ACCEPT_CONFIRMATION = 'provider_reviews_accept_confirmation'
121120
PROVIDER_REVIEWS_RESUBMISSION_CONFIRMATION = 'provider_reviews_resubmission_confirmation'
@@ -235,27 +234,6 @@ def emit(
235234
email_context=email_context,
236235
)
237236

238-
def add_user_to_subscription(self, user, *args, **kwargs):
239-
"""
240-
"""
241-
from osf.models.notification_subscription import NotificationSubscription
242-
243-
provider = kwargs.pop('provider', None)
244-
node = kwargs.pop('node', None)
245-
data = {}
246-
if subscribed_object := provider or node:
247-
data = {
248-
'object_id': subscribed_object.id,
249-
'content_type_id': ContentType.objects.get_for_model(subscribed_object).id,
250-
}
251-
252-
notification, created = NotificationSubscription.objects.get_or_create(
253-
user=user,
254-
notification_type=self,
255-
**data,
256-
)
257-
return notification
258-
259237
def remove_user_from_subscription(self, user):
260238
"""
261239
"""

osf/models/provider.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import requests
33

44
from django.apps import apps
5-
from django.contrib.contenttypes.models import ContentType
65
from django.contrib.postgres import fields
76
from django.core.exceptions import ValidationError
87
from django.db import connection
@@ -16,7 +15,6 @@
1615

1716
from framework import sentry
1817
from osf.models.notification_type import NotificationType
19-
from osf.models.notification_subscription import NotificationSubscription
2018
from .base import BaseModel, TypedObjectIDMixin
2119
from .mixins import ReviewProviderMixin
2220
from .brand import Brand
@@ -473,19 +471,6 @@ def create_provider_auth_groups(sender, instance, created, **kwargs):
473471
instance.update_group_permissions()
474472

475473

476-
@receiver(post_save, sender=CollectionProvider)
477-
@receiver(post_save, sender=PreprintProvider)
478-
@receiver(post_save, sender=RegistrationProvider)
479-
def create_provider_notification_subscriptions(sender, instance, created, **kwargs):
480-
if created:
481-
for subscription in instance.DEFAULT_SUBSCRIPTIONS:
482-
NotificationSubscription.objects.get_or_create(
483-
notification_type__name=subscription,
484-
object_id=instance.id,
485-
content_type=ContentType.objects.get_for_model(instance)
486-
)
487-
488-
489474
@receiver(post_save, sender=CollectionProvider)
490475
def create_primary_collection_for_provider(sender, instance, created, **kwargs):
491476
if created:

osf/utils/notifications.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def notify_submit(resource, user, *args, **kwargs):
5050
timestamp=timezone.now(),
5151
context=context,
5252
resource=resource,
53+
5354
)
5455

5556

website/notifications/listeners.py

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ def subscribe_creator(resource):
2424
if user.is_registered:
2525
NotificationSubscription.objects.get_or_create(
2626
user=user,
27-
notification_type__name=NotificationType.Type.USER_FILE_UPDATED,
27+
notification_type=NotificationType.objects.get(name=NotificationType.Type.USER_FILE_UPDATED),
28+
_is_digest=True,
2829
)
2930
NotificationSubscription.objects.get_or_create(
3031
user=user,
31-
notification_type__name=NotificationType.Type.FILE_UPDATED,
32+
notification_type=NotificationType.objects.get(name=NotificationType.Type.FILE_UPDATED),
3233
object_id=resource.id,
33-
content_type=ContentType.objects.get_for_model(resource)
34+
content_type=ContentType.objects.get_for_model(resource),
35+
_is_digest=True,
3436
)
3537

3638
@contributor_added.connect
@@ -45,28 +47,25 @@ def subscribe_contributor(resource, contributor, auth=None, *args, **kwargs):
4547
if contributor.is_registered:
4648
NotificationSubscription.objects.get_or_create(
4749
user=contributor,
48-
notification_type__name=NotificationType.Type.USER_FILE_UPDATED,
50+
notification_type=NotificationType.objects.get(name=NotificationType.Type.USER_FILE_UPDATED),
51+
_is_digest=True,
4952
)
5053
NotificationSubscription.objects.get_or_create(
5154
user=contributor,
52-
notification_type__name=NotificationType.Type.FILE_UPDATED,
55+
notification_type=NotificationType.objects.get(name=NotificationType.Type.FILE_UPDATED),
5356
object_id=resource.id,
54-
content_type=ContentType.objects.get_for_model(resource)
57+
content_type=ContentType.objects.get_for_model(resource),
58+
_is_digest=True,
5559
)
5660

5761
@user_confirmed.connect
5862
def subscribe_confirmed_user(user):
5963
NotificationSubscription = apps.get_model('osf.NotificationSubscription')
6064
NotificationType = apps.get_model('osf.NotificationType')
61-
user_events = [
62-
NotificationType.Type.USER_FILE_UPDATED,
63-
NotificationType.Type.USER_REVIEWS,
64-
]
65-
for user_event in user_events:
66-
NotificationSubscription.objects.get_or_create(
67-
user=user,
68-
notification_type__name=user_event
69-
)
65+
NotificationSubscription.objects.get_or_create(
66+
user=user,
67+
notification_type=NotificationType.objects.get(name=NotificationType.Type.USER_FILE_UPDATED)
68+
)
7069

7170
@privacy_set_public.connect
7271
def queue_first_public_project_email(user, node):
@@ -94,8 +93,7 @@ def reviews_submit_notification_moderators(self, timestamp, context, resource):
9493
"""
9594

9695
# imports moved here to avoid AppRegistryNotReady error
97-
from osf.models import NotificationSubscription, NotificationType
98-
from django.contrib.contenttypes.models import ContentType
96+
from osf.models import NotificationType
9997
from website.settings import DOMAIN
10098

10199
provider = resource.provider
@@ -120,17 +118,15 @@ def reviews_submit_notification_moderators(self, timestamp, context, resource):
120118
context['message'] = f'resubmitted "{resource.title}".'
121119
else:
122120
context['message'] = f'submitted "{resource.title}".'
123-
provider_subscription, created = NotificationSubscription.objects.get_or_create(
124-
notification_type__name=NotificationType.Type.PROVIDER_NEW_PENDING_SUBMISSIONS,
125-
object_id=provider.id,
126-
content_type=ContentType.objects.get_for_model(provider.__class__),
127-
)
128-
for recipient in provider_subscription.subscribed_object.get_group('moderator').user_set.all():
121+
122+
for recipient in resource.provider.get_group('moderator').user_set.all():
129123
NotificationType.objects.get(
130124
name=NotificationType.Type.PROVIDER_NEW_PENDING_SUBMISSIONS
131125
).emit(
132126
user=recipient,
133-
event_context=context
127+
subscribed_object=provider,
128+
event_context=context,
129+
is_digest=True,
134130
)
135131

136132
# Handle email notifications to notify moderators of new submissions.

0 commit comments

Comments
 (0)