2015-05-27 10:21:30 +00:00
|
|
|
from django.contrib import admin
|
2016-06-03 05:07:47 +00:00
|
|
|
from django.utils.html import format_html
|
|
|
|
from django.core.urlresolvers import reverse
|
2017-03-15 12:02:55 +00:00
|
|
|
from django.conf.urls import url
|
|
|
|
from django.template.response import TemplateResponse
|
|
|
|
from django.conf import settings
|
2016-05-27 05:51:10 +00:00
|
|
|
from utils.mailer import BaseEmail
|
2017-03-15 12:02:55 +00:00
|
|
|
from django import template
|
|
|
|
import oca
|
|
|
|
import socket
|
|
|
|
from oca.exceptions import OpenNebulaException
|
2016-06-03 05:07:47 +00:00
|
|
|
|
|
|
|
from .forms import HostingOrderAdminForm
|
2017-03-15 12:02:55 +00:00
|
|
|
from .models import VirtualMachineType, VirtualMachinePlan, HostingOrder, ManageVMs
|
|
|
|
|
|
|
|
register = template.Library()
|
|
|
|
|
|
|
|
def get_vm_state(value):
|
|
|
|
if value == 1:
|
|
|
|
return 'PENDING'
|
|
|
|
#states = {-2: 'Any incl done', -1 : 'Any except done', 0 : 'INIT', 1 : 'PENDING', 2 : 'HOLD', 3 : 'ACTIVE', 4 : 'STOPPED', 5 : 'SUSPENDED', 6 : 'DONE', 7 : 'FAILED'}
|
|
|
|
#return states.get(value)
|
|
|
|
return 'UNKNO'
|
2016-06-03 05:07:47 +00:00
|
|
|
|
2017-03-15 12:02:55 +00:00
|
|
|
register.filter('get_vm_state', get_vm_state)
|
2016-06-03 05:07:47 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2016-07-11 03:08:51 +00:00
|
|
|
# Assigning permissions
|
|
|
|
obj.assign_permissions(customer.user)
|
|
|
|
|
2016-06-03 05:07:47 +00:00
|
|
|
context = {
|
|
|
|
'order': obj,
|
2016-06-16 06:04:48 +00:00
|
|
|
'vm': obj.vm_plan,
|
|
|
|
'base_url': "{0}://{1}".format(request.scheme, request.get_host())
|
2016-06-03 05:07:47 +00:00
|
|
|
}
|
|
|
|
email_data = {
|
2016-06-16 06:04:48 +00:00
|
|
|
'subject': 'Your VM plan has been charged',
|
2016-06-03 05:07:47 +00:00
|
|
|
'to': obj.customer.user.email,
|
|
|
|
'context': context,
|
|
|
|
'template_name': 'vm_charged',
|
Created signup view. Added login after signup.Added signup url to nosystem app urls.py. Added logout view, Added logout button on nabber, Added password reset form, Added password view , Added password reset html, Added password reset email for nosystemd app. Added confirm_reset_password.html, Added confirm_ reset password view, Added confirm reset password form, Fixed reset password token generation, Started donation view, Added donation view, Added donation.html, Added donation form, Adding donation.js lib in order to capture stripe payments for nosystem app.
2016-07-22 06:24:32 +00:00
|
|
|
'template_path': 'hosting/emails/'
|
2016-06-03 05:07:47 +00:00
|
|
|
}
|
|
|
|
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"
|
2015-05-27 10:21:30 +00:00
|
|
|
|
2016-04-18 01:05:39 +00:00
|
|
|
|
2016-05-27 05:51:10 +00:00
|
|
|
class VirtualMachinePlanAdmin(admin.ModelAdmin):
|
|
|
|
list_display = ('name', 'id', 'email')
|
|
|
|
|
|
|
|
def email(self, obj):
|
|
|
|
return obj.hosting_orders.latest('id').customer.user.email
|
|
|
|
|
|
|
|
def save_model(self, request, obj, form, change):
|
|
|
|
email = self.email(obj)
|
2016-06-10 04:50:49 +00:00
|
|
|
if 'status' in form.changed_data:
|
2016-05-27 05:51:10 +00:00
|
|
|
context = {
|
2016-06-16 06:04:48 +00:00
|
|
|
'vm': obj,
|
|
|
|
'base_url': "{0}://{1}".format(request.scheme, request.get_host())
|
2016-05-27 05:51:10 +00:00
|
|
|
}
|
|
|
|
email_data = {
|
|
|
|
'subject': 'Your VM has been activated',
|
|
|
|
'to': email,
|
|
|
|
'context': context,
|
2016-06-10 04:50:49 +00:00
|
|
|
'template_name': 'vm_status_changed',
|
Created signup view. Added login after signup.Added signup url to nosystem app urls.py. Added logout view, Added logout button on nabber, Added password reset form, Added password view , Added password reset html, Added password reset email for nosystemd app. Added confirm_reset_password.html, Added confirm_ reset password view, Added confirm reset password form, Fixed reset password token generation, Started donation view, Added donation view, Added donation.html, Added donation form, Adding donation.js lib in order to capture stripe payments for nosystem app.
2016-07-22 06:24:32 +00:00
|
|
|
'template_path': 'hosting/emails/'
|
2016-05-27 05:51:10 +00:00
|
|
|
}
|
|
|
|
email = BaseEmail(**email_data)
|
|
|
|
email.send()
|
|
|
|
obj.save()
|
|
|
|
|
2017-03-15 12:02:55 +00:00
|
|
|
class HostingManageVMsAdmin(admin.ModelAdmin):
|
|
|
|
client = None
|
|
|
|
def get_urls(self):
|
|
|
|
urls = super().get_urls()
|
|
|
|
socket.setdefaulttimeout(5)
|
|
|
|
my_urls = [
|
|
|
|
url(r'^$', self.admin_site.admin_view(self.my_view, cacheable=True)),
|
|
|
|
url(r'^create_vm/$', self.admin_site.admin_view(self.create_vm, cacheable=True), name='createvm'),
|
|
|
|
#url(r'^my_views/$', self.admin_site.admin_view(self.my_view, cacheable=True))
|
|
|
|
]
|
|
|
|
return my_urls + urls
|
|
|
|
|
|
|
|
def my_view(self, request):
|
|
|
|
s_message = ''
|
|
|
|
e_message = ''
|
|
|
|
try :
|
|
|
|
client = oca.Client(settings.OPENNEBULA_USERNAME + ':' + settings.OPENNEBULA_PASSWORD, settings.OPENNEBULA_PROTOCOL + '://' + settings.OPENNEBULA_DOMAIN + ':' + settings.OPENNEBULA_PORT + settings.OPENNEBULA_ENDPOINT)
|
|
|
|
vm_pool = oca.VirtualMachinePool(client)
|
|
|
|
vm_pool.info()
|
|
|
|
for vm in vm_pool:
|
|
|
|
vm.info()
|
|
|
|
print("%s (memory: %s MB)" % ( vm.name, vm.template.memory))
|
|
|
|
|
|
|
|
except socket.timeout:
|
|
|
|
e_message = "Socket timeout error."
|
|
|
|
except OpenNebulaException:
|
|
|
|
e_message = "OpenNebulaException occurred."
|
|
|
|
context = dict(
|
|
|
|
# Include common variables for rendering the admin template.
|
|
|
|
self.admin_site.each_context(request),
|
|
|
|
error_msg = e_message,
|
|
|
|
success_msg = s_message,
|
|
|
|
vms = vm_pool,
|
|
|
|
# Anything else you want in the context...
|
|
|
|
# key=value,
|
|
|
|
)
|
|
|
|
return TemplateResponse(request, "hosting/managevms.html", context)
|
|
|
|
|
|
|
|
def create_vm(self, request):
|
|
|
|
s_message = ''
|
|
|
|
e_message = ''
|
|
|
|
try :
|
|
|
|
client = oca.Client(settings.OPENNEBULA_USERNAME + ':' + settings.OPENNEBULA_PASSWORD, settings.OPENNEBULA_PROTOCOL + '://' + settings.OPENNEBULA_DOMAIN + ':' + settings.OPENNEBULA_PORT + settings.OPENNEBULA_ENDPOINT)
|
|
|
|
# Lets create a test VM with 128MB of ram and 1 CPU
|
|
|
|
vm_id = oca.VirtualMachine.allocate(client, '<VM><MEMORY>128</MEMORY><CPU>1</CPU></VM>')
|
|
|
|
s_message = "Created with id = " + str(vm_id)
|
|
|
|
# Lets print the VMs available in the pool
|
|
|
|
# print("Printing the available VMs in the pool.")
|
|
|
|
# vm_pool = oca.VirtualMachinePool(client)
|
|
|
|
# for vm in vm_pool:
|
|
|
|
# print("%s (memory: %s MB)" % ( vm.name, vm.template.memory))
|
|
|
|
except socket.timeout:
|
|
|
|
e_message = "Socket timeout error."
|
|
|
|
except OpenNebulaException:
|
|
|
|
e_message = "OpenNebulaException occurred."
|
|
|
|
context = dict(
|
|
|
|
# Include common variables for rendering the admin template.
|
|
|
|
self.admin_site.each_context(request),
|
|
|
|
error_msg=e_message,
|
|
|
|
success_msg=s_message,
|
|
|
|
# Anything else you want in the context...
|
|
|
|
# key=value,
|
|
|
|
)
|
|
|
|
return TemplateResponse(request, "hosting/managevms.html", context)
|
|
|
|
|
|
|
|
def get_vms():
|
|
|
|
vm_pool = oca.VirtualMachinePool(self.client)
|
|
|
|
return vm_pool
|
2016-05-27 05:51:10 +00:00
|
|
|
|
2016-06-03 05:07:47 +00:00
|
|
|
admin.site.register(HostingOrder, HostingOrderAdmin)
|
2016-04-18 01:05:39 +00:00
|
|
|
admin.site.register(VirtualMachineType)
|
2016-05-27 05:51:10 +00:00
|
|
|
admin.site.register(VirtualMachinePlan, VirtualMachinePlanAdmin)
|
2017-03-15 12:02:55 +00:00
|
|
|
admin.site.register(ManageVMs, HostingManageVMsAdmin)
|