|
1 |
| -from typing import Type, Dict |
| 1 | +from typing import Dict |
2 | 2 |
|
3 | 3 | from django.conf import settings
|
4 | 4 | from django.core.handlers.wsgi import WSGIRequest
|
|
9 | 9 |
|
10 | 10 | from odm2cvs.controlled_vocabularies import Vocabulary
|
11 | 11 |
|
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 | + } |
16 | 35 | }
|
17 | 36 |
|
18 | 37 |
|
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)) |
26 | 72 |
|
27 | 73 |
|
28 |
| -def notify_approval(sender: Form, **kwargs) -> None: |
| 74 | +def notify_update_approved(sender: Form, **kwargs) -> None: |
29 | 75 | """
|
30 | 76 | Handler to send email to users when their request has been approved by an administrator.
|
31 | 77 | :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. |
33 | 79 | :return:
|
34 | 80 | """
|
| 81 | + email_handler = EmailHandler(sender, kwargs.get('web_request'), kwargs.get('vocabulary'), 'update_approved', 'user') |
| 82 | + email_handler.send_email() |
35 | 83 |
|
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')) |
39 | 84 |
|
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: |
44 | 86 | """
|
45 | 87 | Handler to send email to users when their request has been refused by an administrator.
|
46 | 88 | :param sender: The signal sender, in this case the django form object.
|
47 | 89 | :param kwargs: Dict with the vocabulary and vocabulary code of the request being refused.
|
48 | 90 | :return:
|
49 | 91 | """
|
50 | 92 |
|
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 | + """ |
54 | 116 |
|
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() |
0 commit comments