Skip to content

Commit ad6ebc7

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

6 files changed

Lines changed: 22 additions & 34 deletions

File tree

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
from website import settings
44

55
import logging
6-
from django.contrib.contenttypes.models import ContentType
7-
from osf.models.notification_type import NotificationType
86
from django.core.management.base import BaseCommand
97
from django.db import transaction
108

@@ -17,6 +15,8 @@
1715
}
1816

1917
def populate_notification_types(*args, **kwargs):
18+
from django.contrib.contenttypes.models import ContentType
19+
from osf.models.notification_type import NotificationType
2020

2121
with open(settings.NOTIFICATION_TYPES_YAML) as stream:
2222
notification_types = yaml.safe_load(stream)

osf/models/mixins.py

Lines changed: 3 additions & 0 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
)

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:

website/notifications/listeners.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ 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),
2828
)
2929
NotificationSubscription.objects.get_or_create(
3030
user=user,
31-
notification_type__name=NotificationType.Type.FILE_UPDATED,
31+
notification_type=NotificationType.objects.get(name=NotificationType.Type.FILE_UPDATED),
3232
object_id=resource.id,
3333
content_type=ContentType.objects.get_for_model(resource)
3434
)
@@ -45,11 +45,11 @@ def subscribe_contributor(resource, contributor, auth=None, *args, **kwargs):
4545
if contributor.is_registered:
4646
NotificationSubscription.objects.get_or_create(
4747
user=contributor,
48-
notification_type__name=NotificationType.Type.USER_FILE_UPDATED,
48+
notification_type=NotificationType.objects.get(name=NotificationType.Type.USER_FILE_UPDATED),
4949
)
5050
NotificationSubscription.objects.get_or_create(
5151
user=contributor,
52-
notification_type__name=NotificationType.Type.FILE_UPDATED,
52+
notification_type=NotificationType.objects.get(name=NotificationType.Type.FILE_UPDATED),
5353
object_id=resource.id,
5454
content_type=ContentType.objects.get_for_model(resource)
5555
)
@@ -58,15 +58,10 @@ def subscribe_contributor(resource, contributor, auth=None, *args, **kwargs):
5858
def subscribe_confirmed_user(user):
5959
NotificationSubscription = apps.get_model('osf.NotificationSubscription')
6060
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-
)
61+
NotificationSubscription.objects.get_or_create(
62+
user=user,
63+
notification_type=NotificationType.objects.get(name=NotificationType.Type.USER_FILE_UPDATED)
64+
)
7065

7166
@privacy_set_public.connect
7267
def queue_first_public_project_email(user, node):
@@ -121,7 +116,7 @@ def reviews_submit_notification_moderators(self, timestamp, context, resource):
121116
else:
122117
context['message'] = f'submitted "{resource.title}".'
123118
provider_subscription, created = NotificationSubscription.objects.get_or_create(
124-
notification_type__name=NotificationType.Type.PROVIDER_NEW_PENDING_SUBMISSIONS,
119+
notification_type=NotificationType.objects.get(name=NotificationType.Type.PROVIDER_NEW_PENDING_SUBMISSIONS),
125120
object_id=provider.id,
126121
content_type=ContentType.objects.get_for_model(provider.__class__),
127122
)

website/reviews/listeners.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def reviews_withdraw_requests_notification_moderators(self, timestamp, context,
1010
from osf.models import NotificationSubscription, NotificationType
1111

1212
provider_subscription, _ = NotificationSubscription.objects.get_or_create(
13-
notification_type__name=NotificationType.Type.PROVIDER_REVIEWS_WITHDRAWAL_REQUESTED,
13+
notification_type=NotificationType.objects.get(name=NotificationType.Type.PROVIDER_REVIEWS_WITHDRAWAL_REQUESTED),
1414
object_id=provider.id,
1515
content_type=ContentType.objects.get_for_model(provider.__class__),
1616
)
@@ -35,7 +35,7 @@ def reviews_withdrawal_requests_notification(self, timestamp, context):
3535
from osf.models import NotificationSubscription, NotificationType
3636

3737
provider_subscription, _ = NotificationSubscription.objects.get_or_create(
38-
notification_type__name=NotificationType.Type.PROVIDER_NEW_PENDING_SUBMISSIONS,
38+
notification_type=NotificationType.objects.get(name=NotificationType.Type.PROVIDER_NEW_PENDING_SUBMISSIONS),
3939
object_id=preprint.provider.id,
4040
content_type=ContentType.objects.get_for_model(preprint.provider.__class__),
4141
)
@@ -87,7 +87,7 @@ def reviews_submit_notification_moderators(self, timestamp, resource, context):
8787

8888
# Get NotificationSubscription instance, which contains reference to all subscribers
8989
provider_subscription, created = NotificationSubscription.objects.get_or_create(
90-
notification_type__name=NotificationType.Type.PROVIDER_NEW_PENDING_SUBMISSIONS,
90+
notification_type=NotificationType.objects.get(name=NotificationType.Type.PROVIDER_NEW_PENDING_SUBMISSIONS),
9191
object_id=provider.id,
9292
content_type=ContentType.objects.get_for_model(provider.__class__),
9393
)

0 commit comments

Comments
 (0)