Skip to content

Commit a6e3ee8

Browse files
committed
update the file operations to use new notifications system
1 parent c1bfdf3 commit a6e3ee8

File tree

5 files changed

+176
-104
lines changed

5 files changed

+176
-104
lines changed

addons/base/views.py

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -576,25 +576,24 @@ def create_waterbutler_log(payload, **kwargs):
576576
params=payload
577577
)
578578

579-
if payload.get('email') is True:
580-
notification_type = NotificationType.Type.FILE_OPERATION_SUCCESS
581-
elif payload.get('errors'):
582-
notification_type = NotificationType.Type.FILE_OPERATION_FAILED
583-
else:
584-
raise NotImplementedError('No email template for this')
585-
586-
NotificationType.objects.get(name=notification_type.value).emit(
587-
user=user,
588-
event_context={
589-
'action': payload['action'],
590-
'source_node': source_node,
591-
'destination_node': destination_node,
592-
'source_path': payload['source']['materialized'],
593-
'source_addon': payload['source']['addon'],
594-
'destination_addon': payload['destination']['addon'],
595-
'osf_support_email': settings.OSF_SUPPORT_EMAIL
596-
}
597-
)
579+
if payload.get('email') or payload.get('errors'):
580+
if payload.get('email'):
581+
notification_type = NotificationType.Type.FILE_OPERATION_SUCCESS
582+
if payload.get('errors'):
583+
notification_type = NotificationType.Type.FILE_OPERATION_FAILED
584+
585+
NotificationType.objects.get(name=notification_type.value).emit(
586+
user=user,
587+
event_context={
588+
'action': payload['action'],
589+
'source_node': source_node,
590+
'destination_node': destination_node,
591+
'source_path': payload['source']['materialized'],
592+
'source_addon': payload['source']['addon'],
593+
'destination_addon': payload['destination']['addon'],
594+
'osf_support_email': settings.OSF_SUPPORT_EMAIL
595+
}
596+
)
598597
if payload.get('errors'):
599598
# Action failed but our function succeeded
600599
# Bail out to avoid file_signals
@@ -608,27 +607,35 @@ def create_waterbutler_log(payload, **kwargs):
608607
if target_node and payload['action'] != 'download_file':
609608
update_storage_usage_with_size(payload)
610609

611-
with transaction.atomic():
612-
f_type, item_action = action.split('_')
613-
if payload['metadata']['materialized'].endswith('/'):
614-
f_type = 'folder'
615-
match f'node_{action}':
616-
case NotificationType.Type.NODE_FILE_ADDED:
617-
NotificationType.objects.get(
618-
name=NotificationType.Type.NODE_FILE_ADDED
619-
).emit(
620-
user=user,
621-
event_context={
622-
'message': f'{markupsafe.escape(item_action)} {markupsafe.escape(f_type)} "'
623-
f'<b>{markupsafe.escape(payload['metadata']['materialized'].lstrip('/'))}</b>".',
624-
'profile_image_url': user.profile_image_url(),
625-
'localized_timestamp': localize_timestamp(timezone.now(), user),
626-
'user_fullname': user.fullname,
627-
'url': node.absolute_url,
628-
}
629-
)
630-
case _:
631-
raise NotImplementedError(f'action {action} not implemented')
610+
file_signals.file_updated.send(target=node, user=user, event_type=action, payload=payload)
611+
612+
match f'node_{action}':
613+
case NotificationType.Type.NODE_FILE_ADDED:
614+
notification = NotificationType.objects.get(name=NotificationType.Type.NODE_FILE_ADDED)
615+
case NotificationType.Type.NODE_FILE_REMOVED:
616+
notification = NotificationType.objects.get(name=NotificationType.Type.NODE_FILE_REMOVED)
617+
case NotificationType.Type.NODE_FILE_UPDATED:
618+
notification = NotificationType.objects.get(name=NotificationType.Type.NODE_FILE_UPDATED)
619+
case NotificationType.Type.NODE_ADDON_FILE_RENAMED:
620+
notification = NotificationType.objects.get(name=NotificationType.Type.NODE_ADDON_FILE_RENAMED)
621+
case NotificationType.Type.NODE_ADDON_FILE_COPIED:
622+
notification = NotificationType.objects.get(name=NotificationType.Type.NODE_ADDON_FILE_COPIED)
623+
case NotificationType.Type.NODE_ADDON_FILE_REMOVED:
624+
notification = NotificationType.objects.get(name=NotificationType.Type.NODE_ADDON_FILE_REMOVED)
625+
case NotificationType.Type.NODE_ADDON_FILE_MOVED:
626+
notification = NotificationType.objects.get(name=NotificationType.Type.NODE_ADDON_FILE_MOVED)
627+
case _:
628+
raise NotImplementedError(f'action {action} not implemented')
629+
630+
notification.emit(
631+
user=user,
632+
event_context={
633+
'profile_image_url': user.profile_image_url(),
634+
'localized_timestamp': localize_timestamp(timezone.now(), user),
635+
'user_fullname': user.fullname,
636+
'url': node.absolute_url,
637+
}
638+
)
632639

