2016-04-23 07:22:44 +00:00
|
|
|
import json
|
|
|
|
|
2015-05-27 10:21:30 +00:00
|
|
|
from django.db import models
|
2016-04-18 00:52:19 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2016-05-03 05:59:40 +00:00
|
|
|
from django.utils.functional import cached_property
|
2016-04-18 00:52:19 +00:00
|
|
|
from django.core import serializers
|
2016-04-26 06:16:03 +00:00
|
|
|
from membership.models import StripeCustomer
|
|
|
|
from utils.models import BillingAddress
|
2016-04-18 00:52:19 +00:00
|
|
|
|
2016-05-03 05:59:40 +00:00
|
|
|
from .managers import VMPlansManager
|
|
|
|
|
2016-04-18 00:52:19 +00:00
|
|
|
|
2015-05-27 13:41:43 +00:00
|
|
|
class RailsBetaUser(models.Model):
|
2015-05-27 10:21:30 +00:00
|
|
|
email = models.EmailField(unique=True)
|
|
|
|
received_date = models.DateTimeField('date received')
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "%s - %s" % (self.email, self.received_date)
|
2016-04-18 00:52:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class VirtualMachineType(models.Model):
|
|
|
|
|
|
|
|
HETZNER_NUG = 'hetzner_nug'
|
|
|
|
HETZNER = 'hetzner'
|
|
|
|
HETZNER_R6 = 'hetzner_raid6'
|
|
|
|
HETZNER_G = 'hetzner_glusterfs'
|
|
|
|
BERN = 'bern'
|
|
|
|
|
|
|
|
HOSTING_TYPES = (
|
|
|
|
(HETZNER_NUG, 'Hetzner No Uptime Guarantee'),
|
|
|
|
(HETZNER, 'Hetzner'),
|
|
|
|
(HETZNER_R6, 'Hetzner Raid6'),
|
|
|
|
(HETZNER_G, 'Hetzner Glusterfs'),
|
|
|
|
(BERN, 'Bern'),
|
|
|
|
)
|
|
|
|
|
|
|
|
description = models.TextField()
|
|
|
|
base_price = models.FloatField()
|
|
|
|
memory_price = models.FloatField()
|
|
|
|
core_price = models.FloatField()
|
|
|
|
disk_size_price = models.FloatField()
|
2016-04-18 01:05:39 +00:00
|
|
|
hosting_company = models.CharField(max_length=30, choices=HOSTING_TYPES)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "%s" % (self.get_hosting_company_display())
|
2016-04-18 00:52:19 +00:00
|
|
|
|
2016-04-19 06:04:15 +00:00
|
|
|
@classmethod
|
|
|
|
def get_serialized_vm_types(cls):
|
|
|
|
return [vm.get_serialized_data()
|
2016-04-20 06:03:32 +00:00
|
|
|
for vm in cls.objects.all()]
|
2016-04-23 07:22:44 +00:00
|
|
|
|
|
|
|
def calculate_price(self, specifications):
|
|
|
|
price = float(specifications['cores']) * self.core_price
|
|
|
|
price += float(specifications['memory']) * self.memory_price
|
|
|
|
price += float(specifications['disk_size']) * self.disk_size_price
|
|
|
|
price += self.base_price
|
|
|
|
return price
|
2016-04-19 06:04:15 +00:00
|
|
|
|
2016-04-20 06:03:32 +00:00
|
|
|
def defeault_price(self):
|
|
|
|
price = self.base_price
|
|
|
|
price += self.core_price
|
|
|
|
price += self.memory_price
|
|
|
|
price += self.disk_size_price * 10
|
|
|
|
return price
|
|
|
|
|
2016-04-18 00:52:19 +00:00
|
|
|
def get_serialized_data(self):
|
2016-04-19 06:04:15 +00:00
|
|
|
return {
|
|
|
|
'description': self.description,
|
|
|
|
'base_price': self.base_price,
|
|
|
|
'core_price': self.core_price,
|
|
|
|
'disk_size_price': self.disk_size_price,
|
|
|
|
'memory_price': self.memory_price,
|
|
|
|
'hosting_company_name': self.get_hosting_company_display(),
|
|
|
|
'hosting_company': self.hosting_company,
|
2016-04-20 06:03:32 +00:00
|
|
|
'default_price': self.defeault_price(),
|
2016-04-19 06:04:15 +00:00
|
|
|
'id': self.id,
|
|
|
|
}
|
2016-04-23 07:22:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
class VirtualMachinePlan(models.Model):
|
|
|
|
cores = models.IntegerField()
|
|
|
|
memory = models.IntegerField()
|
|
|
|
disk_size = models.IntegerField()
|
|
|
|
vm_type = models.ForeignKey(VirtualMachineType)
|
|
|
|
price = models.FloatField()
|
|
|
|
|
2016-05-03 05:59:40 +00:00
|
|
|
objects = VMPlansManager()
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def hosting_company_name(self):
|
|
|
|
return self.vm_type.get_hosting_company_display()
|
|
|
|
|
2016-05-04 05:16:41 +00:00
|
|
|
@cached_property
|
|
|
|
def name(self):
|
|
|
|
name = 'vm-%s' % self.id
|
|
|
|
return name
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def orders(self):
|
|
|
|
return [self.hostingorder]
|
|
|
|
|
2016-04-23 07:22:44 +00:00
|
|
|
@classmethod
|
|
|
|
def create(cls, data, user):
|
|
|
|
instance = cls.objects.create(**data)
|
2016-04-26 06:16:03 +00:00
|
|
|
return instance
|
|
|
|
|
|
|
|
|
|
|
|
class HostingOrder(models.Model):
|
2016-05-03 05:59:40 +00:00
|
|
|
|
|
|
|
ORDER_APPROVED_STATUS = 'Approved'
|
|
|
|
ORDER_DECLINED_STATUS = 'Declined'
|
|
|
|
|
2016-04-26 06:16:03 +00:00
|
|
|
VMPlan = models.OneToOneField(VirtualMachinePlan)
|
|
|
|
customer = models.ForeignKey(StripeCustomer)
|
|
|
|
billing_address = models.ForeignKey(BillingAddress)
|
|
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
|
|
approved = models.BooleanField(default=False)
|
2016-05-03 05:59:40 +00:00
|
|
|
last4 = models.CharField(max_length=4)
|
|
|
|
cc_brand = models.CharField(max_length=10)
|
2016-04-26 06:16:03 +00:00
|
|
|
stripe_charge_id = models.CharField(max_length=100, null=True)
|
|
|
|
|
2016-05-03 05:59:40 +00:00
|
|
|
@cached_property
|
|
|
|
def status(self):
|
|
|
|
return self.ORDER_APPROVED_STATUS if self.approved else self.ORDER_DECLINED_STATUS
|
|
|
|
|
2016-04-26 06:16:03 +00:00
|
|
|
@classmethod
|
|
|
|
def create(cls, VMPlan=None, customer=None, billing_address=None):
|
|
|
|
instance = cls.objects.create(VMPlan=VMPlan, customer=customer,
|
|
|
|
billing_address=billing_address)
|
|
|
|
return instance
|
|
|
|
|
|
|
|
def set_approved(self):
|
|
|
|
self.approved = True
|
|
|
|
self.save()
|
|
|
|
|
|
|
|
def set_stripe_charge(self, stripe_charge):
|
|
|
|
self.stripe_charge_id = stripe_charge.id
|
2016-05-03 05:59:40 +00:00
|
|
|
self.last4 = stripe_charge.source.last4
|
|
|
|
self.cc_brand = stripe_charge.source.brand
|
2016-04-26 06:16:03 +00:00
|
|
|
self.save()
|
|
|
|
|
2016-04-23 07:22:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|