Skip to content

Commit

Permalink
refactor: Update field labels in UserAdmin, enhance validation messag…
Browse files Browse the repository at this point in the history
…es in RegisterSerializer, and adjust middleware settings in settings.py
  • Loading branch information
AhmedNassar7 committed Dec 22, 2024
1 parent 87cd7f2 commit 33924a4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion apps/users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class UserAdmin(admin.ModelAdmin):
('Permissions', {
'fields': ('is_active', 'is_staff', 'is_superuser')
}),
('Audit Information', {
('Account Information', {
'fields': ('last_login', 'date_joined')
}),
('Subscription Information', {
Expand Down
11 changes: 7 additions & 4 deletions apps/users/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,23 @@ class Meta:
]

def validate(self, data):
# Ensure passwords match
# Password match validation
if data["password"] != data["confirm_password"]:
raise serializers.ValidationError({"password": "Passwords do not match."})
# Best Practice: Check that email is unique as well

# Unique email check
if User.objects.filter(email=data["email"]).exists():
raise serializers.ValidationError(
{"email": "This email is already registered."}
)
# Ensure username is unique

# Unique username check
if User.objects.filter(username=data["username"]).exists():
raise serializers.ValidationError(
{"username": "This username is already taken."}
)
# Best Practice: Validate phone number format (example: phone number should be 10 digits)

# Phone number format validation
if data.get("phone_number") and len(data["phone_number"]) != 11:
raise serializers.ValidationError(
{"phone_number": "Phone number must be 11 digits."}
Expand Down
19 changes: 16 additions & 3 deletions egypt_metro/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@

# External packages
"allauth", # Authentication
# "allauth.account", # Account management
"allauth.account", # Account management
# "allauth.socialaccount", # Social authentication
# "allauth.socialaccount.providers.google", # Google OAuth provider
"rest_framework", # REST framework
Expand All @@ -75,15 +75,15 @@

MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware", # Security middleware
"whitenoise.middleware.WhiteNoiseMiddleware", # WhiteNoise middleware
"django.contrib.sessions.middleware.SessionMiddleware", # Session middleware
"django.middleware.common.CommonMiddleware", # Common middleware
"django.middleware.csrf.CsrfViewMiddleware", # CSRF middleware
"django.contrib.auth.middleware.AuthenticationMiddleware", # Authentication middleware
"django.contrib.messages.middleware.MessageMiddleware", # Messages middleware
"django.middleware.clickjacking.XFrameOptionsMiddleware", # Clickjacking middleware
"corsheaders.middleware.CorsMiddleware", # CORS middleware
"whitenoise.middleware.WhiteNoiseMiddleware", # WhiteNoise middleware
# "allauth.account.middleware.AccountMiddleware", # Account middleware
"allauth.account.middleware.AccountMiddleware", # Account middleware
# "debug_toolbar.middleware.DebugToolbarMiddleware", # Debug toolbar middleware
]

Expand Down Expand Up @@ -183,12 +183,25 @@
raise ValueError(f"{var} is not set in environment variables.")

if ENVIRONMENT == "prod":
CACHES = {
"default": {
"BACKEND": "django.core.cache.backends.redis.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1",
}
}
SECURE_SSL_REDIRECT = True
SECURE_HSTS_SECONDS = 31536000
SECURE_HSTS_PRELOAD = True
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True
else:
SECURE_SSL_REDIRECT = False
SECURE_HSTS_PRELOAD = False
SECURE_HSTS_INCLUDE_SUBDOMAINS = False
CSRF_COOKIE_SECURE = False
SESSION_COOKIE_SECURE = False


# if not DEBUG: # Enable only in production
# SECURE_SSL_REDIRECT = os.getenv("SECURE_SSL_REDIRECT", "True") == "True"
Expand Down

0 comments on commit 33924a4

Please sign in to comment.