Skip to content

Commit

Permalink
#49 Separated the email handling functionality into it's own module a…
Browse files Browse the repository at this point in the history
…nd set up signals to send emails when requests are updated.
  • Loading branch information
jcaraballo17 committed Nov 4, 2020
1 parent e6ca564 commit 524fa56
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/cvinterface/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@

class CvinterfaceConfig(AppConfig):
name = 'cvinterface'

def ready(self):
import cvinterface.signals
55 changes: 55 additions & 0 deletions src/cvinterface/email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from typing import Type, Dict

from django.conf import settings
from django.core.handlers.wsgi import WSGIRequest
from django.core.mail import send_mail
from django.forms import Form
from django.template.loader import render_to_string
from django.urls import reverse

from odm2cvs.controlled_vocabularies import Vocabulary

template_map: Dict[str, str] = {
'update_subject': 'cvinterface/email/update_request_subject.tpl',
'approved': 'cvinterface/email/update_approved_body.tpl',
'rejected': 'cvinterface/email/update_rejected_body.tpl'
}


def get_email_context(web_form: Form, web_request: WSGIRequest, vocabulary: Vocabulary) -> Dict[str, str]:
return {
'submitter_name': web_form.instance.submitter_name,
'concept_name': web_form.instance.name,
'vocabulary_verbose_name': vocabulary.get('name'),
'vocabulary_list_url': web_request.build_absolute_uri(reverse(vocabulary.get('list_url_name')))
}


def notify_approval(sender: Form, **kwargs) -> None:
"""
Handler to send email to users when their request has been approved by an administrator.
:param sender: The signal sender, in this case the django form object.
:param kwargs: Dict with the vocabulary and vocabulary code of the request being approved.
:return:
"""

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

send_mail(email_subject, email_message, settings.EMAIL_SENDER, [sender.instance.submitter_email])


def notify_refusal(sender: Form, **kwargs) -> None:
"""
Handler to send email to users when their request has been refused by an administrator.
:param sender: The signal sender, in this case the django form object.
:param kwargs: Dict with the vocabulary and vocabulary code of the request being refused.
:return:
"""

email_context: Dict[str, str] = get_email_context(sender, kwargs.get('web_request'), kwargs.get('vocabulary'))
email_message: str = render_to_string(template_name=template_map.get('refused'), context=email_context)
email_subject: str = render_to_string(template_name=template_map.get('update_subject'))

send_mail(email_subject, email_message, settings.EMAIL_SENDER, [sender.instance.submitter_email])
17 changes: 17 additions & 0 deletions src/cvinterface/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import django.dispatch

from cvinterface.email import notify_approval, notify_refusal

# signal for when a request is created
request_submitted = django.dispatch.Signal()

# signal for when a request was rejected
request_rejected = django.dispatch.Signal()

# signal for when a request was rejected
request_approved = django.dispatch.Signal()


# signal handlers
request_approved.connect(notify_approval, dispatch_uid="cv_request_approved")
request_rejected.connect(notify_refusal, dispatch_uid="cv_request_rejected")
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{submitter_name}}, your submission "{{concept_name}}" for the {{vocabulary_verbose_name}} vocabulary has been approved.


To see the updated list of terms go to {{vocabulary_list_url}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{submitter_name}}, your submission "{{concept_name}}" for the {{vocabulary_verbose_name}} vocabulary was rejected.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ODM2 Controlled Vocabularies - Submission Update

0 comments on commit 524fa56

Please sign in to comment.