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/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/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 bceaaacf..b59b7f5a 100644 --- a/hosting/models.py +++ b/hosting/models.py @@ -1,6 +1,7 @@ import os -import oca +import socket +import oca from django.db import models from django.utils.translation import ugettext_lazy as _ from django.utils.functional import cached_property @@ -15,6 +16,7 @@ from membership.models import StripeCustomer, CustomUser from utils.models import BillingAddress from utils.mixins import AssignPermissionsMixin from .managers import VMPlansManager +from oca.exceptions import OpenNebulaException from oca.pool import WrongNameError import logging @@ -313,6 +315,47 @@ class ManageVM(models.Model): class Meta: managed = False + +class HostingBill(AssignPermissionsMixin, models.Model): + customer = models.ForeignKey(StripeCustomer) + billing_address = models.ForeignKey(BillingAddress) + total_price = models.FloatField(default=0.0) + + permissions = ('view_hostingbill',) + + class Meta: + permissions = ( + ('view_hostingbill', 'View Hosting Bill'), + ) + + def __str__(self): + return "%s" % (self.customer.user.email) + + @classmethod + def create(cls, customer=None, billing_address=None): + instance = cls.objects.create(customer=customer, billing_address=billing_address) + return instance + + def get_vms(self): + email = self.customer.user.email + # Get opennebula client + opennebula_client = OpenNebulaManager(create_user=False) + + # Get vm pool + vm_pool = opennebula_client.get_vms(email) + + # Reset total price + self.total_price = 0 + vms = [] + # Add vm in vm_pool to context + for vm in vm_pool: + vm_data = OpenNebulaManager.parse_vm(vm) + self.total_price += vm_data['price'] + vms.append(vm_data) + self.save() + return vms + + def get_user_opennebula_password(): ''' diff --git a/hosting/templates/hosting/bill_detail.html b/hosting/templates/hosting/bill_detail.html new file mode 100644 index 00000000..2d7f04b4 --- /dev/null +++ b/hosting/templates/hosting/bill_detail.html @@ -0,0 +1,91 @@ +{% extends "hosting/base_short.html" %} +{% load staticfiles bootstrap3 %} +{% load i18n %} +{% block content %} + +
Name | +Cores | +Memory | +Disk Size | +Price | +
---|---|---|---|---|
{{ vm.name }} | +{{ vm.cores }} | +{{ vm.memory|floatformat }} GiB | +{{ vm.disk_size|floatformat }} GiB | +{{ vm.price|floatformat }} CHF | + +
{% trans "Total:" %} | +{{ bill.total_price|floatformat}} CHF | +
Could not get HostingBill object for client.
+Please create a HostingBill object via the admin page
+{% trans "Name"%} | +{% trans "Email"%} | ++ |
---|---|---|
{{ user.user.name}} | +{{ user.user.email}} | ++ + | +