from django.contrib.auth.models import AbstractUser
from django.core.validators import (
MinValueValidator,
MaxValueValidator,
RegexValidator
)
from django.db import models
from django.utils.translation import ugettext_lazy as _
from .managers import UserManager
class User(AbstractUser):
username = models.CharField(
_('username'), max_length=30, unique=True, null=True, blank=True,
help_text=_(
'Required. 30 characters or fewer. Letters, digits and '
'@/./+/-/_ only.'
),
validators=[
RegexValidator(
r'^[\w.@+-]+$',
_('Enter a valid username. '
'This value may contain only letters, numbers '
'and @/./+/-/_ characters.'), 'invalid'),
],
error_messages={
'unique': _("A user with that username already exists."),
})
email = models.EmailField(unique=True, null=False, blank=False)
contact_no = models.IntegerField(unique=True, blank=True, null=True)
objects = UserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
def __str__(self):
return self.email
@property
def account_no(self):
if hasattr(self, 'account'):
return self.account.account_no
return None
@property
def full_name(self):
return '{} {}'.format(self.first_name, self.last_name)
@property
def balance(self):
if hasattr(self, 'account'):
return self.account.balance
return None
@property
def full_address(self):
if hasattr(self, 'address'):
return '{}, {}-{}, {}'.format(
self.address.street_address,
self.address.city,
self.address.postal_code,
self.address.country,
)
return None
class AccountDetails(models.Model):
GENDER_CHOICE = (
("M", "Male"),
("F", "Female"),
)
user = models.OneToOneField(
User,
related_name='account',
on_delete=models.CASCADE,
)
account_no = models.PositiveIntegerField(
unique=True,
validators=[
MinValueValidator(10000000),
MaxValueValidator(99999999)
]
)
gender = models.CharField(max_length=1, choices=GENDER_CHOICE)
birth_date = models.DateField(null=True, blank=True)
balance = models.DecimalField(
default=0,
max_digits=12,
decimal_places=2
)
picture = models.ImageField(
null=True,
blank=True,
upload_to='account_pictures/',
)
def __str__(self):
return str(self.account_no)
class UserAddress(models.Model):
user = models.OneToOneField(
User,
related_name='address',
on_delete=models.CASCADE,
)
street_address = models.CharField(max_length=512)
city = models.CharField(max_length=256)
postal_code = models.PositiveSmallIntegerField()
country = models.CharField(max_length=256)
def __str__(self):
return self.user.email