# -*- coding: utf-8 -*-

# Code will only work with Django >= 1.5. See tests/config.py
import re

from django.utils.translation import ugettext_lazy as _
from django.db import models
from django.core import validators
from django.contrib.auth.models import BaseUserManager

from oscar.apps.customer.abstract_models import AbstractUser


class CustomUserManager(BaseUserManager):

    def create_user(self, username, email, password):
        """
        Creates and saves a User with the given email and password.
        """
        if not email:
            raise ValueError('Users must have an email address')

        user = self.model(
            email=CustomUserManager.normalize_email(email),
            username=username,
            is_active=True,
        )

        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, username, email, password):
        u = self.create_user(username, email, password=password)
        u.is_admin = True
        u.is_staff = True
        u.save(using=self._db)
        return u


class User(AbstractUser):
    """
    Custom user based on Oscar's AbstractUser
    """
    username = models.CharField(_('username'), max_length=30, unique=True,
        help_text=_('Required. 30 characters or fewer. Letters, numbers and '
                    '@/./+/-/_ characters'),
        validators=[
            validators.RegexValidator(re.compile('^[\w.@+-]+$'), _('Enter a valid username.'), 'invalid')
        ])
    extra_field = models.CharField(
        _('Nobody needs me'), max_length=5, blank=True)

    objects = CustomUserManager()

    class Meta:
        app_label = 'myauth'

[((45, 32, 45, 45), 'django.utils.translation.ugettext_lazy', '_', ({(45, 34, 45, 44): '"""username"""'}, {}), "('username')", True, 'from django.utils.translation import ugettext_lazy as _\n'), ((52, 8, 52, 28), 'django.utils.translation.ugettext_lazy', '_', ({(52, 10, 52, 27): '"""Nobody needs me"""'}, {}), "('Nobody needs me')", True, 'from django.utils.translation import ugettext_lazy as _\n'), ((46, 18, 47, 43), 'django.utils.translation.ugettext_lazy', '_', ({(46, 20, 47, 42): '"""Required. 30 characters or fewer. Letters, numbers and @/./+/-/_ characters"""'}, {}), "('Required. 30 characters or fewer. Letters, numbers and @/./+/-/_ characters'\n    )", True, 'from django.utils.translation import ugettext_lazy as _\n'), ((49, 38, 49, 63), 're.compile', 're.compile', ({(49, 49, 49, 62): '"""^[\\\\w.@+-]+$"""'}, {}), "('^[\\\\w.@+-]+$')", False, 'import re\n'), ((49, 65, 49, 93), 'django.utils.translation.ugettext_lazy', '_', ({(49, 67, 49, 92): '"""Enter a valid username."""'}, {}), "('Enter a valid username.')", True, 'from django.utils.translation import ugettext_lazy as _\n')]