You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

models.py 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # -*- coding: utf-8 -*-
  2. # Code will only work with Django >= 1.5. See tests/config.py
  3. import re
  4. from django.utils.translation import ugettext_lazy as _
  5. from django.db import models
  6. from django.core import validators
  7. from django.contrib.auth.models import BaseUserManager
  8. from oscar.apps.customer.abstract_models import AbstractUser
  9. class CustomUserManager(BaseUserManager):
  10. def create_user(self, username, email, password):
  11. """
  12. Creates and saves a User with the given email and password.
  13. """
  14. if not email:
  15. raise ValueError('Users must have an email address')
  16. user = self.model(
  17. email=CustomUserManager.normalize_email(email),
  18. username=username,
  19. is_active=True,
  20. )
  21. user.set_password(password)
  22. user.save(using=self._db)
  23. return user
  24. def create_superuser(self, username, email, password):
  25. u = self.create_user(username, email, password=password)
  26. u.is_admin = True
  27. u.is_staff = True
  28. u.save(using=self._db)
  29. return u
  30. class User(AbstractUser):
  31. """
  32. Custom user based on Oscar's AbstractUser
  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. extra_field = models.CharField(
  41. _('Nobody needs me'), max_length=5, blank=True)
  42. objects = CustomUserManager()
  43. class Meta:
  44. app_label = 'myauth'