Created custom “add order view” to admin panel, Added vm name and user email to orders list admin view , Started function to charge an user for his VM subscription from the admin panel, Created template for email after charge an user for his virtual machine plan, Handle errors creating an order in the admin panel, Now an email is sent to the user after charge him for his virtual machine plan
This commit is contained in:
parent
a846f42bf6
commit
d472d37fab
10 changed files with 649 additions and 21 deletions
|
|
@ -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("<a href='{url}'>{email}</a>", 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("<a href='{url}'>{vm_name}</a>", 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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue