From 2ff8b9e4a59815ed40e937c1bbbada95c99adaf7 Mon Sep 17 00:00:00 2001 From: Modulos Date: Fri, 5 May 2017 14:59:11 +0200 Subject: [PATCH 01/15] Add hosting bill view, model and urls --- hosting/admin.py | 4 +- hosting/migrations/0028_managevms.py | 24 ++++++ hosting/migrations/0029_managevm.py | 24 ++++++ hosting/migrations/0030_hostingbill.py | 31 ++++++++ hosting/models.py | 16 ++++ hosting/templates/hosting/bill_detail.html | 93 ++++++++++++++++++++++ hosting/templates/hosting/bills.html | 60 ++++++++++++++ hosting/urls.py | 5 +- hosting/views.py | 76 +++++++++++++++++- 9 files changed, 330 insertions(+), 3 deletions(-) create mode 100644 hosting/migrations/0028_managevms.py create mode 100644 hosting/migrations/0029_managevm.py create mode 100644 hosting/migrations/0030_hostingbill.py create mode 100644 hosting/templates/hosting/bill_detail.html create mode 100644 hosting/templates/hosting/bills.html diff --git a/hosting/admin.py b/hosting/admin.py index b4f1ba24..ee4e7415 100644 --- a/hosting/admin.py +++ b/hosting/admin.py @@ -5,7 +5,8 @@ from django.core.urlresolvers import reverse from utils.mailer import BaseEmail from .forms import HostingOrderAdminForm -from .models import VirtualMachineType, VirtualMachinePlan, HostingOrder, ManageVM +from .models import VirtualMachineType, VirtualMachinePlan, HostingOrder, \ + ManageVM, HostingBill from .opennebula_functions import HostingManageVMAdmin @@ -98,3 +99,4 @@ admin.site.register(HostingOrder, HostingOrderAdmin) admin.site.register(VirtualMachineType) admin.site.register(VirtualMachinePlan, VirtualMachinePlanAdmin) admin.site.register(ManageVM, HostingManageVMAdmin) +admin.site.register(HostingBill) diff --git a/hosting/migrations/0028_managevms.py b/hosting/migrations/0028_managevms.py new file mode 100644 index 00000000..b71480f2 --- /dev/null +++ b/hosting/migrations/0028_managevms.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.4 on 2017-04-24 04:24 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('hosting', '0027_auto_20160711_0210'), + ] + + operations = [ + migrations.CreateModel( + name='ManageVMs', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ], + options={ + 'managed': False, + }, + ), + ] diff --git a/hosting/migrations/0029_managevm.py b/hosting/migrations/0029_managevm.py new file mode 100644 index 00000000..946e4264 --- /dev/null +++ b/hosting/migrations/0029_managevm.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.4 on 2017-04-24 04:25 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('hosting', '0028_managevms'), + ] + + operations = [ + migrations.CreateModel( + name='ManageVM', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ], + options={ + 'managed': False, + }, + ), + ] diff --git a/hosting/migrations/0030_hostingbill.py b/hosting/migrations/0030_hostingbill.py new file mode 100644 index 00000000..edb01aed --- /dev/null +++ b/hosting/migrations/0030_hostingbill.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.4 on 2017-05-05 11:50 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion +import utils.mixins + + +class Migration(migrations.Migration): + + dependencies = [ + ('utils', '0005_auto_20170322_1443'), + ('membership', '0006_auto_20160526_0445'), + ('hosting', '0029_managevm'), + ] + + operations = [ + migrations.CreateModel( + name='HostingBill', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('billing_address', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='utils.BillingAddress')), + ('customer', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='membership.StripeCustomer')), + ], + options={ + 'permissions': (('view_hostingbill', 'View Hosting Bill'),), + }, + bases=(utils.mixins.AssignPermissionsMixin, models.Model), + ), + ] diff --git a/hosting/models.py b/hosting/models.py index b24dc855..a27fb487 100644 --- a/hosting/models.py +++ b/hosting/models.py @@ -1,5 +1,6 @@ import os +import oca from django.db import models from django.utils.translation import ugettext_lazy as _ from django.utils.functional import cached_property @@ -233,3 +234,18 @@ class ManageVM(models.Model): class Meta: managed = False + +class HostingBill(AssignPermissionsMixin, models.Model): + customer = models.ForeignKey(StripeCustomer) + billing_address = models.ForeignKey(BillingAddress) + + permissions = ('view_hostingbill',) + + class Meta: + permissions = ( + ('view_hostingbill', 'View Hosting Bill'), + ) + + def __str__(self): + return "%s" % (self.customer.user.email) + diff --git a/hosting/templates/hosting/bill_detail.html b/hosting/templates/hosting/bill_detail.html new file mode 100644 index 00000000..1904bf82 --- /dev/null +++ b/hosting/templates/hosting/bill_detail.html @@ -0,0 +1,93 @@ +{% extends "hosting/base_short.html" %} +{% load staticfiles bootstrap3 %} +{% load i18n %} +{% block content %} + +

