-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathusername.py
More file actions
52 lines (36 loc) · 1.53 KB
/
Copy pathusername.py
File metadata and controls
52 lines (36 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from django.db import models
from django.contrib.auth.hashers import check_password, is_password_usable, make_password
from django.contrib.auth.validators import UnicodeUsernameValidator
from django.utils.translation import gettext_lazy as _
from django.conf import settings
from django_otp.util import random_hex
from .abstract import AbstractService, AbstractUserMixin, PASSCODE_EXPIRY
DEBUG = getattr(settings, 'DEBUG', False)
MULTAUTH_DEBUG = getattr(settings, 'MULTAUTH_DEBUG', DEBUG)
class UsernameService(AbstractService):
USER_MIXIN = 'UsernameUserMixin'
IDENTIFIER_FIELD = 'username'
class Meta:
abstract = True
class UsernameUserMixin(AbstractUserMixin):
username_validator = UnicodeUsernameValidator()
username = models.CharField(_('Username'), max_length=150, blank=True, null=True, unique=True, # editable=False
help_text=_('Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.'),
validators=[username_validator],
error_messages={
'unique': _('A user with that username already exists.'),
},
)
IDENTIFIER = 'username'
class Meta:
abstract = True
def __str__(self):
return str(getattr(self, 'username'))
def clean(self):
super().clean()
if self.username:
self.username = self.__class__.objects.normalize_username(self.username)
def get_username_service(self):
return None # todo: or UsernameService()?
def verify(self, request=None):
super().verify(request)