2021-04-12 07:49:00 +00:00
|
|
|
from django.contrib.gis.db import models
|
|
|
|
from django.contrib.gis.geos import Point
|
2021-03-01 09:56:10 +00:00
|
|
|
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
|
|
|
|
from django.contrib.auth import get_user_model
|
2021-03-21 15:50:50 +00:00
|
|
|
from django.utils import timezone
|
2021-03-01 09:56:10 +00:00
|
|
|
|
2021-03-22 17:42:07 +00:00
|
|
|
from django_countries.fields import CountryField
|
2021-04-14 05:38:52 +00:00
|
|
|
from phonenumber_field.modelfields import PhoneNumberField
|
2021-05-26 12:07:52 +00:00
|
|
|
from polymorphic.models import PolymorphicModel
|
2021-03-22 17:42:07 +00:00
|
|
|
|
2021-04-12 12:39:18 +00:00
|
|
|
from .helpers import profile_to_coords, upload_path
|
2021-04-12 07:49:00 +00:00
|
|
|
|
2021-06-14 04:06:32 +00:00
|
|
|
from frontend.fields import LanguageField
|
|
|
|
|
2021-03-01 09:56:10 +00:00
|
|
|
class UserManager(BaseUserManager):
|
|
|
|
use_in_migrations = True
|
|
|
|
|
2021-04-14 09:01:38 +00:00
|
|
|
def _create_user(self, email, password, **extra_fields):
|
|
|
|
values = [email]
|
2021-03-21 15:50:50 +00:00
|
|
|
field_value_map = dict(zip(self.model.REQUIRED_FIELDS, values))
|
|
|
|
for field_name, value in field_value_map.items():
|
|
|
|
if not value:
|
|
|
|
raise ValueError('The {} value must be set'.format(field_name))
|
|
|
|
|
2021-03-01 09:56:10 +00:00
|
|
|
email = self.normalize_email(email)
|
2021-03-21 15:50:50 +00:00
|
|
|
user = self.model(
|
|
|
|
email=email,
|
|
|
|
**extra_fields
|
|
|
|
)
|
2021-03-01 09:56:10 +00:00
|
|
|
user.set_password(password)
|
|
|
|
user.save(using=self._db)
|
|
|
|
return user
|
|
|
|
|
2021-04-14 09:01:38 +00:00
|
|
|
def create_user(self, email, password=None, **extra_fields):
|
|
|
|
return self._create_user(email, password, **extra_fields)
|
2021-03-01 09:56:10 +00:00
|
|
|
|
2021-04-14 09:01:38 +00:00
|
|
|
def create_superuser(self, email, password=None, **extra_fields):
|
2021-03-21 15:50:50 +00:00
|
|
|
extra_fields.setdefault('is_superuser', True)
|
2021-04-14 09:01:38 +00:00
|
|
|
return self._create_user(email, password, **extra_fields)
|
2021-03-01 09:56:10 +00:00
|
|
|
|
2021-03-21 15:50:50 +00:00
|
|
|
class User(AbstractBaseUser):
|
|
|
|
email = models.EmailField(unique=True)
|
|
|
|
is_active = models.BooleanField(default=True)
|
|
|
|
date_joined = models.DateTimeField(default=timezone.now)
|
|
|
|
last_login = models.DateTimeField(null=True)
|
|
|
|
is_superuser = models.BooleanField(default=False)
|
2021-06-14 04:06:32 +00:00
|
|
|
language = LanguageField(max_length=16, default="de")
|
2021-03-01 09:56:10 +00:00
|
|
|
|
2021-03-21 15:50:50 +00:00
|
|
|
objects = UserManager()
|
2021-03-01 09:56:10 +00:00
|
|
|
|
|
|
|
USERNAME_FIELD = 'email'
|
|
|
|
|
|
|
|
def get_full_name(self):
|
|
|
|
return self.email
|
|
|
|
|
|
|
|
def get_short_name(self):
|
|
|
|
return self.email
|
|
|
|
|
2021-03-21 15:50:50 +00:00
|
|
|
def has_permission(self, *args, **kwargs):
|
|
|
|
return self.is_superuser
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_staff(self):
|
|
|
|
return self.is_superuser
|
|
|
|
|
|
|
|
has_module_perms = has_permission
|
2021-03-22 17:42:07 +00:00
|
|
|
has_perm = has_permission
|
|
|
|
|
2021-05-26 12:07:52 +00:00
|
|
|
@property
|
|
|
|
def phone(self):
|
|
|
|
try:
|
|
|
|
return self.clientprofile.phone
|
|
|
|
except:
|
|
|
|
try:
|
|
|
|
return self.partnerprofile.phone
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
2021-04-12 07:49:00 +00:00
|
|
|
class AddressMixin(models.Model):
|
2021-04-14 11:15:36 +00:00
|
|
|
street = models.CharField("Straße", max_length=64)
|
|
|
|
city = models.CharField("Stadt", max_length=64)
|
|
|
|
zip = models.CharField("PLZ", max_length=16)
|
|
|
|
state = models.CharField("Bundesland", max_length=64, null=True, blank=True)
|
|
|
|
country = CountryField("Staat")
|
2021-04-12 07:49:00 +00:00
|
|
|
|
2021-04-12 12:39:18 +00:00
|
|
|
@property
|
|
|
|
def full_address(self):
|
|
|
|
return f"{self.street}, {self.city}, {self.zip}, {self.state}, {self.country}"
|
|
|
|
|
2021-04-12 07:49:00 +00:00
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
class LocationMixin(AddressMixin):
|
|
|
|
coords = models.PointField()
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if not self.coords:
|
|
|
|
lat, lon = profile_to_coords(self)
|
|
|
|
self.coords = Point(lon, lat)
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
2021-04-12 12:39:18 +00:00
|
|
|
class ImageMixin(models.Model):
|
2021-04-13 12:44:22 +00:00
|
|
|
image = models.ImageField(upload_to=upload_path, null=True, blank=True)
|
2021-04-12 12:39:18 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
2021-04-14 05:38:52 +00:00
|
|
|
class PhoneMixin(models.Model):
|
2021-04-14 11:15:36 +00:00
|
|
|
phone = PhoneNumberField("Mobiltelefon")
|
2021-04-14 05:38:52 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
2021-04-18 14:46:57 +00:00
|
|
|
class PersonMixin(models.Model):
|
2021-04-14 11:15:36 +00:00
|
|
|
company = models.CharField("Firma", max_length=64, null=True, blank=True)
|
|
|
|
vat_id = models.CharField("UID-Nummer", max_length=32, null=True, blank=True)
|
|
|
|
first_name = models.CharField("Vorname", max_length=64)
|
|
|
|
last_name = models.CharField("Nachname", max_length=64)
|
2021-04-18 14:46:57 +00:00
|
|
|
|
2021-03-23 18:23:04 +00:00
|
|
|
@property
|
|
|
|
def full_name(self):
|
2021-06-10 11:18:52 +00:00
|
|
|
name = self.full_name_only
|
2021-04-14 05:40:26 +00:00
|
|
|
|
2021-03-23 18:23:04 +00:00
|
|
|
if self.company:
|
2021-04-14 05:40:26 +00:00
|
|
|
name += f" ({self.company})"
|
|
|
|
|
|
|
|
return name
|
2021-03-22 17:42:07 +00:00
|
|
|
|
2021-06-10 11:18:52 +00:00
|
|
|
@property
|
|
|
|
def full_name_only(self):
|
|
|
|
name = " ".join([self.first_name, self.last_name])
|
|
|
|
|
|
|
|
return name
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
class Profile(PersonMixin, AddressMixin, PhoneMixin):
|
|
|
|
user = models.OneToOneField(get_user_model(), models.CASCADE)
|
|
|
|
verified = models.BooleanField(default=False)
|
|
|
|
enabled = models.BooleanField(default=True)
|
|
|
|
|
2021-03-22 17:42:07 +00:00
|
|
|
class Meta:
|
2021-05-26 12:07:52 +00:00
|
|
|
abstract = True
|
|
|
|
|
|
|
|
class TwoFactor(PolymorphicModel):
|
|
|
|
user = models.ForeignKey(User, models.CASCADE)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def initiate(cls, user):
|
|
|
|
raise NotImplementedError("%s does not implement initiate()" % cls.__name__)
|
|
|
|
|
|
|
|
def send_token(self, description=""):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def validate_token(self, token):
|
|
|
|
raise NotImplementedError("%s does not implement validate_token()" % cls.__name__)
|