{{ bill }}

+
+ {# Adress bar #} +
+
+

{% trans "Invoice"%}

{% trans "Order #"%} {{bill.id}}

+
+
+
+
+
+
+ {{bill.customer.user.name}}
+ {{bill.billing_address.street_address}},{{bill.billing_address.postal_code}}
+ {{bill.billing_address.city}}, {{bill.billing_address.country}}. +
+
+
+
+ {% trans "ungleich GmbH" %}
+ {% trans "buchhaltung@ungleich.ch" %}
+ {% trans "Hauptstrasse 14"%}
+ {% trans "CH-8775 Luchsingen"%}
+ {% trans "Mwst-Nummer: CHE-109.549.333 MWST"%}
+ +
+
+
+
+ + {# Bill header #} + + + + + + + + + + + {# Bill items#} + {% for vm in vms %} + + + + + + + + + {% endfor %} + {# Bill total#} + + + + + + + + +
NameCoresMemoryDisk SizePrice
{{ vm.name }}{{ vm.cores }}{{ vm.memory }}{{ vm.disk_size }}{{ vm.price }}
{% trans "Total:" %} {% trans "Brutto" %} {% trans "Netto" %}
+
+ {# Bill Footer #} +
+ {% trans "Alles Preise in CHF mit 8% Mehrwertsteuer." %} + {% trans "Betrag zahlbar innerhalb von 30 Tagen ab Rechnungseingang." %} + {% trans "Kontoverbindung:" %} +
+
+ {% trans "IBAN:" %} +
+
+ {% trans "BIC:" %} +
+
+
+
+ {% trans "CH02 ............" %} +
+
+ {% trans "POFICHBEXXX" %} +
+
+
+
+{% endblock %} + diff --git a/hosting/templates/hosting/bills.html b/hosting/templates/hosting/bills.html new file mode 100644 index 00000000..cc1883fb --- /dev/null +++ b/hosting/templates/hosting/bills.html @@ -0,0 +1,60 @@ +{% extends "hosting/base_short.html" %} +{% load staticfiles bootstrap3 %} +{% load i18n %} + +{% block content %} + +
+
+
+
+ +

{% trans "Customers"%}

+
+ + + + + + + + + {% for user in users %} + + + + + + {% endfor %} + + +
{% trans "Name"%}{% trans "Email"%}
{{ user.name}}{{ user.email}} CHF + +
+ + {% if is_paginated %} + + {% endif %} + +
+ +
+
+ +
+ +{% endblock %} diff --git a/hosting/urls.py b/hosting/urls.py index 5ceeba97..1df6be9e 100644 --- a/hosting/urls.py +++ b/hosting/urls.py @@ -4,7 +4,8 @@ from .views import DjangoHostingView, RailsHostingView, PaymentVMView,\ NodeJSHostingView, LoginView, SignupView, IndexView, \ OrdersHostingListView, OrdersHostingDetailView, VirtualMachinesPlanListView,\ VirtualMachineView, GenerateVMSSHKeysView, OrdersHostingDeleteView, NotificationsView, \ - MarkAsReadNotificationView, PasswordResetView, PasswordResetConfirmView, HostingPricingView + MarkAsReadNotificationView, PasswordResetView, PasswordResetConfirmView, \ + HostingPricingView, HostingBillListView, HostingBillDetailView urlpatterns = [ url(r'index/?$', IndexView.as_view(), name='index'), @@ -15,6 +16,8 @@ urlpatterns = [ url(r'payment/?$', PaymentVMView.as_view(), name='payment'), url(r'orders/?$', OrdersHostingListView.as_view(), name='orders'), url(r'orders/(?P\d+)/?$', OrdersHostingDetailView.as_view(), name='orders'), + url(r'bills/?$', HostingBillListView.as_view(), name='bills'), + url(r'bills/(?P\d+)/?$', HostingBillDetailView.as_view(), name='bills'), url(r'cancel_order/(?P\d+)/?$', OrdersHostingDeleteView.as_view(), name='delete_order'), url(r'my-virtual-machines/?$', VirtualMachinesPlanListView.as_view(), name='virtual_machines'), url(r'my-virtual-machines/(?P\d+)/?$', VirtualMachineView.as_view(), diff --git a/hosting/views.py b/hosting/views.py index 6b21c91b..c9634200 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -1,3 +1,4 @@ +import oca from django.shortcuts import render from django.core.urlresolvers import reverse_lazy, reverse @@ -19,7 +20,7 @@ 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 .models import VirtualMachineType, VirtualMachinePlan, HostingOrder, HostingBill from .forms import HostingUserSignupForm, HostingUserLoginForm from .mixins import ProcessVMSelectionMixin @@ -328,6 +329,10 @@ class OrdersHostingDetailView(PermissionRequiredMixin, LoginRequiredMixin, Detai permission_required = ['view_hostingorder'] model = HostingOrder + def get_context_data(self, **kwargs): + context = super(DetailView, self).get_context_data(**kwargs) + print(context) + return context class OrdersHostingListView(LoginRequiredMixin, ListView): template_name = "hosting/orders.html" @@ -396,3 +401,72 @@ class VirtualMachineView(PermissionRequiredMixin, LoginRequiredMixin, UpdateView email.send() return HttpResponseRedirect(self.get_success_url()) + +class HostingBillListView(LoginRequiredMixin, ListView): + template_name = "hosting/bills.html" + login_url = reverse_lazy('hosting:login') + context_object_name = "users" + model = StripeCustomer + paginate_by = 10 + ordering = '-id' + #TODO show only clients i.e. get_query_set + +class HostingBillDetailView(PermissionRequiredMixin, LoginRequiredMixin, DetailView): + template_name = "hosting/bill_detail.html" + login_url = reverse_lazy('hosting:login') + permission_required = ['view_hostingview'] + context_object_name = "bill" + model = HostingBill + + def get_object(self, queryset=None): + #Get HostingBill for primary key (Select from customer users) + pk = self.kwargs['pk'] + return HostingBill.objects.filter(customer__id=pk).first() + + def get_context_data(self, **kwargs): + # Get User + user_email = self.object.customer.user.email + # Get context + context = super(DetailView, self).get_context_data(**kwargs) + # Add VMs to context + context['vms'] = [] + + # Connect to open nebula server + client = oca.Client("{0}:{1}".format(settings.OPENNEBULA_USERNAME, + settings.OPENNEBULA_PASSWORD), + "{protocol}://{domain}:{port}{endpoint}".format( + protocol=settings.OPENNEBULA_PROTOCOL, + domain=settings.OPENNEBULA_DOMAIN, + port=settings.OPENNEBULA_PORT, + endpoint=settings.OPENNEBULA_ENDPOINT + )) + # Get open nebula user id for given email + user_pool = oca.UserPool(client) + user_pool.info() + user_id = user_pool.get_by_name('alain').id + + # Get vm_pool for given user_id + vm_pool = oca.VirtualMachinePool(client) + vm_pool.info(filter=user_id) + # Add vm in vm_pool to context + for vm in vm_pool: + #TODO: Replace with vm plan + name = vm.name + cores = int(vm.template.vcpu) + memory = int(vm.template.memory) / 1024 + # Check if vm has more than one disk + if 'DISK' in vm.template.multiple: + disk_size = 0 + for disk in vm.template.disks: + disk_size += int(disk.size) / 1024 + else: + disk_size = int(vm.template.disk.size) / 1024 + vm = {} + vm['name'] = name + vm['price'] = 0.6 * disk_size + 2 * memory + 5 * cores + vm['disk_size'] = disk_size + vm['cores'] = cores + vm['memory'] = memory + context['vms'].append(vm) + + return context From 673e8a0c79df1b5d601c75913950d3c77e0a3e1e Mon Sep 17 00:00:00 2001 From: Modulos Date: Sat, 6 May 2017 14:44:08 +0200 Subject: [PATCH 02/15] Add total_price to HostingBill model --- .../0031_hostingbill_total_price.py | 20 ++++ hosting/models.py | 1 + hosting/templates/hosting/bill_detail.html | 91 +++++++++---------- hosting/views.py | 10 +- 4 files changed, 73 insertions(+), 49 deletions(-) create mode 100644 hosting/migrations/0031_hostingbill_total_price.py diff --git a/hosting/migrations/0031_hostingbill_total_price.py b/hosting/migrations/0031_hostingbill_total_price.py new file mode 100644 index 00000000..0a15c1f9 --- /dev/null +++ b/hosting/migrations/0031_hostingbill_total_price.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.4 on 2017-05-06 12:30 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('hosting', '0030_hostingbill'), + ] + + operations = [ + migrations.AddField( + model_name='hostingbill', + name='total_price', + field=models.FloatField(default=0.0), + ), + ] diff --git a/hosting/models.py b/hosting/models.py index a27fb487..8c6a2c0d 100644 --- a/hosting/models.py +++ b/hosting/models.py @@ -238,6 +238,7 @@ class ManageVM(models.Model): class HostingBill(AssignPermissionsMixin, models.Model): customer = models.ForeignKey(StripeCustomer) billing_address = models.ForeignKey(BillingAddress) + total_price = models.FloatField(default=0.0) permissions = ('view_hostingbill',) diff --git a/hosting/templates/hosting/bill_detail.html b/hosting/templates/hosting/bill_detail.html index 1904bf82..b58c21f7 100644 --- a/hosting/templates/hosting/bill_detail.html +++ b/hosting/templates/hosting/bill_detail.html @@ -7,29 +7,29 @@
{# Adress bar #}
-
-

{% trans "Invoice"%}

{% trans "Order #"%} {{bill.id}}

-
+
+

{% trans "Invoice"%}

{% trans "Order #"%} {{bill.id}}

+

-
-
- {{bill.customer.user.name}}
- {{bill.billing_address.street_address}},{{bill.billing_address.postal_code}}
- {{bill.billing_address.city}}, {{bill.billing_address.country}}. -
-
-
-
- {% trans "ungleich GmbH" %}
- {% trans "buchhaltung@ungleich.ch" %}
- {% trans "Hauptstrasse 14"%}
- {% trans "CH-8775 Luchsingen"%}
- {% trans "Mwst-Nummer: CHE-109.549.333 MWST"%}
+
+
+ {{bill.customer.user.name}}
+ {{bill.billing_address.street_address}},{{bill.billing_address.postal_code}}
+ {{bill.billing_address.city}}, {{bill.billing_address.country}}. +
+
+
+
+ {% trans "ungleich GmbH" %}
+ {% trans "buchhaltung@ungleich.ch" %}
+ {% trans "Hauptstrasse 14"%}
+ {% trans "CH-8775 Luchsingen"%}
+ {% trans "Mwst-Nummer: CHE-109.549.333 MWST"%}
-
-
+
+

@@ -48,45 +48,42 @@ {% for vm in vms %} - - - - + + + + {% endfor %} {# Bill total#} - - - - - + +
{{ vm.name }}{{ vm.cores }}{{ vm.memory }}{{ vm.disk_size }}{{ vm.price }}{{ vm.cores }}{{ vm.memory|floatformat }} GiB {{ vm.disk_size|floatformat }} GiB {{ vm.price|floatformat }} CHF
{% trans "Total:" %} {% trans "Brutto" %} {% trans "Netto" %} {% trans "Total:" %} {{ bill.total_price}} CHF

{# Bill Footer #}
- {% trans "Alles Preise in CHF mit 8% Mehrwertsteuer." %} - {% trans "Betrag zahlbar innerhalb von 30 Tagen ab Rechnungseingang." %} - {% trans "Kontoverbindung:" %} -
-
- {% trans "IBAN:" %} -
-
- {% trans "BIC:" %} -
-
-
-
- {% trans "CH02 ............" %} -
-
- {% trans "POFICHBEXXX" %} -
-
+ {% trans "Alles Preise in CHF mit 8% Mehrwertsteuer." %} + {% trans "Betrag zahlbar innerhalb von 30 Tagen ab Rechnungseingang." %} + {% trans "Kontoverbindung:" %} +
+
+ {% trans "IBAN:" %} +
+
+ {% trans "BIC:" %} +
+
+
+
+ {% trans "CH02 ............" %} +
+
+ {% trans "POFICHBEXXX" %} +
+
{% endblock %} diff --git a/hosting/views.py b/hosting/views.py index c9634200..b552063a 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -448,9 +448,10 @@ class HostingBillDetailView(PermissionRequiredMixin, LoginRequiredMixin, DetailV # Get vm_pool for given user_id vm_pool = oca.VirtualMachinePool(client) vm_pool.info(filter=user_id) + # Reset total price + context['bill'].total_price = 0 # Add vm in vm_pool to context for vm in vm_pool: - #TODO: Replace with vm plan name = vm.name cores = int(vm.template.vcpu) memory = int(vm.template.memory) / 1024 @@ -461,12 +462,17 @@ class HostingBillDetailView(PermissionRequiredMixin, LoginRequiredMixin, DetailV disk_size += int(disk.size) / 1024 else: disk_size = int(vm.template.disk.size) / 1024 + + #TODO: Replace with vm plan + price = 0.6 * disk_size + 2 * memory + 5 * cores vm = {} vm['name'] = name - vm['price'] = 0.6 * disk_size + 2 * memory + 5 * cores + vm['price'] = price vm['disk_size'] = disk_size vm['cores'] = cores vm['memory'] = memory context['vms'].append(vm) + context['bill'].total_price += price + context['bill'].save() return context From 6f252def5d9aa7bda7ea9bb99036a69cb3b1114d Mon Sep 17 00:00:00 2001 From: Modulos Date: Sat, 6 May 2017 15:16:10 +0200 Subject: [PATCH 03/15] Return error if HostingBill object does not exist --- hosting/templates/hosting/bill_error.html | 14 ++++++++++++++ hosting/views.py | 8 ++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 hosting/templates/hosting/bill_error.html diff --git a/hosting/templates/hosting/bill_error.html b/hosting/templates/hosting/bill_error.html new file mode 100644 index 00000000..5374ecb5 --- /dev/null +++ b/hosting/templates/hosting/bill_error.html @@ -0,0 +1,14 @@ +{% extends "hosting/base_short.html" %} +{% load staticfiles bootstrap3 %} +{% load i18n %} +{% block content %} + +
+
+

Error

+

Could not get HostingBill object for client.

+

Please create a HostingBill object via the admin page

+
+
+{% endblock %} + diff --git a/hosting/views.py b/hosting/views.py index b552063a..b5a18597 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -424,10 +424,14 @@ class HostingBillDetailView(PermissionRequiredMixin, LoginRequiredMixin, DetailV return HostingBill.objects.filter(customer__id=pk).first() def get_context_data(self, **kwargs): - # Get User - user_email = self.object.customer.user.email # Get context context = super(DetailView, self).get_context_data(**kwargs) + # Get User + try: + user_email = self.object.customer.user.email + except AttributeError: + self.template_name = 'hosting/bill_error.html' + return context # Add VMs to context context['vms'] = [] From bd362cb6191fd2a2449d42b58823b547984669b1 Mon Sep 17 00:00:00 2001 From: Modulos Date: Sat, 6 May 2017 15:28:18 +0200 Subject: [PATCH 04/15] Change to user_email add TODOs --- hosting/views.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/hosting/views.py b/hosting/views.py index b5a18597..923a6f29 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -1,4 +1,5 @@ import oca +import socket from django.shortcuts import render from django.core.urlresolvers import reverse_lazy, reverse @@ -7,6 +8,7 @@ from django.views.generic import View, CreateView, FormView, ListView, DetailVie DeleteView, TemplateView, UpdateView from django.http import HttpResponseRedirect from django.contrib.auth import authenticate, login +from django.contrib import messages from django.conf import settings from guardian.mixins import PermissionRequiredMixin @@ -24,6 +26,9 @@ from .models import VirtualMachineType, VirtualMachinePlan, HostingOrder, Hostin from .forms import HostingUserSignupForm, HostingUserLoginForm from .mixins import ProcessVMSelectionMixin +from oca.exceptions import OpenNebulaException +from oca.pool import WrongNameError + class DjangoHostingView(ProcessVMSelectionMixin, View): template_name = "hosting/django.html" @@ -436,6 +441,7 @@ class HostingBillDetailView(PermissionRequiredMixin, LoginRequiredMixin, DetailV context['vms'] = [] # Connect to open nebula server + # TODO: handle potential connection error client = oca.Client("{0}:{1}".format(settings.OPENNEBULA_USERNAME, settings.OPENNEBULA_PASSWORD), "{protocol}://{domain}:{port}{endpoint}".format( @@ -447,11 +453,13 @@ class HostingBillDetailView(PermissionRequiredMixin, LoginRequiredMixin, DetailV # Get open nebula user id for given email user_pool = oca.UserPool(client) user_pool.info() - user_id = user_pool.get_by_name('alain').id + # TODO: handle potential name error + user_id = user_pool.get_by_name(user_email).id # Get vm_pool for given user_id vm_pool = oca.VirtualMachinePool(client) vm_pool.info(filter=user_id) + # Reset total price context['bill'].total_price = 0 # Add vm in vm_pool to context From 4ab8963149f851687170ae55e26d75e7be221fd8 Mon Sep 17 00:00:00 2001 From: Modulos Date: Sun, 7 May 2017 06:43:28 +0200 Subject: [PATCH 05/15] Follow fat models small views Based on the recommondation in 'Two scoops of Django' I moved the code for accessing the customers vm from the view to the model. --- hosting/models.py | 55 ++++++++++++++++++++++++++++++++++++++++ hosting/views.py | 64 ++--------------------------------------------- 2 files changed, 57 insertions(+), 62 deletions(-) diff --git a/hosting/models.py b/hosting/models.py index 4ca46844..e48f2f4b 100644 --- a/hosting/models.py +++ b/hosting/models.py @@ -267,3 +267,58 @@ class HostingBill(AssignPermissionsMixin, models.Model): def __str__(self): return "%s" % (self.customer.user.email) + def get_vms(self): + # Get User + user_email = self.customer.user.email + + # Connect to open nebula server + # TODO: handle potential connection error + client = oca.Client("{0}:{1}".format(settings.OPENNEBULA_USERNAME, + settings.OPENNEBULA_PASSWORD), + "{protocol}://{domain}:{port}{endpoint}".format( + protocol=settings.OPENNEBULA_PROTOCOL, + domain=settings.OPENNEBULA_DOMAIN, + port=settings.OPENNEBULA_PORT, + endpoint=settings.OPENNEBULA_ENDPOINT + )) + # Get open nebula user id for given email + user_pool = oca.UserPool(client) + user_pool.info() + # TODO: handle potential name error + user_id = user_pool.get_by_name(user_email).id + + # Get vm_pool for given user_id + vm_pool = oca.VirtualMachinePool(client) + vm_pool.info(filter=user_id) + + # Reset total price + self.total_price = 0 + vms = [] + # Add vm in vm_pool to context + for vm in vm_pool: + name = vm.name + cores = int(vm.template.vcpu) + memory = int(vm.template.memory) / 1024 + # Check if vm has more than one disk + if 'DISK' in vm.template.multiple: + disk_size = 0 + for disk in vm.template.disks: + disk_size += int(disk.size) / 1024 + else: + disk_size = int(vm.template.disk.size) / 1024 + + #TODO: Replace with vm plan + price = 0.6 * disk_size + 2 * memory + 5 * cores + vm = {} + vm['name'] = name + vm['price'] = price + vm['disk_size'] = disk_size + vm['cores'] = cores + vm['memory'] = memory + vms.append(vm) + self.total_price += price + + self.save() + return vms + + diff --git a/hosting/views.py b/hosting/views.py index 07bda4d2..91b92667 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -386,11 +386,6 @@ class OrdersHostingDetailView(PermissionRequiredMixin, LoginRequiredMixin, Detai permission_required = ['view_hostingorder'] model = HostingOrder - def get_context_data(self, **kwargs): - context = super(DetailView, self).get_context_data(**kwargs) - print(context) - return context - class OrdersHostingListView(LoginRequiredMixin, ListView): template_name = "hosting/orders.html" login_url = reverse_lazy('hosting:login') @@ -520,60 +515,5 @@ class HostingBillDetailView(PermissionRequiredMixin, LoginRequiredMixin, DetailV def get_context_data(self, **kwargs): # Get context context = super(DetailView, self).get_context_data(**kwargs) - # Get User - try: - user_email = self.object.customer.user.email - except AttributeError: - self.template_name = 'hosting/bill_error.html' - return context - # Add VMs to context - context['vms'] = [] - - # Connect to open nebula server - # TODO: handle potential connection error - client = oca.Client("{0}:{1}".format(settings.OPENNEBULA_USERNAME, - settings.OPENNEBULA_PASSWORD), - "{protocol}://{domain}:{port}{endpoint}".format( - protocol=settings.OPENNEBULA_PROTOCOL, - domain=settings.OPENNEBULA_DOMAIN, - port=settings.OPENNEBULA_PORT, - endpoint=settings.OPENNEBULA_ENDPOINT - )) - # Get open nebula user id for given email - user_pool = oca.UserPool(client) - user_pool.info() - # TODO: handle potential name error - user_id = user_pool.get_by_name(user_email).id - - # Get vm_pool for given user_id - vm_pool = oca.VirtualMachinePool(client) - vm_pool.info(filter=user_id) - - # Reset total price - context['bill'].total_price = 0 - # Add vm in vm_pool to context - for vm in vm_pool: - name = vm.name - cores = int(vm.template.vcpu) - memory = int(vm.template.memory) / 1024 - # Check if vm has more than one disk - if 'DISK' in vm.template.multiple: - disk_size = 0 - for disk in vm.template.disks: - disk_size += int(disk.size) / 1024 - else: - disk_size = int(vm.template.disk.size) / 1024 - - #TODO: Replace with vm plan - price = 0.6 * disk_size + 2 * memory + 5 * cores - vm = {} - vm['name'] = name - vm['price'] = price - vm['disk_size'] = disk_size - vm['cores'] = cores - vm['memory'] = memory - context['vms'].append(vm) - context['bill'].total_price += price - - context['bill'].save() - return context + # Get vms + context['vms'] = self.get_object().get_vms() From c2a76e6c39576169c6933bd293f8c20746d9dd92 Mon Sep 17 00:00:00 2001 From: Modulos Date: Sun, 7 May 2017 16:09:41 +0200 Subject: [PATCH 06/15] Merge migrations and fix spacing --- hosting/migrations/0037_merge.py | 16 ++++++++++++++++ hosting/models.py | 5 +++-- 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 hosting/migrations/0037_merge.py diff --git a/hosting/migrations/0037_merge.py b/hosting/migrations/0037_merge.py new file mode 100644 index 00000000..091a16c5 --- /dev/null +++ b/hosting/migrations/0037_merge.py @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.4 on 2017-05-07 04:49 +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('hosting', '0031_hostingbill_total_price'), + ('hosting', '0036_auto_20170506_2312'), + ] + + operations = [ + ] diff --git a/hosting/models.py b/hosting/models.py index e48f2f4b..63bf8654 100644 --- a/hosting/models.py +++ b/hosting/models.py @@ -4,6 +4,7 @@ import oca from django.db import models from django.utils.translation import ugettext_lazy as _ from django.utils.functional import cached_property +from django.conf import settings from Crypto.PublicKey import RSA from stored_messages.settings import stored_messages_settings @@ -293,7 +294,7 @@ class HostingBill(AssignPermissionsMixin, models.Model): # Reset total price self.total_price = 0 - vms = [] + vms = [] # Add vm in vm_pool to context for vm in vm_pool: name = vm.name @@ -319,6 +320,6 @@ class HostingBill(AssignPermissionsMixin, models.Model): self.total_price += price self.save() - return vms + return vms From f871bb2aba11d1aab12543c74d71080cc0eb7d60 Mon Sep 17 00:00:00 2001 From: Modulos Date: Mon, 8 May 2017 01:43:47 +0200 Subject: [PATCH 07/15] Show correct user name and email --- hosting/templates/hosting/bills.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hosting/templates/hosting/bills.html b/hosting/templates/hosting/bills.html index cc1883fb..1e789e12 100644 --- a/hosting/templates/hosting/bills.html +++ b/hosting/templates/hosting/bills.html @@ -21,8 +21,8 @@ {% for user in users %} - {{ user.name}} - {{ user.email}} CHF + {{ user.user.name}} + {{ user.user.email}}