633640
return {'status': 'success'}
634641

api/nodes/views.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,11 @@
152152
File,
153153
Folder,
154154
CedarMetadataRecord,
155-
Preprint, Collection,
155+
Preprint, Collection, NotificationType,
156156
)
157157
from addons.osfstorage.models import Region
158158
from osf.utils.permissions import ADMIN, WRITE_NODE
159-
from website import mails, settings
159+
from website import settings
160160

161161
# This is used to rethrow v1 exceptions as v2
162162
HTTP_CODE_MAP = {
@@ -1045,11 +1045,28 @@ def perform_create(self, serializer):
10451045
try:
10461046
fork = serializer.save(node=node)
10471047
except Exception as exc:
1048-
mails.send_mail(user.email, mails.FORK_FAILED, title=node.title, guid=node._id, can_change_preferences=False)
1048+
NotificationType.objects.get(
1049+
name=NotificationType.Type.NODE_FORK_FAILED,
1050+
).emit(
1051+
user=user,
1052+
event_context={
1053+
'guid': node._id,
1054+
'title': node._id,
1055+
'can_change_preferences': False,
1056+
},
1057+
)
10491058
raise exc
10501059
else:
1051-
mails.send_mail(user.email, mails.FORK_COMPLETED, title=node.title, guid=fork._id, can_change_preferences=False)
1052-
1060+
NotificationType.objects.get(
1061+
name=NotificationType.Type.NODE_FORK_COMPLETED,
1062+
).emit(
1063+
user=user,
1064+
event_context={
1065+
'guid': fork._id,
1066+
'title': node._id,
1067+
'can_change_preferences': False,
1068+
},
1069+
)
10531070

10541071
class NodeLinkedByNodesList(JSONAPIBaseView, generics.ListAPIView, NodeMixin):
10551072
permission_classes = (

notifications.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,31 @@ notification_types:
121121
object_content_type_model_name: abstractnode
122122
template: 'website/templates/emails/file_updated.html.mako'
123123
notification_freq_default: instantly
124+
- name: node_file_removed
125+
__docs__: ...
126+
object_content_type_model_name: abstractnode
127+
template: 'website/templates/emails/file_updated.html.mako'
128+
notification_freq_default: instantly
129+
- name: node_addon_file_renamed
130+
__docs__: ...
131+
object_content_type_model_name: abstractnode
132+
template: 'website/templates/emails/file_updated.html.mako'
133+
notification_freq_default: instantly
134+
- name: node_addon_file_copied
135+
__docs__: ...
136+
object_content_type_model_name: abstractnode
137+
template: 'website/templates/emails/file_updated.html.mako'
138+
notification_freq_default: instantly
139+
- name: node_addon_file_moved
140+
__docs__: ...
141+
object_content_type_model_name: abstractnode
142+
template: 'website/templates/emails/file_updated.html.mako'
143+
notification_freq_default: instantly
144+
- name: node_addon_file_removed
145+
__docs__: ...
146+
object_content_type_model_name: abstractnode
147+
template: 'website/templates/emails/file_updated.html.mako'
148+
notification_freq_default: instantly
124149
- name: node_wiki_updated
125150
__docs__: ...
126151
object_content_type_model_name: abstractnode

osf/models/notification_type.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ class Type(str, Enum):
5959
USER_ARCHIVE_JOB_EXCEEDED = 'user_archive_job_exceeded'
6060
USER_ARCHIVE_JOB_COPY_ERROR = 'user_archive_job_copy_error'
6161
USER_ARCHIVE_JOB_FILE_NOT_FOUND = 'user_archive_job_file_not_found'
62-
USER_ARCHIVE_JOB_UNCAUGHT_ERROR = 'user_archive_job_uncaught_error'
6362
USER_COMMENT_REPLIES = 'user_comment_replies'
6463
USER_COMMENTS = 'user_comments'
6564
USER_FILE_UPDATED = 'user_file_updated'
@@ -101,6 +100,11 @@ class Type(str, Enum):
101100
NODE_PENDING_EMBARGO_TERMINATION_ADMIN = 'node_pending_embargo_termination_admin'
102101
NODE_FILE_UPDATED = 'node_file_updated'
103102
NODE_FILE_ADDED = 'node_file_added'
103+
NODE_FILE_REMOVED = 'node_file_removed'
104+
NODE_ADDON_FILE_COPIED = 'node_addon_file_copied'
105+
NODE_ADDON_FILE_RENAMED = 'node_addon_file_renamed'
106+
NODE_ADDON_FILE_MOVED = 'node_addon_file_moved'
107+
NODE_ADDON_FILE_REMOVED = 'node_addon_file_removed'
104108

105109
# Provider notifications
106110
PROVIDER_NEW_PENDING_SUBMISSIONS = 'provider_new_pending_submissions'

website/archiver/utils.py

Lines changed: 77 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55
from django.db.models import CharField, OuterRef, Subquery
66
from framework.auth import Auth
77
from framework.utils import sanitize_html
8+
from osf.models import NotificationType
89

9-
from website import (
10-
mails,
11-
settings
12-
)
10+
from website import settings
1311
from website.archiver import (
1412
StatResult, AggregateStatResult,
1513
ARCHIVER_NETWORK_ERROR,
@@ -29,79 +27,100 @@ def normalize_unicode_filenames(filename):
2927

3028

3129
def send_archiver_size_exceeded_mails(src, user, stat_result, url):
32-
mails.send_mail(
33-
to_addr=settings.OSF_SUPPORT_EMAIL,
34-
mail=mails.ARCHIVE_SIZE_EXCEEDED_DESK,
35-
user=user,
36-
src=src,
37-
stat_result=stat_result,
38-
can_change_preferences=False,
39-
url=url,
30+
NotificationType.objects.get(
31+
name=NotificationType.Type.DESK_ARCHIVE_JOB_EXCEEDED
32+
).emit(
33+
event_context={
34+
'user': user.id,
35+
'src': src._id,
36+
'stat_result': stat_result,
37+
'url': url,
38+
'can_change_preferences': False,
39+
}
4040
)
41-
mails.send_mail(
42-
to_addr=user.username,
43-
mail=mails.ARCHIVE_SIZE_EXCEEDED_USER,
41+
NotificationType.objects.get(
42+
name=NotificationType.Type.USER_ARCHIVE_JOB_EXCEEDED,
43+
).emit(
4444
user=user,
45-
src=src,
46-
can_change_preferences=False,
45+
event_context={
46+
'user': user,
47+
'src': src,
48+
'can_change_preferences': False,
49+
}
4750
)
4851

4952

5053
def send_archiver_copy_error_mails(src, user, results, url):
51-
mails.send_mail(
52-
to_addr=settings.OSF_SUPPORT_EMAIL,
53-
mail=mails.ARCHIVE_COPY_ERROR_DESK,
54+
NotificationType.objects.get(
55+
name=NotificationType.Type.DESK_ARCHIVE_JOB_COPY_ERROR
56+
).emit(
5457
user=user,
55-
src=src,
56-
results=results,
57-
url=url,
58-
can_change_preferences=False,
58+
event_context={
59+
'user': user.id,
60+
'src': src._id,
61+
'results': results,
62+
'url': url,
63+
'can_change_preferences': False,
64+
}
5965
)
60-
mails.send_mail(
61-
to_addr=user.username,
62-
mail=mails.ARCHIVE_COPY_ERROR_USER,
66+
NotificationType.objects.get(
67+
name=NotificationType.Type.USER_ARCHIVE_JOB_COPY_ERROR
68+
).emit(
6369
user=user,
64-
src=src,
65-
results=results,
66-
can_change_preferences=False,
70+
event_context={
71+
'user': user.id,
72+
'src': src._id,
73+
'results': results,
74+
'can_change_preferences': False,
75+
}
6776
)
6877

6978
def send_archiver_file_not_found_mails(src, user, results, url):
70-
mails.send_mail(
71-
to_addr=settings.OSF_SUPPORT_EMAIL,
72-
mail=mails.ARCHIVE_FILE_NOT_FOUND_DESK,
73-
can_change_preferences=False,
74-
user=user,
75-
src=src,
76-
results=results,
77-
url=url,
79+
NotificationType.objects.get(
80+
name=NotificationType.Type.DESK_ARCHIVE_JOB_FILE_NOT_FOUND
81+
).emit(
82+
event_context={
83+
'user': user.id,
84+
'src': src._id,
85+
'results': results,
86+
'url': url,
87+
'can_change_preferences': False,
88+
}
7889
)
79-
mails.send_mail(
80-
to_addr=user.username,
81-
mail=mails.ARCHIVE_FILE_NOT_FOUND_USER,
90+
NotificationType.objects.get(
91+
name=NotificationType.Type.USER_ARCHIVE_JOB_FILE_NOT_FOUND
92+
).emit(
8293
user=user,
83-
src=src,
84-
results=results,
85-
can_change_preferences=False,
94+
event_context={
95+
'user': user.id,
96+
'src': src._id,
97+
'results': results,
98+
'can_change_preferences': False,
99+
}
86100
)
87101

88102
def send_archiver_uncaught_error_mails(src, user, results, url):
89-
mails.send_mail(
90-
to_addr=settings.OSF_SUPPORT_EMAIL,
91-
mail=mails.ARCHIVE_UNCAUGHT_ERROR_DESK,
92-
user=user,
93-
src=src,
94-
results=results,
95-
can_change_preferences=False,
96-
url=url,
103+
NotificationType.objects.get(
104+
name=NotificationType.Type.DESK_ARCHIVE_JOB_UNCAUGHT_ERROR
105+
).emit(
106+
event_context={
107+
'user': user.id,
108+
'src': src._id,
109+
'results': results,
110+
'url': url,
111+
'can_change_preferences': False,
112+
}
97113
)
98-
mails.send_mail(
99-
to_addr=user.username,
100-
mail=mails.ARCHIVE_UNCAUGHT_ERROR_USER,
114+
NotificationType.objects.get(
115+
name=NotificationType.Type.USER_ARCHIVE_JOB_UNCAUGHT_ERROR
116+
).emit(
101117
user=user,
102-
src=src,
103-
results=results,
104-
can_change_preferences=False,
118+
event_context={
119+
'user': user.id,
120+
'src': src._id,
121+
'results': results,
122+
'can_change_preferences': False,
123+
}
105124
)
106125

107126

0 commit comments

Comments
 (0)