Skip to content

Commit 245016e

Browse files
authored
Merge pull request #11305 from Ostap-Zherebetskyi/fix/file_event_notifications
[ENG-8861] File Operations: Email content and subject not correct when file is renamed
2 parents 32c23f3 + 6672f2d commit 245016e

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

addons/base/views.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,10 +619,12 @@ def create_waterbutler_log(payload, **kwargs):
619619

620620
@file_signals.file_updated.connect
621621
def emit_notification(self, target, user, payload, *args, **kwargs):
622+
if not isinstance(target, Preprint):
623+
return
622624
notification_types = {
623625
'rename': NotificationType.Type.ADDON_FILE_RENAMED.instance,
624626
'copy': NotificationType.Type.ADDON_FILE_COPIED.instance,
625-
'create': NotificationType.Type.FILE_UPDATED.instance,
627+
'create': NotificationType.Type.FILE_ADDED.instance,
626628
'move': NotificationType.Type.ADDON_FILE_MOVED.instance,
627629
'delete': NotificationType.Type.FILE_REMOVED.instance,
628630
'update': NotificationType.Type.FILE_UPDATED.instance,

notifications/file_event_notifications.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,27 @@ def source_url(self):
261261

262262
@register(NodeLog.FILE_RENAMED)
263263
class AddonFileRenamed(ComplexFileEvent):
264+
def perform(self):
265+
'''
266+
Currently, WB sends the "move" action for renamed files.
267+
This code will remain useless until the correct action is sent.
268+
'''
269+
if self.node == self.source_node:
270+
super().perform()
271+
return
272+
273+
NotificationType.Type.ADDON_FILE_RENAMED.instance.emit(
274+
user=self.user,
275+
event_context={
276+
'user_fullname': self.user.fullname,
277+
'message': self.html_message,
278+
'profile_image_url': self.profile_image_url,
279+
'localized_timestamp': self.timestamp,
280+
'url': self.url,
281+
},
282+
is_digest=True,
283+
)
284+
264285
@property
265286
def html_message(self):
266287
return 'renamed {kind} "<b>{source_name}</b>" to "<b>{destination_name}</b>".'.format(
@@ -306,7 +327,7 @@ def perform(self):
306327
super().perform()
307328
return
308329

309-
NotificationType.Type.ADDON_FILE_MOVED.instance.emit(
330+
NotificationType.Type.ADDON_FILE_COPIED.instance.emit(
310331
user=self.user,
311332
event_context={
312333
'user_fullname': self.user.fullname,

tests/test_preprints.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2244,13 +2244,14 @@ def test_action_file_rename(self):
22442244
'kind': 'file',
22452245
},
22462246
)
2247-
with capture_notifications():
2247+
with capture_notifications() as notifications:
22482248
self.app.put(
22492249
url,
22502250
json=payload,
22512251
)
22522252
self.preprint.reload()
2253-
2253+
assert len(notifications['emits']) == 1
2254+
assert notifications['emits'][0]['type'] == NotificationType.Type.ADDON_FILE_RENAMED
22542255
assert self.preprint.logs.latest().action == 'osf_storage_addon_file_renamed'
22552256

22562257
def test_action_downloads_contrib(self):
@@ -2278,9 +2279,11 @@ def test_add_file_osfstorage_log(self):
22782279
url = self.preprint.api_url_for('create_waterbutler_log')
22792280
payload = self.build_payload(metadata={'nid': self.preprint._id, 'materialized': path, 'kind': 'file', 'path': path})
22802281
nlogs = self.preprint.logs.count()
2281-
with capture_notifications():
2282+
with capture_notifications() as notifications:
22822283
self.app.put(url, json=payload)
22832284
self.preprint.reload()
2285+
assert len(notifications['emits']) == 1
2286+
assert notifications['emits'][0]['type'] == NotificationType.Type.FILE_ADDED
22842287
assert self.preprint.logs.count() == nlogs + 1
22852288
assert ('urls' in self.preprint.logs.filter(action='osf_storage_file_added')[0].params)
22862289

@@ -2289,9 +2292,11 @@ def test_add_folder_osfstorage_log(self):
22892292
url = self.preprint.api_url_for('create_waterbutler_log')
22902293
payload = self.build_payload(metadata={'nid': self.preprint._id, 'materialized': path, 'kind': 'folder', 'path': path})
22912294
nlogs = self.preprint.logs.count()
2292-
with capture_notifications():
2295+
with capture_notifications() as notifications:
22932296
self.app.put(url, json=payload)
22942297
self.preprint.reload()
2298+
assert len(notifications['emits']) == 1
2299+
assert notifications['emits'][0]['type'] == NotificationType.Type.FILE_ADDED
22952300
assert self.preprint.logs.count() == nlogs + 1
22962301
assert ('urls' not in self.preprint.logs.filter(action='osf_storage_file_added')[0].params)
22972302

0 commit comments

Comments
 (0)