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()