-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Username Hyphen Validation on Django Admin
Signed-off-by: Kipchirchir Sigei <[email protected]>
- Loading branch information
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# -*- coding: utf-8 -*- | ||
"""API Django admin amendments.""" | ||
# pylint: disable=imported-auth-user | ||
from django.contrib import admin | ||
from django.contrib.auth.admin import UserAdmin | ||
from django.contrib.auth.forms import UserCreationForm, UserChangeForm | ||
from django.contrib.auth.models import User | ||
from django.core.exceptions import ValidationError | ||
|
||
|
||
class BaseCustomUserForm: | ||
""" | ||
Base form class for custom user forms. | ||
Contains common logic for validating the username. | ||
""" | ||
|
||
def clean_username(self): | ||
""" | ||
Clean the username field to ensure it does not contain hyphens. | ||
Raises: | ||
ValidationError: If the username contains hyphens. | ||
Returns: | ||
str: The cleaned username. | ||
""" | ||
username = self.cleaned_data["username"] | ||
if "-" in username: | ||
raise ValidationError("Usernames cannot contain hyphens.") | ||
return username | ||
|
||
|
||
class CustomUserCreationForm(BaseCustomUserForm, UserCreationForm): | ||
""" | ||
Custom form for user creation. | ||
Inherits from BaseCustomUserForm and UserCreationForm. | ||
""" | ||
|
||
class Meta(UserCreationForm.Meta): | ||
model = User | ||
|
||
|
||
class CustomUserChangeForm(BaseCustomUserForm, UserChangeForm): | ||
""" | ||
Custom form for user change. | ||
Inherits from BaseCustomUserForm and UserChangeForm. | ||
""" | ||
|
||
class Meta(UserChangeForm.Meta): | ||
model = User | ||
|
||
|
||
class CustomUserAdmin(UserAdmin): | ||
""" | ||
Custom User admin panel configuration. | ||
""" | ||
|
||
add_form = CustomUserCreationForm | ||
form = CustomUserChangeForm | ||
|
||
|
||
admin.site.unregister(User) | ||
admin.site.register(User, CustomUserAdmin) |