Skip to content

Commit e421342

Browse files
authored
Merge pull request #11395 from Ostap-Zherebetskyi/fix/NR_review_comments
[ENG-9162] fix NR review comments
2 parents 20b97ee + 0aad687 commit e421342

File tree

14 files changed

+136
-152
lines changed

14 files changed

+136
-152
lines changed

addons/boa/tasks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ async def submit_to_boa_async(host, username, password, user_guid, project_guid,
186186
NotificationType.Type.ADDONS_BOA_JOB_COMPLETE.instance.emit(
187187
user=user,
188188
event_context={
189-
'fullname': user.fullname,
189+
'user_fullname': user.fullname,
190190
'query_file_name': query_file_name,
191191
'query_file_full_path': file_full_path,
192192
'output_file_name': output_file_name,

addons/boa/tests/test_tasks.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ def setUp(self):
3939
self.output_file_name = 'fake_boa_script_results.txt'
4040
self.job_id = '1a2b3c4d5e6f7g8'
4141

42-
def tearDown(self):
43-
super().tearDown()
44-
4542
def test_boa_error_code(self):
4643
assert BoaErrorCode.NO_ERROR == -1
4744
assert BoaErrorCode.UNKNOWN == 0

addons/osfstorage/tests/test_models.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from framework.auth import Auth
1111
from addons.osfstorage.models import OsfStorageFile, OsfStorageFileNode, OsfStorageFolder
12-
from osf.models import BaseFileNode
12+
from osf.models import BaseFileNode, NotificationType
1313
from osf.exceptions import ValidationError
1414
from osf.utils.permissions import WRITE, ADMIN
1515

@@ -746,8 +746,11 @@ def test_after_fork_copies_versions(self, node, node_settings, auth_obj):
746746
version = factories.FileVersionFactory()
747747
record.add_version(version)
748748

749-
with capture_notifications():
749+
with capture_notifications() as notifications:
750750
fork = node.fork_node(auth_obj)
751+
752+
assert len(notifications['emits']) == 1
753+
assert notifications['emits'][0]['type'] == NotificationType.Type.NODE_CONTRIBUTOR_ADDED_DEFAULT
751754
fork_node_settings = fork.get_addon('osfstorage')
752755
fork_node_settings.reload()
753756

api/providers/tasks.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,7 @@ def bulk_upload_finish_job(upload, row_count, success_count, draft_errors, appro
646646
notification_type = NotificationType.Type.USER_REGISTRATION_BULK_UPLOAD_FAILURE_ALL
647647
else:
648648
logger.error(f'Unexpected job state for upload [{upload.id}]: {upload.state.name}')
649+
sentry.log_message(f'Unexpected job state for upload [{upload.id}]: {upload.state.name}')
649650
return
650651

651652
notification_type.instance.emit(

api_tests/crossref/views/test_crossref_email_response.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ def test_confirmation_marks_legacy_doi_as_deleted(self, app, url, preprint):
216216
update_xml = self.update_success_xml(preprint)
217217

218218
context_data = self.make_mailgun_payload(crossref_response=update_xml)
219-
app.post(url, context_data)
219+
with capture_notifications(expect_none=True):
220+
app.post(url, context_data)
220221

221222
assert preprint.identifiers.get(category='legacy_doi').deleted

api_tests/draft_registrations/views/test_draft_registration_contributor_list.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,8 @@ def test_add_unregistered_contributor_without_email_no_email(self, app, user, ur
315315
}
316316

317317
with capture_signals() as mock_signal:
318-
res = app.post_json_api(url, payload, auth=user.auth)
318+
with capture_notifications(expect_none=True):
319+
res = app.post_json_api(url, payload, auth=user.auth)
319320
assert contributor_added in mock_signal.signals_sent()
320321
assert res.status_code == 201
321322

api_tests/institutions/views/test_institution_auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ def test_user_inactive(self, app, institution, url_auth_institution):
410410

411411
username, fullname, password = '[email protected]', 'Foo Bar', 'FuAsKeEr'
412412
user = make_user(username, fullname)
413-
with capture_notifications() as mock_signals:
413+
with capture_notifications():
414414
user.set_password(password)
415415
# User must be saved before deactivation
416416
user.save()

notifications.yaml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,6 @@ notification_types:
193193
template: 'website/templates/storage_cap_exceeded_announcement.html.mako'
194194
tests: []
195195

196-
- name: user_add_sso_email_osf4i
197-
subject: 'Your OSF Account Email Address'
198-
__docs__: ...
199-
object_content_type_model_name: osfuser
200-
template: 'website/templates/add_sso_email_osf4i.html.mako'
201-
tests: ['']
202-
203196
- name: user_duplicate_accounts_sso_osf4i
204197
subject: 'Duplicate Account Detection'
205198
__docs__: ...

notifications/listeners.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,22 @@ def subscribe_contributor(resource, contributor, auth=None, *args, **kwargs):
4444
return None
4545

4646
if contributor.is_registered:
47-
NotificationSubscription.objects.get_or_create(
48-
user=contributor,
49-
notification_type=NotificationType.Type.USER_FILE_UPDATED.instance,
50-
object_id=contributor.id,
51-
content_type=ContentType.objects.get_for_model(contributor),
52-
_is_digest=True,
53-
)
54-
NotificationSubscription.objects.get_or_create(
55-
user=contributor,
56-
notification_type=NotificationType.Type.FILE_UPDATED.instance,
57-
object_id=resource.id,
58-
content_type=ContentType.objects.get_for_model(resource),
59-
_is_digest=True,
60-
)
47+
NotificationSubscription.objects.bulk_create([
48+
NotificationSubscription(
49+
user=contributor,
50+
notification_type=NotificationType.Type.USER_FILE_UPDATED.instance,
51+
object_id=contributor.id,
52+
content_type=ContentType.objects.get_for_model(contributor),
53+
_is_digest=True,
54+
),
55+
NotificationSubscription(
56+
user=contributor,
57+
notification_type=NotificationType.Type.FILE_UPDATED.instance,
58+
object_id=resource.id,
59+
content_type=ContentType.objects.get_for_model(resource),
60+
_is_digest=True,
61+
)
62+
], ignore_conflicts=True)
6163

6264

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

notifications/tasks.py

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,41 @@
1919

2020
logger = get_task_logger(__name__)
2121

22-
@celery_app.task(bind=True)
23-
def send_user_email_task(self, user_id, notification_ids, **kwargs):
22+
23+
def get_user_and_email_task(task_id, user_id):
24+
"""Helper to safely fetch user and initialize EmailTask."""
2425
try:
2526
user = OSFUser.objects.get(
2627
guids___id=user_id,
2728
deleted__isnull=True
2829
)
2930
except OSFUser.DoesNotExist:
3031
logger.error(f'OSFUser with id {user_id} does not exist')
31-
email_task, _ = EmailTask.objects.get_or_create(task_id=self.request.id)
32+
email_task, _ = EmailTask.objects.get_or_create(task_id=task_id)
3233
email_task.status = 'NO_USER_FOUND'
3334
email_task.error_message = 'User not found or disabled'
3435
email_task.save()
36+
return None, email_task
37+
38+
email_task, _ = EmailTask.objects.get_or_create(task_id=task_id)
39+
email_task.user = user
40+
email_task.status = 'STARTED'
41+
42+
if user.is_disabled:
43+
email_task.status = 'USER_DISABLED'
44+
email_task.error_message = 'User not found or disabled'
45+
email_task.save()
46+
return None, email_task
47+
48+
return user, email_task
49+
50+
@celery_app.task(bind=True)
51+
def send_user_email_task(self, user_id, notification_ids, **kwargs):
52+
user, email_task = get_user_and_email_task(self.request.id, user_id)
53+
if not user:
3554
return
3655

3756
try:
38-
email_task, _ = EmailTask.objects.get_or_create(task_id=self.request.id)
39-
email_task.user = user
40-
email_task.status = 'STARTED'
41-
if user.is_disabled:
42-
email_task.status = 'USER_DISABLED'
43-
email_task.error_message = 'User not found or disabled'
44-
email_task.save()
45-
return
46-
4757
notifications_qs = Notification.objects.filter(id__in=notification_ids)
4858
rendered_notifications = [n.render() for n in notifications_qs]
4959

@@ -90,29 +100,11 @@ def send_user_email_task(self, user_id, notification_ids, **kwargs):
90100

91101
@celery_app.task(bind=True)
92102
def send_moderator_email_task(self, user_id, provider_id, notification_ids, **kwargs):
93-
try:
94-
user = OSFUser.objects.get(
95-
guids___id=user_id,
96-
deleted__isnull=True
97-
)
98-
except OSFUser.DoesNotExist:
99-
logger.error(f'OSFUser with id {user_id} does not exist')
100-
email_task, _ = EmailTask.objects.get_or_create(task_id=self.request.id)
101-
email_task.status = 'NO_USER_FOUND'
102-
email_task.error_message = 'User not found or disabled'
103-
email_task.save()
103+
user, email_task = get_user_and_email_task(self.request.id, user_id)
104+
if not user:
104105
return
105106

106107
try:
107-
email_task, _ = EmailTask.objects.get_or_create(task_id=self.request.id)
108-
email_task.user = user
109-
email_task.status = 'STARTED'
110-
if user.is_disabled:
111-
email_task.status = 'USER_DISABLED'
112-
email_task.error_message = 'User not found or disabled'
113-
email_task.save()
114-
return
115-
116108
notifications_qs = Notification.objects.filter(id__in=notification_ids)
117109
rendered_notifications = [notification.render() for notification in notifications_qs]
118110

@@ -302,10 +294,7 @@ def remove_supplemental_node_from_preprints(node_id):
302294
AbstractNode = apps.get_model('osf.AbstractNode')
303295

304296
node = AbstractNode.load(node_id)
305-
for preprint in node.preprints.all():
306-
if preprint.node is not None:
307-
preprint.node = None
308-
preprint.save()
297+
node.preprints.filter(node__isnull=False).update(node=None)
309298

310299

311300
@run_postcommit(once_per_request=False, celery=True)

0 commit comments

Comments
 (0)