42 lines
1.5 KiB
Python
42 lines
1.5 KiB
Python
from django.db import models
|
|
from django.contrib.auth.models import PermissionsMixin
|
|
from django.contrib.auth.base_user import AbstractBaseUser
|
|
from django.utils import timezone
|
|
from django.core.mail import send_mail
|
|
from django.core.exceptions import ValidationError
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from .managers import UserManager
|
|
|
|
|
|
class User(AbstractBaseUser, PermissionsMixin):
|
|
email = models.EmailField(_('email address'), unique=True)
|
|
phone = models.CharField(_('phone'), max_length=30, blank=True, unique=True, null=True)
|
|
nickname = models.CharField(_('nickname'), max_length=150, blank=True)
|
|
is_active = models.BooleanField(
|
|
_('active'),
|
|
default=True,
|
|
help_text=_('Designates whether this user should be treated as active. '
|
|
'Unselect this instead of deleting accounts.'),
|
|
)
|
|
date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
|
|
description = models.TextField(_('description'), blank=True)
|
|
last_login_ip = models.CharField(_('last login ip'), max_length=64, blank=True)
|
|
|
|
objects = UserManager()
|
|
|
|
USERNAME_FIELD = 'email'
|
|
REQUIRED_FIELDS = []
|
|
|
|
class Meta:
|
|
verbose_name = _('user')
|
|
verbose_name_plural = _('users')
|
|
|
|
def clean(self):
|
|
super().clean()
|
|
self.email = self.__class__.objects.normalize_email(self.email)
|
|
|
|
def email_user(self, subject, message, from_email=None, **kwargs):
|
|
"""Send an email to this user."""
|
|
send_mail(subject, message, from_email, [self.email], **kwargs)
|