54 lines
No EOL
2.2 KiB
Python
54 lines
No EOL
2.2 KiB
Python
from django.db.models import OneToOneField, SET_NULL, Model, ForeignKey, CASCADE, CharField
|
|
from django.contrib.auth import get_user_model
|
|
|
|
from polymorphic.models import PolymorphicModel
|
|
from multiselectfield import MultiSelectField
|
|
from django_countries.fields import CountryField
|
|
from vies.types import MEMBER_COUNTRY_CODES, VATIN
|
|
|
|
from profiles.helpers import CLIENT_PERMISSION_CHOICES, OPERATOR_PERMISSION_CHOICES
|
|
|
|
# Create your models here.
|
|
|
|
class Client(Model):
|
|
company = CharField(max_length=255, blank=True, null=True)
|
|
country = CountryField()
|
|
vatid = CharField(max_length=255, blank=True, null=True)
|
|
|
|
def validate_vatid(self):
|
|
if not self.vatid:
|
|
return -1
|
|
if self.vatid.lower().startswith("eu"): # pylint: disable=no-member
|
|
return -2
|
|
if not (self.vatid.upper()[:2]) in list(MEMBER_COUNTRY_CODES): # pylint: disable=no-member
|
|
return -3
|
|
if not ((self.country.code == self.vatid.upper()[:2]) or (self.country.code == "GR" and self.vatid.upper()[:2] == "EL")): # pylint: disable=no-member
|
|
return -4
|
|
return 1 if VATIN.from_str(self.vatid).is_valid else 0
|
|
|
|
def vatid_data(self, force=False):
|
|
if force or self.validate_vatid() == 1:
|
|
return VATIN.from_str(self.vatid).data
|
|
raise ValueError("VAT ID is not valid or cannot be validated")
|
|
|
|
class Profile(PolymorphicModel):
|
|
user = OneToOneField(get_user_model(), on_delete=SET_NULL, null=True)
|
|
client = ForeignKey(Client, on_delete=CASCADE)
|
|
first_name = CharField(max_length=255)
|
|
last_name = CharField(max_length=255)
|
|
|
|
class ClientProfile(Profile):
|
|
permissions = MultiSelectField(choices=CLIENT_PERMISSION_CHOICES, null=True)
|
|
address1 = CharField(max_length=255)
|
|
address2 = CharField(max_length=255, null=True, blank=True)
|
|
|
|
class OperatorProfile(Profile):
|
|
permissions = MultiSelectField(choices=OPERATOR_PERMISSION_CHOICES, null=True)
|
|
|
|
class ClientPermissionGroup(Model):
|
|
name = CharField(max_length=255)
|
|
permissions = MultiSelectField(choices=CLIENT_PERMISSION_CHOICES, null=True)
|
|
|
|
class OperatorPermissionGroup(Model):
|
|
name = CharField(max_length=255)
|
|
permissions = MultiSelectField(choices=OPERATOR_PERMISSION_CHOICES, null=True) |