From 1da04a05d1f6bf8d38975cd0324e8f90b4036fca Mon Sep 17 00:00:00 2001 From: PCoder Date: Sun, 27 Aug 2017 15:23:19 +0530 Subject: [PATCH] Created send_plain_email_task celery task and calling it on contact send --- datacenterlight/views.py | 10 ++++++++++ utils/tasks.py | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 utils/tasks.py diff --git a/datacenterlight/views.py b/datacenterlight/views.py index c7b258c2..9eaf95aa 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -21,6 +21,7 @@ from opennebula_api.models import OpenNebulaManager from opennebula_api.serializers import VirtualMachineTemplateSerializer, \ VMTemplateSerializer from datacenterlight.tasks import create_vm_task +from utils.tasks import send_plain_email_task class ContactUsView(FormView): @@ -41,6 +42,15 @@ class ContactUsView(FormView): def form_valid(self, form): form.save() + email_data = { + 'subject': 'Request received on Data Center Light', + 'from_email': settings.DCL_SUPPORT_FROM_ADDRESS, + 'to': ['info@ungleich.ch'], + 'body': "\n".join( + ["%s=%s" % (k, v) for (k, v) in form.cleaned_data.items()]), + 'reply_to': [form.cleaned_data.get('email')], + } + send_plain_email_task.delay(email_data) if self.request.is_ajax(): return self.render_to_response( self.get_context_data(success=True, contact_form=form)) diff --git a/utils/tasks.py b/utils/tasks.py new file mode 100644 index 00000000..5334b507 --- /dev/null +++ b/utils/tasks.py @@ -0,0 +1,20 @@ +from celery.utils.log import get_task_logger +from django.conf import settings +from dynamicweb.celery import app +from django.core.mail import EmailMessage + +logger = get_task_logger(__name__) + + +@app.task(bind=True, max_retries=settings.CELERY_MAX_RETRIES) +def send_plain_email_task(self, email_data): + """ + This is a generic celery task to be used for sending emails. + A celery wrapper task for EmailMessage + + :param self: + :param email_data: A dict of all needed email headers + :return: + """ + email = EmailMessage(**email_data) + email.send()