From a72c1446b45ee794a695a12c0f3c6d384fef8b5d Mon Sep 17 00:00:00 2001 From: Levi Date: Fri, 3 Jun 2016 00:07:47 -0500 Subject: [PATCH] =?UTF-8?q?Created=20custom=20=E2=80=9Cadd=20order=20view?= =?UTF-8?q?=E2=80=9D=20to=20admin=20panel,=20Added=20vm=20name=20and=20use?= =?UTF-8?q?r=20email=20to=20orders=20list=20admin=20view=20,=20Started=20f?= =?UTF-8?q?unction=20to=20charge=20an=20user=20for=20his=20VM=20subscripti?= =?UTF-8?q?on=20from=20the=20admin=20panel,=20Created=20template=20for=20e?= =?UTF-8?q?mail=20after=20charge=20an=20user=20for=20his=20virtual=20machi?= =?UTF-8?q?ne=20plan,=20Handle=20errors=20creating=20an=20order=20in=20the?= =?UTF-8?q?=20admin=20panel,=20Now=20an=20email=20is=20sent=20to=20the=20u?= =?UTF-8?q?ser=20after=20charge=20him=20for=20his=20virtual=20machine=20pl?= =?UTF-8?q?an?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hosting/admin.py | 64 +++++++- hosting/forms.py | 28 ++++ hosting/models.py | 2 +- hosting/templates/emails/new_booked_vm.html | 145 ++++++++++++++++-- hosting/templates/emails/new_booked_vm.txt | 145 ++++++++++++++++-- hosting/templates/emails/vm_charged.html | 135 ++++++++++++++++ hosting/templates/emails/vm_charged.txt | 135 ++++++++++++++++ .../templates/hosting/virtual_machines.html | 12 ++ membership/models.py | 3 + utils/models.py | 1 - 10 files changed, 649 insertions(+), 21 deletions(-) create mode 100644 hosting/templates/emails/vm_charged.html create mode 100644 hosting/templates/emails/vm_charged.txt diff --git a/hosting/admin.py b/hosting/admin.py index ef6249a6..17cb5591 100644 --- a/hosting/admin.py +++ b/hosting/admin.py @@ -1,7 +1,68 @@ from django.contrib import admin +from django.utils.html import format_html +from django.core.urlresolvers import reverse from utils.mailer import BaseEmail -from .models import VirtualMachineType, VirtualMachinePlan +from utils.stripe_utils import StripeUtils + +from .forms import HostingOrderAdminForm +from .models import VirtualMachineType, VirtualMachinePlan, HostingOrder + + +class HostingOrderAdmin(admin.ModelAdmin): + # fields = ('slug', 'imdb_link', 'start', 'finish', 'added_by') + list_display = ('id', 'created_at', 'plan', 'user') + search_fields = ['vm_plan__id', 'customer__user__email'] + + def save_model(self, request, obj, form, change): + if not change: + customer = form.cleaned_data.get('customer') + + # Get and set billing address from the lastest charged order + last_order = HostingOrder.objects.filter(customer=customer).latest('id') + billing_address = last_order.billing_address + obj.billing_address = billing_address + + charge = form.cleaned_data.get('charge') + # Associate an order with a stripe payment + obj.set_stripe_charge(charge) + + # If the Stripe payment was successed, set order status approved + obj.set_approved() + + context = { + 'order': obj, + 'vm': obj.vm_plan + } + email_data = { + 'subject': 'New VM request', + 'to': obj.customer.user.email, + 'context': context, + 'template_name': 'vm_charged', + 'template_path': 'emails/' + } + email = BaseEmail(**email_data) + email.send() + + obj.save() + return obj + + def get_form(self, request, obj=None, **kwargs): + if obj is None: + kwargs['form'] = HostingOrderAdminForm + return super(HostingOrderAdmin, self).get_form(request, obj, **kwargs) + + def user(self, obj): + email = obj.customer.user.email + user_url = reverse("admin:membership_customuser_change", args=[obj.customer.user.id]) + return format_html("{email}", url=user_url, email=email) + + def plan(self, obj): + vm_name = obj.vm_plan.name + vm_url = reverse("admin:hosting_virtualmachineplan_change", args=[obj.vm_plan.id]) + return format_html("{vm_name}", url=vm_url, vm_name=vm_name) + + plan.short_description = "Virtual Machine Plan" class VirtualMachinePlanAdmin(admin.ModelAdmin): @@ -28,5 +89,6 @@ class VirtualMachinePlanAdmin(admin.ModelAdmin): obj.save() +admin.site.register(HostingOrder, HostingOrderAdmin) admin.site.register(VirtualMachineType) admin.site.register(VirtualMachinePlan, VirtualMachinePlanAdmin) diff --git a/hosting/forms.py b/hosting/forms.py index ddb30e1c..c6537355 100644 --- a/hosting/forms.py +++ b/hosting/forms.py @@ -2,6 +2,34 @@ from django import forms from membership.models import CustomUser from django.contrib.auth import authenticate +from utils.stripe_utils import StripeUtils + +from .models import HostingOrder + + +class HostingOrderAdminForm(forms.ModelForm): + + class Meta: + model = HostingOrder + fields = ['vm_plan', 'customer'] + + def clean(self): + customer = self.cleaned_data.get('customer') + vm_plan = self.cleaned_data.get('vm_plan') + + # Make a charge to the customer + stripe_utils = StripeUtils() + charge_response = stripe_utils.make_charge(customer=customer.stripe_id, + amount=vm_plan.price) + charge = charge_response.get('response_object') + if not charge: + raise forms.ValidationError(charge_response.get('error')) + + self.cleaned_data.update({ + 'charge': charge + }) + return self.cleaned_data + class HostingUserLoginForm(forms.Form): diff --git a/hosting/models.py b/hosting/models.py index 518ba175..a8237f3a 100644 --- a/hosting/models.py +++ b/hosting/models.py @@ -104,7 +104,7 @@ class VirtualMachinePlan(models.Model): objects = VMPlansManager() def __str__(self): - return "%s" % (self.id) + return self.name @cached_property def hosting_company_name(self): diff --git a/hosting/templates/emails/new_booked_vm.html b/hosting/templates/emails/new_booked_vm.html index 2785e686..a36f12a8 100644 --- a/hosting/templates/emails/new_booked_vm.html +++ b/hosting/templates/emails/new_booked_vm.html @@ -1,12 +1,139 @@ - - - + + + - + + +Oxygen Invoice - - -NEW VM BOOKED - + + + + + + + + + + + + +
+
+ + +
+ +
+ + + +
+ logo + +
+
+ +
+
+
+
+ + + + + + + + + + +
+ You have booked a virtual machine! +
+ Your virtual machine {{vm.name}} subscription has been charged, +
+ we are going to contact you as soon your virtual machine has been activated. +
+ You can view your invoice clicking on the button below. +
+ +
+
+
+
+ + +
+ ungleich
+
+
+
- \ No newline at end of file + + diff --git a/hosting/templates/emails/new_booked_vm.txt b/hosting/templates/emails/new_booked_vm.txt index 2785e686..3f32d12a 100644 --- a/hosting/templates/emails/new_booked_vm.txt +++ b/hosting/templates/emails/new_booked_vm.txt @@ -1,12 +1,139 @@ - - - + + + - + + +Oxygen Invoice - - -NEW VM BOOKED - + + + + + + + + + + + + +
+
+ + +
+ +
+ + + +
+ logo + +
+
+ +
+
+
+
+ + + + + + + + + + +
+ You have booked a virtual machine! +
+ Your virtual machine {{vm.name}} subscription has been charged, +
+ We are going to contact you as soon your virtual machine has been activated. +
+ You can view your invoice clicking on the button below. +
+ +
+
+
+
+ + +
+ ungleich
+
+
+
- \ No newline at end of file + + diff --git a/hosting/templates/emails/vm_charged.html b/hosting/templates/emails/vm_charged.html new file mode 100644 index 00000000..3f0f5f8f --- /dev/null +++ b/hosting/templates/emails/vm_charged.html @@ -0,0 +1,135 @@ + + + + + + +Oxygen Invoice + + + + + + + + + + + + + +
+
+ + +
+ +
+ + + +
+ logo + +
+
+ +
+
+
+
+ + + + + + + + + + +
+ Your virtual machine plan has been charged! +
+ Your virtual machine {{vm.name}} subscription has been charged,
you can view your invoice clicking on the button below. +
+ +
+
+
+
+ + +
+ ungleich
+
+
+
+ + + diff --git a/hosting/templates/emails/vm_charged.txt b/hosting/templates/emails/vm_charged.txt new file mode 100644 index 00000000..3f0f5f8f --- /dev/null +++ b/hosting/templates/emails/vm_charged.txt @@ -0,0 +1,135 @@ + + + + + + +Oxygen Invoice + + + + + + + + + + + + + +
+
+ + +
+ +
+ + + +
+ logo + +
+
+ +
+
+
+
+ + + + + + + + + + +
+ Your virtual machine plan has been charged! +
+ Your virtual machine {{vm.name}} subscription has been charged,
you can view your invoice clicking on the button below. +
+ +
+
+
+
+ + +
+ ungleich
+
+
+
+ + + diff --git a/hosting/templates/hosting/virtual_machines.html b/hosting/templates/hosting/virtual_machines.html index 208d70fc..61a2a573 100644 --- a/hosting/templates/hosting/virtual_machines.html +++ b/hosting/templates/hosting/virtual_machines.html @@ -13,6 +13,7 @@ ID Location Amount + Status @@ -22,6 +23,17 @@ {{vm.name}} {{vm.location}} {{vm.price}} CHF + + + {% if vm.status == 'pending' %} + {{vm.get_status_display}} + {% elif vm.status == 'online' %} + {{vm.get_status_display}} + {% else %} + {{vm.get_status_display}} + {% endif %} + + diff --git a/membership/models.py b/membership/models.py index ea235f06..0b528c3f 100644 --- a/membership/models.py +++ b/membership/models.py @@ -124,6 +124,9 @@ class StripeCustomer(models.Model): user = models.OneToOneField(CustomUser) stripe_id = models.CharField(unique=True, max_length=100) + def __str__(self): + return "%s - %s" % (self.stripe_id, self.user.email) + @classmethod def get_or_create(cls, email=None, token=None): """ diff --git a/utils/models.py b/utils/models.py index 045a7604..d0cab897 100644 --- a/utils/models.py +++ b/utils/models.py @@ -12,7 +12,6 @@ class BillingAddress(models.Model): country = CountryField() - class ContactMessage(models.Model): name = models.CharField(max_length=200) email = models.EmailField()