Skip to content

Commit 930cb6c

Browse files
committed
refactored to send main via Notification model
1 parent 37b419a commit 930cb6c

File tree

2 files changed

+46
-19
lines changed

2 files changed

+46
-19
lines changed

api/requests/serializers.py

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
PreprintRequest,
1616
Institution,
1717
OSFUser,
18+
NotificationType,
1819
)
20+
from osf.models.notification import FrequencyChoices
1921
from osf.utils.workflows import DefaultStates, RequestTypes, NodeRequestTypes
2022
from osf.utils import permissions as osf_permissions
2123
from website import language, settings
22-
from website.mails import send_mail, NODE_REQUEST_INSTITUTIONAL_ACCESS_REQUEST
24+
from website.mails import NODE_REQUEST_INSTITUTIONAL_ACCESS_REQUEST
2325

2426
from rest_framework.exceptions import PermissionDenied, ValidationError
2527

@@ -188,20 +190,38 @@ def make_node_institutional_access_request(self, node, validated_data) -> NodeRe
188190

189191
comment = validated_data.get('comment', '').strip() or language.EMPTY_REQUEST_INSTITUTIONAL_ACCESS_REQUEST_TEXT
190192

191-
send_mail(
192-
to_addr=recipient.username,
193-
mail=NODE_REQUEST_INSTITUTIONAL_ACCESS_REQUEST,
193+
notification_type_name = NotificationType.Type.NODE_REQUEST_INSTITUTIONAL_ACCESS_REQUEST.value
194+
notification_type, created = NotificationType.objects.get_or_create(
195+
name=notification_type_name,
196+
defaults={
197+
'notification_freq': FrequencyChoices.INSTANTLY.value,
198+
'template': NODE_REQUEST_INSTITUTIONAL_ACCESS_REQUEST
199+
}
200+
)
201+
event_context = {
202+
'recipient': {
203+
'fullname': recipient.full_name,
204+
},
205+
'sender': {
206+
'absolute_url': sender.get_absolute_url(),
207+
'full_name': sender.full_name,
208+
},
209+
'node':{
210+
'absolute_url': node.absolute_url,
211+
'title': node.title,
212+
},
213+
'comment': comment,
214+
'settings': {
215+
'DOMAIN': settings.DOMAIN,
216+
'OSF_CONTACT_EMAIL': settings.OSF_CONTACT_EMAIL,
217+
},
218+
}
219+
220+
notification_type.emit(
194221
user=recipient,
195-
sender=sender,
196-
bcc_addr=[sender.username] if validated_data['bcc_sender'] else None,
197-
reply_to=sender.username if validated_data['reply_to'] else None,
198-
recipient=recipient,
199-
comment=comment,
200-
institution=institution,
201-
osf_url=settings.DOMAIN,
202-
node=node_request.target,
222+
subscribed_object=node_request.target,
223+
event_context=event_context
203224
)
204-
205225
return node_request
206226

207227
def _create_node_request(self, node, validated_data) -> NodeRequest:

osf/utils/machines.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from framework.auth import Auth
77

88
from osf.exceptions import InvalidTransitionError
9+
from osf.models.notification import FrequencyChoices, NotificationType
910
from osf.models.preprintlog import PreprintLog
1011
from osf.models.action import ReviewAction, NodeRequestAction, PreprintRequestAction
1112
from osf.utils import permissions
@@ -240,12 +241,18 @@ def notify_submit(self, ev):
240241
context['project_settings_url'] = f'{self.machineable.target.absolute_url}settings/'
241242
if not self.machineable.request_type == NodeRequestTypes.INSTITUTIONAL_REQUEST.value:
242243
for admin in self.machineable.target.get_users_with_perm(permissions.ADMIN):
243-
mails.send_mail(
244-
admin.username,
245-
mails.ACCESS_REQUEST_SUBMITTED,
246-
admin=admin,
247-
osf_contact_email=OSF_CONTACT_EMAIL,
248-
**context
244+
notification_type_name = NotificationType.Type.NODE_REQUEST_ACCESS_SUBMITTED.value
245+
notification_type, created = NotificationType.objects.get_or_create(
246+
name=notification_type_name,
247+
defaults={
248+
'notification_freq': FrequencyChoices.INSTANTLY.value,
249+
'template': mails.ACCESS_REQUEST_SUBMITTED
250+
}
251+
)
252+
event_context = dict(**context)
253+
notification_type.emit(
254+
user=admin.username,
255+
event_context=event_context
249256
)
250257

251258
def notify_resubmit(self, ev):

0 commit comments

Comments
 (0)