From 900f014d922fd73fb97da08a35e3074d1d2d74f4 Mon Sep 17 00:00:00 2001
From: PCoder <purple.coder@yahoo.co.uk>
Date: Sun, 1 Jul 2018 18:33:10 +0200
Subject: [PATCH] Save order specifications in HostingOrder also

---
 datacenterlight/utils.py                      | 11 ++++--
 hosting/migrations/0045_auto_20180701_1631.py | 35 +++++++++++++++++++
 hosting/models.py                             | 16 ++++++++-
 3 files changed, 59 insertions(+), 3 deletions(-)
 create mode 100644 hosting/migrations/0045_auto_20180701_1631.py

diff --git a/datacenterlight/utils.py b/datacenterlight/utils.py
index 5388b9d3..7b3ef73d 100644
--- a/datacenterlight/utils.py
+++ b/datacenterlight/utils.py
@@ -1,12 +1,12 @@
 from django.contrib.sites.models import Site
 
 from datacenterlight.tasks import create_vm_task
-from hosting.models import HostingOrder, HostingBill
+from hosting.models import HostingOrder, HostingBill, OrderSpecifications
 from membership.models import StripeCustomer
 from utils.forms import UserBillingAddressForm
 from utils.models import BillingAddress
 from .cms_models import CMSIntegration
-from .models import VMPricing
+from .models import VMPricing, VMTemplate
 
 
 def get_cms_integration(name):
@@ -53,6 +53,13 @@ def create_vm(billing_address_data, stripe_customer_id, specs,
         vm_pricing=vm_pricing
     )
 
+    order_specs_obj, obj_created = OrderSpecifications.objects.get_or_create(
+        vm_template=VMTemplate.objects.get(vm_template_id),
+        cores=specs['cpu'], memory=specs['memory'], ssd_size=specs['disk_size']
+    )
+    order.order_specs = order_specs_obj
+    order.save()
+
     # Create a Hosting Bill
     HostingBill.create(customer=customer, billing_address=billing_address)
 
diff --git a/hosting/migrations/0045_auto_20180701_1631.py b/hosting/migrations/0045_auto_20180701_1631.py
new file mode 100644
index 00000000..8af4d821
--- /dev/null
+++ b/hosting/migrations/0045_auto_20180701_1631.py
@@ -0,0 +1,35 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.9.4 on 2018-07-01 16:31
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+import django.db.models.deletion
+import utils.mixins
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('datacenterlight', '0024_dclcalculatorpluginmodel_vm_templates_to_show'),
+        ('hosting', '0044_hostingorder_vm_pricing'),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='OrderSpecifications',
+            fields=[
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+                ('cores', models.IntegerField(default=0)),
+                ('memory', models.IntegerField(default=0)),
+                ('hdd_size', models.IntegerField(default=0)),
+                ('ssd_size', models.IntegerField(default=0)),
+                ('vm_template', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='datacenterlight.VMTemplate')),
+            ],
+            bases=(utils.mixins.AssignPermissionsMixin, models.Model),
+        ),
+        migrations.AddField(
+            model_name='hostingorder',
+            name='order_specs',
+            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='hosting.OrderSpecifications'),
+        ),
+    ]
diff --git a/hosting/models.py b/hosting/models.py
index de4d3aec..c30a25a8 100644
--- a/hosting/models.py
+++ b/hosting/models.py
@@ -7,7 +7,7 @@ from django.utils import timezone
 from django.utils.functional import cached_property
 from Crypto.PublicKey import RSA
 
-from datacenterlight.models import VMPricing
+from datacenterlight.models import VMPricing, VMTemplate
 from membership.models import StripeCustomer, CustomUser
 from utils.models import BillingAddress
 from utils.mixins import AssignPermissionsMixin
@@ -41,6 +41,19 @@ class HostingPlan(models.Model):
         return price
 
 
+class OrderSpecifications(AssignPermissionsMixin, models.Model):
+    vm_template = models.ForeignKey(VMTemplate, blank=True, null=True)
+    cores = models.IntegerField(default=0)
+    memory = models.IntegerField(default=0)
+    hdd_size = models.IntegerField(default=0)
+    ssd_size = models.IntegerField(default=0)
+
+    def __str__(self):
+        return "%s - %s cores, %s GB RAM, %s GB SSD" % (
+            self.vm_template.name, self.cores, self.memory, self.ssd_size
+        )
+
+
 class HostingOrder(AssignPermissionsMixin, models.Model):
     ORDER_APPROVED_STATUS = 'Approved'
     ORDER_DECLINED_STATUS = 'Declined'
@@ -56,6 +69,7 @@ class HostingOrder(AssignPermissionsMixin, models.Model):
     price = models.FloatField()
     subscription_id = models.CharField(max_length=100, null=True)
     vm_pricing = models.ForeignKey(VMPricing)
+    order_specs = models.ForeignKey(OrderSpecifications, null=True, blank=True)
 
     permissions = ('view_hostingorder',)