API Integration
Please review carefully.
This commit is contained in:
parent
b7e8eceb25
commit
130c00c8ee
19 changed files with 310 additions and 890 deletions
203
hosting/views.py
203
hosting/views.py
|
|
@ -19,16 +19,18 @@ from stored_messages.models import Message
|
|||
from stored_messages.api import mark_read
|
||||
|
||||
|
||||
|
||||
from membership.models import CustomUser, StripeCustomer
|
||||
from utils.stripe_utils import StripeUtils
|
||||
from utils.forms import BillingAddressForm, PasswordResetRequestForm
|
||||
from utils.views import PasswordResetViewMixin, PasswordResetConfirmViewMixin, LoginViewMixin
|
||||
from utils.mailer import BaseEmail
|
||||
from .models import VirtualMachineType, VirtualMachinePlan, HostingOrder, HostingBill, UserHostingKey
|
||||
from .models import HostingOrder, HostingBill, UserHostingKey
|
||||
from .forms import HostingUserSignupForm, HostingUserLoginForm, UserHostingKeyForm
|
||||
from .mixins import ProcessVMSelectionMixin
|
||||
from .opennebula_functions import HostingManageVMAdmin, OpenNebulaManager
|
||||
|
||||
from opennebula_api.models import OpenNebulaManager
|
||||
from opennebula_api.serializers import VirtualMachineSerializer,\
|
||||
VirtualMachineTemplateSerializer
|
||||
|
||||
from oca.exceptions import OpenNebulaException
|
||||
from oca.pool import WrongNameError
|
||||
|
|
@ -282,48 +284,26 @@ class PaymentVMView(LoginRequiredMixin, FormView):
|
|||
|
||||
if form.is_valid():
|
||||
context = self.get_context_data()
|
||||
specifications = request.session.get('vm_specs')
|
||||
specifications = request.session.get('template')
|
||||
|
||||
vm_template = specifications.get('vm_template', 1)
|
||||
|
||||
vm_type = VirtualMachineType.objects.get(id=vm_template)
|
||||
|
||||
specs = vm_type.get_specs()
|
||||
|
||||
final_price = vm_type.calculate_price()
|
||||
|
||||
plan_data = {
|
||||
'vm_type': vm_type,
|
||||
'configuration': specifications.get(
|
||||
'configuration',
|
||||
'django'
|
||||
),
|
||||
'price': final_price
|
||||
}
|
||||
|
||||
plan_data.update(specs)
|
||||
vm_template_id = specifications.get('id', 1)
|
||||
|
||||
final_price = specifications.get('price', 1)
|
||||
|
||||
token = form.cleaned_data.get('token')
|
||||
|
||||
owner = self.request.user
|
||||
|
||||
# Get or create stripe customer
|
||||
customer = StripeCustomer.get_or_create(email=self.request.user.email,
|
||||
customer = StripeCustomer.get_or_create(email=owner.email,
|
||||
token=token)
|
||||
if not customer:
|
||||
form.add_error("__all__", "Invalid credit card")
|
||||
return self.render_to_response(self.get_context_data(form=form))
|
||||
|
||||
# Create Virtual Machine Plan
|
||||
plan = VirtualMachinePlan.create(plan_data, request.user)
|
||||
|
||||
# Create Billing Address
|
||||
billing_address = form.save()
|
||||
|
||||
# Create a Hosting Order
|
||||
order = HostingOrder.create(vm_plan=plan, customer=customer,
|
||||
billing_address=billing_address)
|
||||
# Create a Hosting Bill
|
||||
bill = HostingBill.create(customer=customer, billing_address=billing_address)
|
||||
|
||||
# Make stripe charge to a customer
|
||||
stripe_utils = StripeUtils()
|
||||
charge_response = stripe_utils.make_charge(amount=final_price,
|
||||
|
|
@ -340,24 +320,34 @@ class PaymentVMView(LoginRequiredMixin, FormView):
|
|||
|
||||
charge = charge_response.get('response_object')
|
||||
|
||||
# Create OpenNebulaManager
|
||||
|
||||
manager = OpenNebulaManager(email=owner.email,
|
||||
password=owner.password[0:20],
|
||||
create_user=True)
|
||||
template = manager.get_template(vm_template_id)
|
||||
|
||||
# Create a vm using logged user
|
||||
vm_id = manager.create_vm(vm_template_id)
|
||||
# Create a Hosting Order
|
||||
order = HostingOrder.create(vm_id=vm_id, customer=customer,
|
||||
billing_address=billing_address)
|
||||
# Create a Hosting Bill
|
||||
bill = HostingBill.create(customer=customer, billing_address=billing_address)
|
||||
|
||||
|
||||
# Associate an order with a stripe payment
|
||||
order.set_stripe_charge(charge)
|
||||
|
||||
# If the Stripe payment was successed, set order status approved
|
||||
order.set_approved()
|
||||
|
||||
# Create a vm using logged user
|
||||
oppennebula_vm_id = VirtualMachinePlan.create_opennebula_vm(
|
||||
self.request.user,
|
||||
specs
|
||||
)
|
||||
vm = VirtualMachineSerializer(manager.get_vm(vm_id)).data
|
||||
|
||||
plan.oppenebula_id = oppennebula_vm_id
|
||||
plan.save()
|
||||
|
||||
# Send notification to ungleich as soon as VM has been booked
|
||||
context = {
|
||||
'vm': plan,
|
||||
'vm': vm,
|
||||
'order': order,
|
||||
'base_url': "{0}://{1}".format(request.scheme, request.get_host())
|
||||
|
||||
|
|
@ -384,6 +374,18 @@ class OrdersHostingDetailView(PermissionRequiredMixin, LoginRequiredMixin, Detai
|
|||
permission_required = ['view_hostingorder']
|
||||
model = HostingOrder
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
# Get context
|
||||
context = super(DetailView, self).get_context_data(**kwargs)
|
||||
obj = self.get_object()
|
||||
owner = self.request.user
|
||||
manager = OpenNebulaManager(email=owner.email,
|
||||
password=owner.password[0:20],
|
||||
create_user=True)
|
||||
vm = manager.get_vm(obj.vm_id)
|
||||
context['vm'] = VirtualMachineSerializer(vm).data
|
||||
|
||||
|
||||
class OrdersHostingListView(LoginRequiredMixin, ListView):
|
||||
template_name = "hosting/orders.html"
|
||||
login_url = reverse_lazy('hosting:login')
|
||||
|
|
@ -408,24 +410,17 @@ class VirtualMachinesPlanListView(LoginRequiredMixin, ListView):
|
|||
template_name = "hosting/virtual_machines.html"
|
||||
login_url = reverse_lazy('hosting:login')
|
||||
context_object_name = "vms"
|
||||
model = VirtualMachinePlan
|
||||
paginate_by = 10
|
||||
ordering = '-id'
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super(VirtualMachinesPlanListView, self).get_context_data(**kwargs)
|
||||
context.update({
|
||||
'vms_opennebula': VirtualMachinePlan.get_vms(self.request.user)
|
||||
})
|
||||
return context
|
||||
|
||||
def get_queryset(self):
|
||||
# hosting_admin = HostingManageVMAdmin.__new__(HostingManageVMAdmin)
|
||||
# print(hosting_admin.show_vms_view(self.request))
|
||||
# print(VirtualMachinePlan.get_vms(self.request.user.))
|
||||
user = self.request.user
|
||||
self.queryset = VirtualMachinePlan.objects.active(user)
|
||||
return super(VirtualMachinesPlanListView, self).get_queryset()
|
||||
owner = self.request.user
|
||||
manager = OpenNebulaManager(email=owner.email,
|
||||
password=owner.password[0:20],
|
||||
create_user=True)
|
||||
queryset = manager.get_vms()
|
||||
serializer = VirtualMachineSerializer(queryset, many=True)
|
||||
return serializer.data
|
||||
|
||||
|
||||
class CreateVirtualMachinesView(LoginRequiredMixin, View):
|
||||
|
|
@ -433,92 +428,56 @@ class CreateVirtualMachinesView(LoginRequiredMixin, View):
|
|||
login_url = reverse_lazy('hosting:login')
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
#TODO: Replace with OpenNebulaManager.get_apps
|
||||
templates = OpenNebulaManager().get_templates()
|
||||
data = VirtualMachineTemplateSerializer(templates, many=True).data
|
||||
context = {
|
||||
'vm_types': VirtualMachineType.get_serialized_vm_types(),
|
||||
'configuration_options': VirtualMachinePlan.VM_CONFIGURATION
|
||||
'templates': data,
|
||||
#'configuration_options': VirtualMachinePlan.VM_CONFIGURATION
|
||||
}
|
||||
# context = {}
|
||||
return render(request, self.template_name, context)
|
||||
|
||||
def post(self, request):
|
||||
configuration = request.POST.get('configuration')
|
||||
configuration_display = dict(VirtualMachinePlan.VM_CONFIGURATION).get(configuration)
|
||||
vm_template = request.POST.get('vm_template')
|
||||
vm_type = VirtualMachineType.objects.get(id=vm_template)
|
||||
vm_specs = vm_type.get_specs()
|
||||
vm_specs.update({
|
||||
'configuration_display': configuration_display,
|
||||
'configuration': configuration,
|
||||
'final_price': vm_type.final_price,
|
||||
'vm_template': vm_template
|
||||
})
|
||||
request.session['vm_specs'] = vm_specs
|
||||
#XXX: Fix this!
|
||||
#configuration = request.POST.get('configuration')
|
||||
#configuration_display = dict(VirtualMachinePlan.VM_CONFIGURATION).get(configuration)
|
||||
template_id = int(request.POST.get('vm_template_id'))
|
||||
template = OpenNebulaManager().get_template(template_id)
|
||||
data = VirtualMachineTemplateSerializer(template).data
|
||||
vm_specs = {
|
||||
#'configuration_display': configuration_display,
|
||||
#'configuration': configuration,
|
||||
'template': data,
|
||||
}
|
||||
request.session['template'] = data
|
||||
return redirect(reverse('hosting:payment'))
|
||||
|
||||
# def get_queryset(self):
|
||||
# # hosting_admin = HostingManageVMAdmin.__new__(HostingManageVMAdmin)
|
||||
# # print(hosting_admin.show_vms(self.request))
|
||||
# user = self.request.user
|
||||
# self.queryset = VirtualMachinePlan.objects.active(user)
|
||||
# return super(VirtualMachinesPlanListView, self).get_queryset()
|
||||
|
||||
|
||||
class VirtualMachineView(PermissionRequiredMixin, LoginRequiredMixin, View):
|
||||
class VirtualMachineView(LoginRequiredMixin, View):
|
||||
template_name = "hosting/virtual_machine_detail.html"
|
||||
login_url = reverse_lazy('hosting:login')
|
||||
# model = VirtualMachinePlan
|
||||
# context_object_name = "virtual_machine"
|
||||
permission_required = ['view_virtualmachineplan', 'cancel_virtualmachineplan']
|
||||
# fields = '__all__'
|
||||
|
||||
# def get_context_data(self, **kwargs):
|
||||
# vm_plan = get_object()
|
||||
# context = super(VirtualMachineView, self).get_context_data(**kwargs)
|
||||
# context.update({
|
||||
# 'opennebula_vm': VirtualMachinePlan.get_vm(
|
||||
# self.request.user.email,
|
||||
# opennebula_id
|
||||
# )
|
||||
# })
|
||||
# return context
|
||||
|
||||
# def get_object(self, queryset=None):
|
||||
# # if queryset is None:
|
||||
# # queryset = self.get_queryset()
|
||||
# # Next, try looking up by primary key.
|
||||
# vm_id = self.kwargs.get(self.pk_url_kwarg)
|
||||
# try:
|
||||
# return VirtualMachinePlan.get_vm(
|
||||
# self.request.user.email,
|
||||
# vm_id
|
||||
# )
|
||||
# except Exception as error:
|
||||
# raise Http404()
|
||||
|
||||
# def get_success_url(self):
|
||||
# vm = self.get_object()
|
||||
# final_url = "%s%s" % (reverse('hosting:virtual_machines', kwargs={'pk': vm.id}),
|
||||
# '#status-v')
|
||||
# return final_url
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
owner = self.request.user
|
||||
manager = OpenNebulaManager(email=owner.email,
|
||||
password=owner.password[0:20],
|
||||
create_user=True)
|
||||
vm_id = self.kwargs.get('pk')
|
||||
try:
|
||||
opennebula_vm = VirtualMachinePlan.get_vm(
|
||||
self.request.user,
|
||||
vm_id
|
||||
)
|
||||
vm = manager.get_vm(vm_id)
|
||||
serializer = VirtualMachineSerializer(vm)
|
||||
except Exception as error:
|
||||
print(error)
|
||||
raise Http404()
|
||||
|
||||
context = {
|
||||
'virtual_machine': opennebula_vm,
|
||||
'virtual_machine': serializer.data,
|
||||
}
|
||||
# context = {}
|
||||
return render(request, self.template_name, context)
|
||||
|
||||
def post(self, *args, **kwargs):
|
||||
#TODO: add api to OpenNebulaManager
|
||||
vm = self.get_object()
|
||||
vm.cancel_plan()
|
||||
|
||||
|
|
@ -564,10 +523,14 @@ class HostingBillDetailView(PermissionRequiredMixin, LoginRequiredMixin, DetailV
|
|||
def get_context_data(self, **kwargs):
|
||||
# Get context
|
||||
context = super(DetailView, self).get_context_data(**kwargs)
|
||||
|
||||
owner = self.request.user
|
||||
manager = OpenNebulaManager(email=owner.email,
|
||||
password=owner.password[0:20],
|
||||
create_user=True)
|
||||
# Get vms
|
||||
try:
|
||||
context['vms'] = self.get_object().get_vms()
|
||||
except:
|
||||
pass
|
||||
queryset = manager.get_vms()
|
||||
vms = VirtualMachineSerializer(queryset, many=True).data
|
||||
context['vms'] = vms
|
||||
|
||||
return context
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue