/cms/test_utils/project/customuserapp/models.py

https://github.com/furiousdave/django-cms · Python · 83 lines · 56 code · 11 blank · 16 comment · 3 complexity · 6f6d648182bde752437b805618c653d8 MD5 · raw file

  1. # -*- coding: utf-8 -*-
  2. from django.core.mail import send_mail
  3. from django.db import models
  4. from django.utils import timezone
  5. from django.utils.http import urlquote
  6. try:
  7. import re
  8. from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, UserManager
  9. from django.core import validators
  10. from django.utils.translation import ugettext_lazy as _
  11. class CustomUserManager(UserManager):
  12. def _create_user(self, username, email, password,
  13. is_staff, is_superuser, **extra_fields):
  14. """
  15. Creates and saves a User with the given username, email and password.
  16. """
  17. now = timezone.now()
  18. if not username:
  19. raise ValueError('The given username must be set')
  20. email = self.normalize_email(email)
  21. user = self.model(username=username, email=email,
  22. is_staff=is_staff, is_active=True,
  23. is_superuser=is_superuser, last_login=now,
  24. **extra_fields)
  25. user.set_password(password)
  26. user.save(using=self._db)
  27. return user
  28. class User(AbstractBaseUser, PermissionsMixin):
  29. """
  30. An abstract base class implementing a fully featured User model with
  31. admin-compliant permissions.
  32. Username, password and email are required. Other fields are optional.
  33. """
  34. username = models.CharField(_('username'), max_length=30, unique=True,
  35. help_text=_('Required. 30 characters or fewer. Letters, numbers and '
  36. '@/./+/-/_ characters'),
  37. validators=[
  38. validators.RegexValidator(re.compile('^[\w.@+-]+$'), _('Enter a valid username.'), 'invalid')
  39. ])
  40. email = models.EmailField(_('email address'), blank=True)
  41. is_staff = models.BooleanField(_('staff status'), default=False,
  42. help_text=_('Designates whether the user can log into this admin '
  43. 'site.'))
  44. is_active = models.BooleanField(_('active'), default=True,
  45. help_text=_('Designates whether this user should be treated as '
  46. 'active. Unselect this instead of deleting accounts.'))
  47. my_new_field = models.IntegerField(null=True, blank=True, default=42)
  48. objects = CustomUserManager()
  49. USERNAME_FIELD = 'username'
  50. REQUIRED_FIELDS = ['email']
  51. class Meta:
  52. verbose_name = _('user')
  53. verbose_name_plural = _('users')
  54. def get_absolute_url(self):
  55. return "/users/%s/" % urlquote(self.username)
  56. def get_full_name(self):
  57. """
  58. Returns the first_name plus the last_name, with a space in between.
  59. """
  60. return "A user called %s" % self.username
  61. def get_short_name(self):
  62. "Returns the short name for the user."
  63. return self.username
  64. def email_user(self, subject, message, from_email=None):
  65. """
  66. Sends an email to this User.
  67. """
  68. send_mail(subject, message, from_email, [self.email])
  69. except ImportError:
  70. raise
  71. from django.contrib.auth.models import User # nopyflakes