2016-05-24 06:19:49 +00:00
|
|
|
import os
|
2017-05-08 00:47:38 +00:00
|
|
|
import socket
|
2017-05-12 17:13:18 +00:00
|
|
|
import logging
|
|
|
|
|
2016-04-23 07:22:44 +00:00
|
|
|
|
2017-05-05 12:59:11 +00:00
|
|
|
import oca
|
2015-05-27 10:21:30 +00:00
|
|
|
from django.db import models
|
2017-04-26 05:10:52 +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
|
2017-05-09 00:02:29 +00:00
|
|
|
from hosting.opennebula_functions import OpenNebulaManager
|
2017-04-24 22:25:05 +00:00
|
|
|
|
2017-05-07 23:57:44 +00:00
|
|
|
from django.conf import settings
|
|
|
|
|
2017-04-24 22:25:05 +00:00
|
|
|
from Crypto.PublicKey import RSA
|
2016-05-29 18:37:43 +00:00
|
|
|
from stored_messages.settings import stored_messages_settings
|
2016-05-24 06:19:49 +00:00
|
|
|
|
2017-05-04 04:19:32 +00:00
|
|
|
from membership.models import StripeCustomer, CustomUser
|
2017-04-24 22:07:50 +00:00
|
|
|
from utils.models import BillingAddress
|
2017-04-24 22:25:05 +00:00
|
|
|
from utils.mixins import AssignPermissionsMixin
|
2016-05-03 05:59:40 +00:00
|
|
|
from .managers import VMPlansManager
|
2017-05-08 00:47:38 +00:00
|
|
|
from oca.exceptions import OpenNebulaException
|
|
|
|
from oca.pool import WrongNameError
|
2016-05-03 05:59:40 +00:00
|
|
|
|
2017-05-08 10:53:03 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2016-04-18 00:52:19 +00:00
|
|
|
|
2016-04-26 06:16:03 +00:00
|
|
|
|
2016-07-11 03:08:51 +00:00
|
|
|
class HostingOrder(AssignPermissionsMixin, models.Model):
|
2016-05-03 05:59:40 +00:00
|
|
|
|
|
|
|
ORDER_APPROVED_STATUS = 'Approved'
|
|
|
|
ORDER_DECLINED_STATUS = 'Declined'
|
|
|
|
|
2017-05-12 10:07:05 +00:00
|
|
|
vm_id = models.IntegerField(default=0)
|
2016-04-26 06:16:03 +00:00
|
|
|
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)
|
2017-05-12 17:13:18 +00:00
|
|
|
price = models.FloatField()
|
2016-04-26 06:16:03 +00:00
|
|
|
|
2016-07-11 03:08:51 +00:00
|
|
|
permissions = ('view_hostingorder',)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
permissions = (
|
|
|
|
('view_hostingorder', 'View Hosting Order'),
|
|
|
|
)
|
|
|
|
|
2016-05-14 06:42:42 +00:00
|
|
|
def __str__(self):
|
|
|
|
return "%s" % (self.id)
|
|
|
|
|
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
|
2017-05-12 17:13:18 +00:00
|
|
|
def create(cls, price=None, vm_id=None, customer=None, billing_address=None):
|
|
|
|
instance = cls.objects.create(
|
|
|
|
price=price,
|
|
|
|
vm_id=vm_id,
|
|
|
|
customer=customer,
|
|
|
|
billing_address=billing_address
|
|
|
|
)
|
2016-07-11 03:08:51 +00:00
|
|
|
instance.assign_permissions(customer.user)
|
2016-04-26 06:16:03 +00:00
|
|
|
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()
|
|
|
|
|
2017-05-11 05:11:33 +00:00
|
|
|
def get_cc_data(self):
|
|
|
|
return {
|
|
|
|
'last4': self.last4,
|
|
|
|
'cc_brand': self.cc_brand,
|
|
|
|
} if self.last4 and self.cc_brand else None
|
|
|
|
|
2016-04-23 07:22:44 +00:00
|
|
|
|
2017-05-04 04:19:32 +00:00
|
|
|
class UserHostingKey(models.Model):
|
|
|
|
user = models.ForeignKey(CustomUser)
|
|
|
|
public_key = models.TextField()
|
|
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def generate_RSA(bits=2048):
|
|
|
|
'''
|
|
|
|
Generate an RSA keypair with an exponent of 65537 in PEM format
|
|
|
|
param: bits The key length in bits
|
|
|
|
Return private key and public key
|
|
|
|
'''
|
|
|
|
new_key = RSA.generate(2048, os.urandom)
|
|
|
|
public_key = new_key.publickey().exportKey("OpenSSH")
|
|
|
|
private_key = new_key.exportKey("PEM")
|
|
|
|
return private_key, public_key
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def generate_keys(cls):
|
|
|
|
private_key, public_key = cls.generate_RSA()
|
|
|
|
# self.public_key = public_key
|
|
|
|
# self.save(update_fields=['public_key'])
|
|
|
|
return private_key, public_key
|
|
|
|
|
2017-05-05 12:59:11 +00:00
|
|
|
class HostingBill(AssignPermissionsMixin, models.Model):
|
|
|
|
customer = models.ForeignKey(StripeCustomer)
|
|
|
|
billing_address = models.ForeignKey(BillingAddress)
|
2017-05-06 12:44:08 +00:00
|
|
|
total_price = models.FloatField(default=0.0)
|
2017-05-05 12:59:11 +00:00
|
|
|
|
|
|
|
permissions = ('view_hostingbill',)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
permissions = (
|
|
|
|
('view_hostingbill', 'View Hosting Bill'),
|
|
|
|
)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "%s" % (self.customer.user.email)
|
|
|
|
|
2017-05-09 02:41:45 +00:00
|
|
|
@classmethod
|
|
|
|
def create(cls, customer=None, billing_address=None):
|
|
|
|
instance = cls.objects.create(customer=customer, billing_address=billing_address)
|
|
|
|
return instance
|
|
|
|
|