Skip to content

Commit

Permalink
ensure that username not all numeric #321
Browse files Browse the repository at this point in the history
  • Loading branch information
abrahmasandra committed Nov 30, 2023
1 parent 866f0be commit 9cfdf5a
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/chigame/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.contrib.auth.models import AbstractUser
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError
from django.core.validators import MaxValueValidator
from django.urls import reverse
from django.utils import timezone
Expand All @@ -10,6 +11,14 @@
from chigame.users.managers import UserManager


def validate_username(value):
"""
Validate that the username is not all numeric.
"""
if value.isdigit():
raise ValidationError(_("Username cannot be all numbers."), code="invalid_username")


class User(AbstractUser):
"""
Default custom user model for ChiGame.
Expand All @@ -22,14 +31,20 @@ class User(AbstractUser):
first_name = None # type: ignore
last_name = None # type: ignore
email = models.EmailField(_("email address"), unique=True)
username = models.CharField(_("username"), max_length=255, unique=True, blank=True, null=True)
username = models.CharField(
_("username"), max_length=255, unique=True, blank=True, null=True, validators=[validate_username]
)
tokens = models.PositiveSmallIntegerField(validators=[MaxValueValidator(3)], default=1)

USERNAME_FIELD = "email"
REQUIRED_FIELDS = []

objects = UserManager()

def clean(self) -> None:
super().clean()
self.validate_username(self.username)

def get_absolute_url(self) -> str:
"""Get URL for user's detail view.
Expand Down

0 comments on commit 9cfdf5a

Please sign in to comment.