From b443d4d21e2bf65361a6c6c0691363ef53cc3fa1 Mon Sep 17 00:00:00 2001 From: Levi Date: Wed, 25 May 2016 01:23:32 -0500 Subject: [PATCH] Created BaseEmail class , Now we are sending email to info@ungleich.com after an user book a VM, Fixed pricing issue, Now Admin can changed data about a booked VM --- dynamicweb/settings/base.py | 1 + hosting/admin.py | 3 +- hosting/static/hosting/img/DE_flag.png | Bin 376 -> 741 bytes hosting/templates/emails/new_booked_vm.html | 12 ++++++++ hosting/templates/emails/new_booked_vm.txt | 12 ++++++++ .../templates/hosting/includes/_pricing.html | 2 +- hosting/views.py | 17 +++++++++-- utils/mailer.py | 27 ++++++++++++++++++ 8 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 hosting/templates/emails/new_booked_vm.html create mode 100644 hosting/templates/emails/new_booked_vm.txt diff --git a/dynamicweb/settings/base.py b/dynamicweb/settings/base.py index 5f59a319..db2912bd 100644 --- a/dynamicweb/settings/base.py +++ b/dynamicweb/settings/base.py @@ -132,6 +132,7 @@ TEMPLATES = [ 'DIRS': [os.path.join(PROJECT_DIR, 'cms_templates/'), os.path.join(PROJECT_DIR, 'cms_templates/djangocms_blog/'), os.path.join(PROJECT_DIR, 'membership'), + os.path.join(PROJECT_DIR, 'hosting/templates/'), os.path.join(PROJECT_DIR, 'ungleich/templates/djangocms_blog/'), os.path.join(PROJECT_DIR, 'ungleich/templates/cms/ungleichch'), os.path.join(PROJECT_DIR, 'ungleich/templates/ungleich') diff --git a/hosting/admin.py b/hosting/admin.py index 70392113..d8cf3012 100644 --- a/hosting/admin.py +++ b/hosting/admin.py @@ -1,5 +1,6 @@ from django.contrib import admin -from .models import VirtualMachineType +from .models import VirtualMachineType, VirtualMachinePlan admin.site.register(VirtualMachineType) +admin.site.register(VirtualMachinePlan) diff --git a/hosting/static/hosting/img/DE_flag.png b/hosting/static/hosting/img/DE_flag.png index d4f4526e9e00847d0f5bcd13b4d270453cbe2f43..33db924c79d8b468e3b4644d4e726a57f5125b75 100644 GIT binary patch literal 741 zcmeAS@N?(olHy`uVBq!ia0y~yU~~Yo4{)#n$^n{Yj3-Aqenfs%ZFh8s!^4$~NzBAGZ+ zI29(S4rR{y?Ryy#I@yQ5N8oXBt@NJdZDj_tilH1WaNj_a+26P#Hq8R(Qg9%7Z{G(6}q}Y|gW!U_%O^81FzR}` zIEGZrd3)87x5a^h<=}1ZXCI4%{FwO)4$AvnxS0~XG(m3e84LRl``*7{T;my`(&Fkw z2O=rv_Z!YN8F!8Y$rGZJ1jINKC(@kgeEWUAEh8|Pu!9G+{MUD}IjZ?`{Rb)VboFyt I=akR{0M-L+MgRZ+ diff --git a/hosting/templates/emails/new_booked_vm.html b/hosting/templates/emails/new_booked_vm.html new file mode 100644 index 00000000..8583ba1f --- /dev/null +++ b/hosting/templates/emails/new_booked_vm.html @@ -0,0 +1,12 @@ + +< + + + + + + +NEW VM BOOKED + + + \ No newline at end of file diff --git a/hosting/templates/emails/new_booked_vm.txt b/hosting/templates/emails/new_booked_vm.txt new file mode 100644 index 00000000..8583ba1f --- /dev/null +++ b/hosting/templates/emails/new_booked_vm.txt @@ -0,0 +1,12 @@ + +< + + + + + + +NEW VM BOOKED + + + \ No newline at end of file diff --git a/hosting/templates/hosting/includes/_pricing.html b/hosting/templates/hosting/includes/_pricing.html index 1f4e308e..2f8033bc 100644 --- a/hosting/templates/hosting/includes/_pricing.html +++ b/hosting/templates/hosting/includes/_pricing.html @@ -29,7 +29,7 @@
  • -

    {{vm.hosting_company_name}}

    +

    {{vm.location_code}}


    diff --git a/hosting/views.py b/hosting/views.py index ad892143..60733274 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -12,7 +12,7 @@ from django.contrib import messages from membership.models import CustomUser, StripeCustomer from utils.stripe_utils import StripeUtils from utils.forms import BillingAddressForm -from utils.models import BillingAddress +from utils.mailer import BaseEmail from .models import VirtualMachineType, VirtualMachinePlan, HostingOrder from .forms import HostingUserSignupForm, HostingUserLoginForm from .mixins import ProcessVMSelectionMixin @@ -174,6 +174,7 @@ class PaymentVMView(LoginRequiredMixin, FormView): context.update({ 'stripe_key': settings.STRIPE_API_PUBLIC_KEY }) + return context def post(self, request, *args, **kwargs): @@ -199,7 +200,7 @@ class PaymentVMView(LoginRequiredMixin, FormView): customer = StripeCustomer.get_or_create(email=self.request.user.email, token=token) if not customer: - form.add_error("__all__","Invalid credit card") + form.add_error("__all__", "Invalid credit card") return self.render_to_response(self.get_context_data(form=form)) # Create Virtual Machine Plan @@ -233,6 +234,18 @@ class PaymentVMView(LoginRequiredMixin, FormView): # If the Stripe payment was successed, set order status approved order.set_approved() + + # Send notification to ungleich as soon as VM has been booked + # TODO send email using celery + email_data = { + 'subject': 'New VM request', + 'to': 'info@ungleich.ch', + 'template_name': 'new_booked_vm', + 'template_path': 'emails/' + } + email = BaseEmail(**email_data) + email.send() + request.session.update({ 'charge': charge, 'order': order.id, diff --git a/utils/mailer.py b/utils/mailer.py index a12ffba6..8081152d 100644 --- a/utils/mailer.py +++ b/utils/mailer.py @@ -1,9 +1,35 @@ import six from django.core.mail import send_mail +from django.core.mail import EmailMultiAlternatives +from django.template.loader import render_to_string + from django.conf import settings +class BaseEmail(object): + + def __init__(self, *args, **kwargs): + self.to = kwargs.get('to') + self.template_name = kwargs.get('template_name') + self.template_path = kwargs.get('template_path') + self.subject = kwargs.get('subject') + self.context = kwargs.get('context', {}) + self.template_full_path = '%s%s' % (self.template_path, self.template_name) + + text_content = render_to_string('%s.txt' % self.template_full_path, + {'data': self.context}) + html_content = render_to_string('%s.html' % self.template_full_path, + {'data': self.context}) + + self.email = EmailMultiAlternatives(self.subject, text_content) + self.email.attach_alternative(html_content, "text/html") + self.email.to = ['info@digitalglarus.ch'] + + def send(self): + self.email.send() + + class BaseMailer(object): def __init__(self): self._slug = None @@ -50,3 +76,4 @@ class DigitalGlarusRegistrationMailer(BaseMailer): self.registration = self.message self._message = self._message.format(slug=self._slug) super().__init__() +