Exemplo n.º 1
0
def migrate_usernames(stream=None, quiet=False):
    """
    Migrate all existing users to django-email-as-username hashed usernames.
    If any users cannot be migrated an exception will be raised and the
    migration will not run.
    """
    stream = stream or (quiet and open(os.devnull, 'w') or sys.stdout)

    # Check all users can be migrated before applying migration
    emails = set()
    errors = []
    for user in get_user_model().objects.all():
        if not user.email:
            errors.append("Cannot convert user '%s' because email is not "
                          "set." % (user._username, ))
        elif user.email.lower() in emails:
            errors.append("Cannot convert user '%s' because email '%s' "
                          "already exists." % (user._username, user.email))
        else:
            emails.add(user.email.lower())

    # Cannot migrate.
    if errors:
        [stream.write(error + '\n') for error in errors]
        raise Exception('django-email-as-username migration failed.')

    # Can migrate just fine.
    total = get_user_model().objects.count()
    for user in get_user_model().objects.all():
        user.username = _email_to_username(user.email)
        user.save()

    stream.write("Successfully migrated usernames for all %d users\n"
                 % (total, ))
Exemplo n.º 2
0
def get_user(email, queryset=None):
    """
    Return the user with given email address.
    Note that email address matches are case-insensitive.
    """
    if queryset is None:
        queryset = get_user_model().objects
    return queryset.get(username=_email_to_username(email))
Exemplo n.º 3
0
def user_exists(email, queryset=None):
    """
    Return True if a user with given email address exists.
    Note that email address matches are case-insensitive.
    """
    try:
        get_user(email, queryset)
    except get_user_model().DoesNotExist:
        return False
    return True
Exemplo n.º 4
0
def create_user(email, password=None, is_staff=None, is_active=None):
    """
    Create a new user with the given email.
    Use this instead of `User.objects.create_user`.
    """
    try:
        user = get_user_model().objects.create_user(email, email, password)
    except IntegrityError, err:
        regexp = '|'.join(re.escape(e) for e in _DUPLICATE_USERNAME_ERRORS)
        if re.match(regexp, err.message):
            raise IntegrityError('user email is not unique')
        raise
Exemplo n.º 5
0
def user_save_patch(self, *args, **kwargs):
    email_as_username = (self.username.lower() == self.email.lower())
    if self.pk is not None:
        try:
            old_user = self.__class__.objects.get(pk=self.pk)
        except ObjectDoesNotExist:
            pass

        email_as_username = (email_as_username
                             or ('@' in self.username
                                 and old_user.username == old_user.email))

    if email_as_username:
        self.username = _email_to_username(self.email)
    try:
        super(get_user_model(), self).save_base(*args, **kwargs)
    finally:
        if email_as_username:
            self.username = self.email
Exemplo n.º 6
0
def user_save_patch(self, *args, **kwargs):
    email_as_username = (self.username.lower() == self.email.lower())
    if self.pk is not None:
        try:
            old_user = self.__class__.objects.get(pk=self.pk)
        except ObjectDoesNotExist: pass

        email_as_username = (
            email_as_username or
            ('@' in self.username and old_user.username == old_user.email)
        )

    if email_as_username:
        self.username = _email_to_username(self.email)
    try:
        super(get_user_model(), self).save_base(*args, **kwargs)
    finally:
        if email_as_username:
            self.username = self.email
Exemplo n.º 7
0
def unmonkeypatch_user():
    get_user_model().__init__ = original_init
    get_user_model().save_base = original_save_base
Exemplo n.º 8
0
def monkeypatch_user():
    get_user_model().__init__ = user_init_patch
    get_user_model().save_base = user_save_patch
Exemplo n.º 9
0
def user_init_patch(self, *args, **kwargs):
    super(get_user_model(), self).__init__(*args, **kwargs)
    self._username = self.username
    if self.username == _email_to_username(self.email):
        # Username should be replaced by email, since the hashes match
        self.username = self.email
Exemplo n.º 10
0
            pass

        email_as_username = (email_as_username
                             or ('@' in self.username
                                 and old_user.username == old_user.email))

    if email_as_username:
        self.username = _email_to_username(self.email)
    try:
        super(get_user_model(), self).save_base(*args, **kwargs)
    finally:
        if email_as_username:
            self.username = self.email


original_init = get_user_model().__init__
original_save_base = get_user_model().save_base


def monkeypatch_user():
    get_user_model().__init__ = user_init_patch
    get_user_model().save_base = user_save_patch


def unmonkeypatch_user():
    get_user_model().__init__ = original_init
    get_user_model().save_base = original_save_base


monkeypatch_user()
Exemplo n.º 11
0
def unmonkeypatch_user():
    get_user_model().__init__ = original_init
    get_user_model().save_base = original_save_base
Exemplo n.º 12
0
def user_init_patch(self, *args, **kwargs):
    super(get_user_model(), self).__init__(*args, **kwargs)
    self._username = self.username
    if self.username == _email_to_username(self.email):
        # Username should be replaced by email, since the hashes match
        self.username = self.email
Exemplo n.º 13
0
        email_as_username = (
            email_as_username or
            ('@' in self.username and old_user.username == old_user.email)
        )

    if email_as_username:
        self.username = _email_to_username(self.email)
    try:
        super(get_user_model(), self).save_base(*args, **kwargs)
    finally:
        if email_as_username:
            self.username = self.email


original_init = get_user_model().__init__
original_save_base = get_user_model().save_base


def monkeypatch_user():
    get_user_model().__init__ = user_init_patch
    get_user_model().save_base = user_save_patch


def unmonkeypatch_user():
    get_user_model().__init__ = original_init
    get_user_model().save_base = original_save_base


monkeypatch_user()
Exemplo n.º 14
0
def create_superuser(email, password):
    """
    Create a new superuser with the given email.
    Use this instead of `User.objects.create_superuser`.
    """
    return get_user_model().objects.create_superuser(email, email, password)
Exemplo n.º 15
0
 class Meta:
     model = get_user_model() 
Exemplo n.º 16
0
 class Meta:
     model = get_user_model() 
     fields = ("email",)
Exemplo n.º 17
0
def monkeypatch_user():
    get_user_model().__init__ = user_init_patch
    get_user_model().save_base = user_save_patch