expephalon/core/models/profiles.py

59 lines
2.3 KiB
Python

from polymorphic.models import PolymorphicModel
from phonenumber_field.modelfields import PhoneNumberField
from internationalflavor.vat_number.models import VATNumberField
from django_countries.fields import CountryField
from django.db.models import OneToOneField, CASCADE, ImageField, Model, ForeignKey, SET_DEFAULT, ManyToManyField, DateTimeField, TextField
from django.contrib.auth import get_user_model
from core.helpers.files import generate_storage_filename
from core.models.local import Currency
from core.models.brands import Brand
from core.fields.base import LongCharField
from core.fields.color import ColorField
class ClientGroup(Model):
name = LongCharField()
color = ColorField()
class Profile(PolymorphicModel):
user = OneToOneField(get_user_model(), CASCADE)
mobile = PhoneNumberField(blank=True)
class AdminProfile(Profile):
role = LongCharField()
image = ImageField(null=True, blank=True, upload_to=generate_storage_filename)
display_name = LongCharField("Internal Display Name", null=True, blank=True)
permission_string = TextField(blank=True, default="")
@property
def get_internal_name(self):
return self.display_name if self.display_name else self.user.get_full_name
@property
def permissions(self):
return self.permission_string.split(",")
@permissions.setter
def permissions(self, permissions):
if isinstance(permissions, str):
self.permission_string = permissions
else:
self.permission_string = ",".join(permissions)
class ClientProfile(Profile):
company = LongCharField(null=True, blank=True)
address1 = LongCharField()
address2 = LongCharField(null=True, blank=True)
zip = LongCharField()
city = LongCharField()
state = LongCharField(null=True, blank=True)
country = CountryField()
vat_id = VATNumberField(null=True, blank=True)
company_id = LongCharField(null=True, blank=True)
default_currency = ForeignKey(Currency, on_delete=SET_DEFAULT, default=Currency.get_base)
client_groups = ManyToManyField(ClientGroup)
brands = ManyToManyField(Brand)
marketing_opt_in = DateTimeField(null=True)
pgp_key = TextField(null=True)
main_client = ForeignKey("self", null=True, on_delete=SET_DEFAULT, default=None)