forked from uncloud/uncloud
117 lines
4.1 KiB
Python
117 lines
4.1 KiB
Python
from django.db import models
|
|
from django.db.models import JSONField, Q
|
|
from django.utils import timezone
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from uncloud import COUNTRIES
|
|
from uncloud_net.models import UncloudNetwork
|
|
|
|
|
|
class UncloudModel(models.Model):
|
|
"""
|
|
This class extends the standard model with an
|
|
extra_data field that can be used to include public,
|
|
but internal information.
|
|
|
|
For instance if you migrate from an existing virtualisation
|
|
framework to uncloud.
|
|
|
|
The extra_data attribute should be considered a hack and whenever
|
|
data is necessary for running uncloud, it should **not** be stored
|
|
in there.
|
|
|
|
"""
|
|
|
|
extra_data = JSONField(editable=False, blank=True, null=True)
|
|
|
|
class Meta:
|
|
abstract = True
|
|
|
|
# See https://docs.djangoproject.com/en/dev/ref/models/fields/#field-choices-enum-types
|
|
class UncloudStatus(models.TextChoices):
|
|
PENDING = 'PENDING', _('Pending')
|
|
AWAITING_PAYMENT = 'AWAITING_PAYMENT', _('Awaiting payment')
|
|
BEING_CREATED = 'BEING_CREATED', _('Being created')
|
|
SCHEDULED = 'SCHEDULED', _('Scheduled') # resource selected, waiting for dispatching
|
|
ACTIVE = 'ACTIVE', _('Active')
|
|
MODIFYING = 'MODIFYING', _('Modifying') # Resource is being changed
|
|
DELETED = 'DELETED', _('Deleted') # Resource has been deleted
|
|
DISABLED = 'DISABLED', _('Disabled') # Is usable, but cannot be used for new things
|
|
UNUSABLE = 'UNUSABLE', _('Unusable'), # Has some kind of error
|
|
|
|
|
|
|
|
###
|
|
# General address handling
|
|
class CountryField(models.CharField):
|
|
def __init__(self, *args, **kwargs):
|
|
kwargs.setdefault('choices', COUNTRIES)
|
|
kwargs.setdefault('default', 'CH')
|
|
kwargs.setdefault('max_length', 2)
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
def get_internal_type(self):
|
|
return "CharField"
|
|
|
|
|
|
class UncloudAddress(models.Model):
|
|
full_name = models.CharField(max_length=256)
|
|
organization = models.CharField(max_length=256, blank=True, null=True)
|
|
street = models.CharField(max_length=256)
|
|
city = models.CharField(max_length=256)
|
|
postal_code = models.CharField(max_length=64)
|
|
country = CountryField(blank=True)
|
|
|
|
class Meta:
|
|
abstract = True
|
|
|
|
###
|
|
# Who is running / providing this instance of uncloud?
|
|
|
|
class UncloudProvider(UncloudAddress):
|
|
"""
|
|
A class resembling who is running this uncloud instance.
|
|
This might change over time so we allow starting/ending dates
|
|
|
|
This also defines the taxation rules.
|
|
|
|
starting/ending date define from when to when this is valid. This way
|
|
we can model address changes and have it correct in the bills.
|
|
"""
|
|
|
|
# Meta:
|
|
# FIXMe: only allow non overlapping time frames -- how to define this as a constraint?
|
|
starting_date = models.DateField()
|
|
ending_date = models.DateField(blank=True, null=True)
|
|
|
|
billing_network = models.ForeignKey(UncloudNetwork, related_name="uncloudproviderbill", on_delete=models.CASCADE)
|
|
referral_network = models.ForeignKey(UncloudNetwork, related_name="uncloudproviderreferral", on_delete=models.CASCADE)
|
|
|
|
|
|
@classmethod
|
|
def get_provider(cls, when=None):
|
|
"""
|
|
Find active provide at a certain time - if there was any
|
|
"""
|
|
|
|
if not when:
|
|
when = timezone.now()
|
|
|
|
|
|
return cls.objects.get(Q(starting_date__gte=when, ending_date__lte=when) |
|
|
Q(starting_date__gte=when, ending_date__isnull=True))
|
|
|
|
|
|
@classmethod
|
|
def populate_db_defaults(cls):
|
|
obj, created = cls.objects.get_or_create(name="ungleich glarus ag",
|
|
address="Bahnhofstrasse 1\n8783 Linthal\nSwitzerland",
|
|
starting_date=timezone.now(),
|
|
billing_network=UncloudNetwork.objects.get(description="uncloud Billing"),
|
|
referral_network=UncloudNetwork.objects.get(description="uncloud Referral")
|
|
)
|
|
|
|
|
|
def __str__(self):
|
|
return f"{self.name} {self.address}"
|