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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # -*- coding: utf-8 -*-
  2. import re
  3. from django.utils.translation import ugettext_lazy as _
  4. from django.db import models
  5. from django.core import validators
  6. from django.contrib.auth.models import BaseUserManager
  7. from oscar.apps.customer.abstract_models import AbstractUser
  8. class CustomUserManager(BaseUserManager):
  9. def create_user(self, username, email, password):
  10. """
  11. Creates and saves a User with the given email and password.
  12. """
  13. if not email:
  14. raise ValueError('Users must have an email address')
  15. user = self.model(
  16. email=CustomUserManager.normalize_email(email),
  17. username=username,
  18. is_active=True,
  19. )
  20. user.set_password(password)
  21. user.save(using=self._db)
  22. return user
  23. def create_superuser(self, username, email, password):
  24. u = self.create_user(username, email, password=password)
  25. u.is_admin = True
  26. u.is_staff = True
  27. u.save(using=self._db)
  28. return u
  29. class User(AbstractUser):
  30. """
  31. Custom user based on Oscar's AbstractUser
  32. """
  33. username = models.CharField(_('username'), max_length=30, unique=True,
  34. help_text=_('Required. 30 characters or fewer. Letters, numbers and '
  35. '@/./+/-/_ characters'),
  36. validators=[
  37. validators.RegexValidator(re.compile('^[\w.@+-]+$'), _('Enter a valid username.'), 'invalid')
  38. ])
  39. extra_field = models.CharField(
  40. _('Nobody needs me'), max_length=5, blank=True)
  41. objects = CustomUserManager()
  42. class Meta:
  43. app_label = 'myauth'