adding integration opennebula-hosting app
This commit is contained in:
parent
2658205008
commit
ed806910e6
20 changed files with 641 additions and 239 deletions
106
hosting/views.py
106
hosting/views.py
|
|
@ -8,6 +8,8 @@ from django.views.generic import View, CreateView, FormView, ListView, DetailVie
|
|||
from django.http import HttpResponseRedirect
|
||||
from django.contrib.auth import authenticate, login
|
||||
from django.conf import settings
|
||||
from django.shortcuts import redirect
|
||||
|
||||
|
||||
from guardian.mixins import PermissionRequiredMixin
|
||||
from stored_messages.settings import stored_messages_settings
|
||||
|
|
@ -15,13 +17,14 @@ 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
|
||||
from .forms import HostingUserSignupForm, HostingUserLoginForm
|
||||
from .models import VirtualMachineType, VirtualMachinePlan, HostingOrder, UserHostingKey
|
||||
from .forms import HostingUserSignupForm, HostingUserLoginForm, UserHostingKeyForm
|
||||
from .mixins import ProcessVMSelectionMixin
|
||||
from .opennebula_functions import HostingManageVMAdmin
|
||||
|
||||
|
|
@ -40,6 +43,7 @@ class DjangoHostingView(ProcessVMSelectionMixin, View):
|
|||
'google_analytics': "UA-62285904-6",
|
||||
'email': "info@django-hosting.ch",
|
||||
'vm_types': VirtualMachineType.get_serialized_vm_types(),
|
||||
'configuration_options': dict(VirtualMachinePlan.VM_CONFIGURATION)
|
||||
}
|
||||
|
||||
return context
|
||||
|
|
@ -208,26 +212,52 @@ class MarkAsReadNotificationView(LoginRequiredMixin, UpdateView):
|
|||
return HttpResponseRedirect(reverse('hosting:notifications'))
|
||||
|
||||
|
||||
class GenerateVMSSHKeysView(LoginRequiredMixin, DetailView):
|
||||
model = VirtualMachinePlan
|
||||
class GenerateVMSSHKeysView(LoginRequiredMixin, FormView):
|
||||
form_class = UserHostingKeyForm
|
||||
model = UserHostingKey
|
||||
template_name = 'hosting/virtual_machine_key.html'
|
||||
success_url = reverse_lazy('hosting:orders')
|
||||
login_url = reverse_lazy('hosting:login')
|
||||
context_object_name = "virtual_machine"
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
try:
|
||||
user_key = UserHostingKey.objects.get(
|
||||
user=self.request.user
|
||||
)
|
||||
except UserHostingKey.DoesNotExist:
|
||||
user_key = None
|
||||
|
||||
context = super(
|
||||
GenerateVMSSHKeysView,
|
||||
self
|
||||
).get_context_data(**kwargs)
|
||||
|
||||
context.update({
|
||||
'user_key': user_key
|
||||
})
|
||||
|
||||
context = super(GenerateVMSSHKeysView, self).get_context_data(**kwargs)
|
||||
vm = self.get_object()
|
||||
if not vm.public_key:
|
||||
private_key, public_key = vm.generate_keys()
|
||||
context.update({
|
||||
'private_key': private_key,
|
||||
'public_key': public_key
|
||||
})
|
||||
return context
|
||||
return context
|
||||
|
||||
def get_form_kwargs(self):
|
||||
kwargs = super(GenerateVMSSHKeysView, self).get_form_kwargs()
|
||||
kwargs.update({'request': self.request})
|
||||
return kwargs
|
||||
|
||||
def form_valid(self, form):
|
||||
form.save()
|
||||
context = self.get_context_data()
|
||||
|
||||
if form.cleaned_data.get('private_key'):
|
||||
context.update({
|
||||
'private_key': form.cleaned_data.get('private_key'),
|
||||
'key_name': form.cleaned_data.get('name')
|
||||
})
|
||||
|
||||
# print("form", form.cleaned_data)
|
||||
|
||||
return render(self.request, self.template_name, context)
|
||||
|
||||
|
||||
class PaymentVMView(LoginRequiredMixin, FormView):
|
||||
template_name = 'hosting/payment.html'
|
||||
|
|
@ -251,11 +281,11 @@ class PaymentVMView(LoginRequiredMixin, FormView):
|
|||
|
||||
vm_template = specifications.get('vm_template', 1)
|
||||
|
||||
vm_type = VirtualMachineType.objects.first()
|
||||
vm_type = VirtualMachineType.objects.get(id=vm_template)
|
||||
|
||||
final_price = VirtualMachineType.get_price(vm_template)
|
||||
specs = vm_type.get_specs()
|
||||
|
||||
specs = VirtualMachineType.get_specs(vm_template)
|
||||
final_price = vm_type.calculate_price()
|
||||
|
||||
plan_data = {
|
||||
'vm_type': vm_type,
|
||||
|
|
@ -318,9 +348,9 @@ class PaymentVMView(LoginRequiredMixin, FormView):
|
|||
# 'vm_template': vm_template
|
||||
# }
|
||||
|
||||
hosting_admin = HostingManageVMAdmin.__new__(HostingManageVMAdmin)
|
||||
hosting_admin.init_opennebula_client(request)
|
||||
hosting_admin.create(request)
|
||||
# hosting_admin = HostingManageVMAdmin.__new__(HostingManageVMAdmin)
|
||||
# hosting_admin.init_opennebula_client(request)
|
||||
# hosting_admin.create(request)
|
||||
|
||||
# Send notification to ungleich as soon as VM has been booked
|
||||
context = {
|
||||
|
|
@ -381,13 +411,47 @@ class VirtualMachinesPlanListView(LoginRequiredMixin, ListView):
|
|||
ordering = '-id'
|
||||
|
||||
def get_queryset(self):
|
||||
hosting_admin = HostingManageVMAdmin.__new__(HostingManageVMAdmin)
|
||||
print(hosting_admin.show_vms(self.request))
|
||||
# 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 CreateVirtualMachinesView(LoginRequiredMixin, View):
|
||||
template_name = "hosting/create_virtual_machine.html"
|
||||
login_url = reverse_lazy('hosting:login')
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
context = {
|
||||
'vm_types': VirtualMachineType.get_serialized_vm_types(),
|
||||
'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
|
||||
})
|
||||
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, UpdateView):
|
||||
template_name = "hosting/virtual_machine_detail.html"
|
||||
login_url = reverse_lazy('hosting:login')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue