Skip to content

Commit 663c0ac

Browse files
committed
#49 #50 Re-wrote the email sending module with a class and better organization of templates.
1 parent d4c5a90 commit 663c0ac

File tree

10 files changed

+112
-29
lines changed

10 files changed

+112
-29
lines changed

src/cvinterface/email.py

+88-25
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Type, Dict
1+
from typing import Dict
22

33
from django.conf import settings
44
from django.core.handlers.wsgi import WSGIRequest
@@ -9,47 +9,110 @@
99

1010
from odm2cvs.controlled_vocabularies import Vocabulary
1111

12-
template_map: Dict[str, str] = {
13-
'update_subject': 'cvinterface/email/update_request_subject.tpl',
14-
'approved': 'cvinterface/email/update_approved_body.tpl',
15-
'rejected': 'cvinterface/email/update_rejected_body.tpl'
12+
email_map: Dict[str, Dict[str, Dict[str, str]]] = {
13+
'request_made': {
14+
'admin': {
15+
'subject': 'cvinterface/email/request_made/subject_admin.tpl',
16+
'body': 'cvinterface/email/request_made/body_admin.tpl'
17+
},
18+
'user': {
19+
'subject': 'cvinterface/email/request_made/subject_user.tpl',
20+
'body': 'cvinterface/email/request_made/body_user.tpl'
21+
}
22+
},
23+
'update_approved': {
24+
'user': {
25+
'subject': 'cvinterface/email/update_approved/subject.tpl',
26+
'body': 'cvinterface/email/update_approved/body.tpl'
27+
}
28+
},
29+
'update_rejected': {
30+
'user': {
31+
'subject': 'cvinterface/email/update_rejected/subject.tpl',
32+
'body': 'cvinterface/email/update_rejected/body.tpl'
33+
}
34+
}
1635
}
1736

1837

19-
def get_email_context(web_form: Form, web_request: WSGIRequest, vocabulary: Vocabulary) -> Dict[str, str]:
20-
return {
21-
'submitter_name': web_form.instance.submitter_name,
22-
'concept_name': web_form.instance.name,
23-
'vocabulary_verbose_name': vocabulary.get('name'),
24-
'vocabulary_list_url': web_request.build_absolute_uri(reverse(vocabulary.get('list_url_name')))
25-
}
38+
class EmailHandler:
39+
def __init__(self, web_form: Form, web_request: WSGIRequest, vocabulary: Vocabulary, email_type: str, receiver: str):
40+
self.form = web_form
41+
self.web_request = web_request
42+
self.vocabulary = vocabulary
43+
self.email_type = email_type
44+
self.email_receiver = receiver
45+
self.recipients = {
46+
'user': [self.form.instance.submitter_email],
47+
'admin': settings.EMAIL_RECIPIENTS
48+
}
49+
50+
def get_email_context(self) -> Dict[str, str]:
51+
return {
52+
'submitter_name': self.form.instance.submitter_name,
53+
'submitter_email': self.form.instance.submitter_email,
54+
'concept_name': self.form.instance.name,
55+
'concept_term': self.form.instance.term,
56+
'concept_notes': self.form.instance.note,
57+
'concept_definition': self.form.instance.definition,
58+
'request_reason': self.form.instance.request_reason,
59+
'vocabulary_verbose_name': self.vocabulary.get('name'),
60+
'action': 'creation of a new ' if not self.form.instance.request_for_id else 'update of a ',
61+
'vocabulary_list_url': self.web_request.build_absolute_uri(reverse(self.vocabulary.get('list_url_name'))),
62+
'requests_list_url': self.web_request.build_absolute_uri(reverse('requests_list'))
63+
}
64+
65+
def send_email(self):
66+
context: Dict[str, str] = self.get_email_context()
67+
email: Dict[str, str] = email_map.get(self.email_type).get(self.email_receiver)
68+
69+
subject: str = render_to_string(template_name=email.get('subject'))
70+
message: str = render_to_string(template_name=email.get('body'), context=context)
71+
send_mail(subject, message, settings.EMAIL_SENDER, self.recipients.get(self.email_receiver))
2672

2773

28-
def notify_approval(sender: Form, **kwargs) -> None:
74+
def notify_update_approved(sender: Form, **kwargs) -> None:
2975
"""
3076
Handler to send email to users when their request has been approved by an administrator.
3177
:param sender: The signal sender, in this case the django form object.
32-
:param kwargs: Dict with the vocabulary and vocabulary code of the request being approved.
78+
:param kwargs: Dict with metadata for the request being approved.
3379
:return:
3480
"""
81+
email_handler = EmailHandler(sender, kwargs.get('web_request'), kwargs.get('vocabulary'), 'update_approved', 'user')
82+
email_handler.send_email()
3583

36-
email_context: Dict[str, str] = get_email_context(sender, kwargs.get('web_request'), kwargs.get('vocabulary'))
37-
email_message: str = render_to_string(template_name=template_map.get('approved'), context=email_context)
38-
email_subject: str = render_to_string(template_name=template_map.get('update_subject'))
3984

40-
send_mail(email_subject, email_message, settings.EMAIL_SENDER, [sender.instance.submitter_email])
41-
42-
43-
def notify_refusal(sender: Form, **kwargs) -> None:
85+
def notify_update_rejected(sender: Form, **kwargs) -> None:
4486
"""
4587
Handler to send email to users when their request has been refused by an administrator.
4688
:param sender: The signal sender, in this case the django form object.
4789
:param kwargs: Dict with the vocabulary and vocabulary code of the request being refused.
4890
:return:
4991
"""
5092

51-
email_context: Dict[str, str] = get_email_context(sender, kwargs.get('web_request'), kwargs.get('vocabulary'))
52-
email_message: str = render_to_string(template_name=template_map.get('refused'), context=email_context)
53-
email_subject: str = render_to_string(template_name=template_map.get('update_subject'))
93+
email_handler = EmailHandler(sender, kwargs.get('web_request'), kwargs.get('vocabulary'), 'update_rejected', 'user')
94+
email_handler.send_email()
95+
96+
97+
def notify_admin_request_made(sender: Form, **kwargs) -> None:
98+
"""
99+
Handler to send email to administrators when a request has been made by a user.
100+
:param sender: The signal sender, in this case the django form object.
101+
:param kwargs: Dict with the vocabulary and vocabulary code of the request being made.
102+
:return:
103+
"""
104+
105+
email_handler = EmailHandler(sender, kwargs.get('web_request'), kwargs.get('vocabulary'), 'request_made', 'admin')
106+
email_handler.send_email()
107+
108+
109+
def notify_user_request_made(sender: Form, **kwargs) -> None:
110+
"""
111+
Handler to send a confirmation email to a user who just requested a new term.
112+
:param sender: The signal sender, in this case the django form object.
113+
:param kwargs: Dict with the vocabulary and vocabulary code of the request being made.
114+
:return:
115+
"""
54116

55-
send_mail(email_subject, email_message, settings.EMAIL_SENDER, [sender.instance.submitter_email])
117+
email_handler = EmailHandler(sender, kwargs.get('web_request'), kwargs.get('vocabulary'), 'request_made', 'user')
118+
email_handler.send_email()

src/cvinterface/signals.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import django.dispatch
22

3-
from cvinterface.email import notify_approval, notify_refusal
3+
from cvinterface.email import notify_update_approved, notify_update_rejected, \
4+
notify_user_request_made, notify_admin_request_made
45

56
# signal for when a request is created
6-
request_submitted = django.dispatch.Signal()
7+
request_made = django.dispatch.Signal()
78

89
# signal for when a request was rejected
910
request_rejected = django.dispatch.Signal()
@@ -13,5 +14,7 @@
1314

1415

1516
# signal handlers
16-
request_approved.connect(notify_approval, dispatch_uid="cv_request_approved")
17-
request_rejected.connect(notify_refusal, dispatch_uid="cv_request_rejected")
17+
request_made.connect(notify_user_request_made, dispatch_uid='cv_request_made_user')
18+
request_made.connect(notify_admin_request_made, dispatch_uid='cv_request_made_admin')
19+
request_approved.connect(notify_update_approved, dispatch_uid='cv_request_approved')
20+
request_rejected.connect(notify_update_rejected, dispatch_uid='cv_request_rejected')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
User {{ submitter_name }} ( {{ submitter_email }} ) made a request for the {{ action }} {{ vocabulary_verbose_name }} vocabulary term.
2+
3+
Term: {{ concept_term }}
4+
Definition: {{ concept_definition }}
5+
Notes: {{ concept_notes }}
6+
Reason given for request: {{ request_reason }}
7+
To review this submission go to {{ requests_list_url }}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Thank you for your submission to ODM2 Controlled Vocabularies.
2+
3+
Vocabulary: {{ vocabulary_verbose_name }}
4+
Term: {{ concept_term }}
5+
Definition: {{ concept_definition }}
6+
Notes: {{ concept_notes }}
7+
Reason given for request: {{ request_reason }}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
New request for an ODM2 Controlled Vocabulary Term
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ODM2 Controlled Vocabularies Submission
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ODM2 Controlled Vocabularies - Submission Update

0 commit comments

Comments
 (0)