Merge remote-tracking branch 'origin/feature/auto-username' into develop
This commit is contained in:
commit
d2c03b0f37
|
|
@ -18,6 +18,7 @@ from utils.tokens import GMRefreshToken
|
|||
# Serializers
|
||||
class SignupSerializer(serializers.ModelSerializer):
|
||||
"""Signup serializer serializer mixin"""
|
||||
|
||||
class Meta:
|
||||
model = account_models.User
|
||||
fields = (
|
||||
|
|
@ -35,11 +36,18 @@ class SignupSerializer(serializers.ModelSerializer):
|
|||
|
||||
def validate_username(self, value):
|
||||
"""Custom username validation"""
|
||||
valid = utils_methods.username_validator(username=value)
|
||||
if not valid:
|
||||
raise utils_exceptions.NotValidUsernameError()
|
||||
if account_models.User.objects.filter(username__iexact=value).exists():
|
||||
raise serializers.ValidationError()
|
||||
if value:
|
||||
valid = utils_methods.username_validator(username=value)
|
||||
if not valid:
|
||||
raise utils_exceptions.NotValidUsernameError()
|
||||
if account_models.User.objects.filter(username__iexact=value).exists():
|
||||
raise serializers.ValidationError()
|
||||
|
||||
else:
|
||||
value = utils_methods.username_random()
|
||||
while account_models.User.objects.filter(username__iexact=value).exists():
|
||||
value = utils_methods.username_random()
|
||||
|
||||
return value
|
||||
|
||||
def validate_email(self, value):
|
||||
|
|
|
|||
|
|
@ -56,6 +56,14 @@ def username_validator(username: str) -> bool:
|
|||
return True
|
||||
|
||||
|
||||
def username_random():
|
||||
"""Generate random username"""
|
||||
return '{letters}{digits}'.format(
|
||||
letters=''.join([random.choice(string.ascii_lowercase) for _ in range(5)]),
|
||||
digits=random.randrange(100, 1000)
|
||||
)
|
||||
|
||||
|
||||
def image_path(instance, filename):
|
||||
"""Determine avatar path method."""
|
||||
filename = '%s.jpeg' % generate_code()
|
||||
|
|
@ -185,4 +193,4 @@ def get_image_meta_by_url(url) -> (int, int, int):
|
|||
image_raw = requests.get(url)
|
||||
image = Image.open(BytesIO(image_raw.content))
|
||||
width, height = image.size
|
||||
return int(image_raw.headers.get('content-length')), width, height
|
||||
return int(image_raw.headers.get('content-length')), width, height
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user