feat: add constants for image types and error messages in forms

This commit is contained in:
2026-05-26 10:19:21 +02:00
parent 35e7e93600
commit 6ec0f4e732
2 changed files with 15 additions and 9 deletions
+3
View File
@@ -0,0 +1,3 @@
IMAGE_TYPE = "image/*"
EMAIL_FORMNAME = "Correo Electrónico"
INCORRECT_PASSWORDS = "Las contraseñas no coinciden"
+12 -9
View File
@@ -2,10 +2,13 @@ from django import forms
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.core.validators import FileExtensionValidator, MinLengthValidator, MaxLengthValidator from django.core.validators import FileExtensionValidator, MinLengthValidator, MaxLengthValidator
from .models import Category from .models import Category
from .constants import *
ALLOWED_IMAGE_EXTENSIONS = ['jpg', 'jpeg', 'png', 'gif', 'webp'] ALLOWED_IMAGE_EXTENSIONS = ['jpg', 'jpeg', 'png', 'gif', 'webp']
ALLOWED_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'] ALLOWED_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp']
def validate_image_file(value): def validate_image_file(value):
ext = value.name.split('.')[-1].lower() ext = value.name.split('.')[-1].lower()
if ext not in ALLOWED_IMAGE_EXTENSIONS: if ext not in ALLOWED_IMAGE_EXTENSIONS:
@@ -76,7 +79,7 @@ class ProductForm(forms.Form):
widget = forms.ClearableFileInput( widget = forms.ClearableFileInput(
attrs = { attrs = {
'class': 'form-control', 'class': 'form-control',
'accept': 'image/*' 'accept': IMAGE_TYPE
} }
) )
) )
@@ -133,7 +136,7 @@ class SecondaryImageForm(forms.Form):
widget = forms.ClearableFileInput( widget = forms.ClearableFileInput(
attrs = { attrs = {
'class': 'form-control', 'class': 'form-control',
'accept': 'image/*' 'accept': IMAGE_TYPE
} }
) )
) )
@@ -192,7 +195,7 @@ class UserRegisterForm(forms.Form):
) )
) )
email = forms.EmailField( email = forms.EmailField(
label = "Correo Electrónico", label = EMAIL_FORMNAME,
max_length = 255, max_length = 255,
required = True, required = True,
widget = forms.TextInput( widget = forms.TextInput(
@@ -236,7 +239,7 @@ class UserRegisterForm(forms.Form):
password = cleaned_data.get("password") password = cleaned_data.get("password")
password_confirm = cleaned_data.get("password_confirm") password_confirm = cleaned_data.get("password_confirm")
if password and password_confirm and password != password_confirm: if password and password_confirm and password != password_confirm:
raise ValidationError("Las contraseñas no coinciden.") raise ValidationError(INCORRECT_PASSWORDS)
class EditProfileForm(forms.Form): class EditProfileForm(forms.Form):
@@ -253,7 +256,7 @@ class EditProfileForm(forms.Form):
widget=forms.TextInput(attrs={'class': 'form-control'}) widget=forms.TextInput(attrs={'class': 'form-control'})
) )
email = forms.EmailField( email = forms.EmailField(
label="Correo Electrónico", label=EMAIL_FORMNAME,
max_length=254, max_length=254,
required=True, required=True,
widget=forms.EmailInput(attrs={'class': 'form-control'}) widget=forms.EmailInput(attrs={'class': 'form-control'})
@@ -285,7 +288,7 @@ class ChangePasswordForm(forms.Form):
new_password = cleaned_data.get("new_password") new_password = cleaned_data.get("new_password")
confirm_password = cleaned_data.get("confirm_password") confirm_password = cleaned_data.get("confirm_password")
if new_password and confirm_password and new_password != confirm_password: if new_password and confirm_password and new_password != confirm_password:
raise ValidationError("Las contraseñas no coinciden.") raise ValidationError(INCORRECT_PASSWORDS)
if new_password and len(new_password) < 8: if new_password and len(new_password) < 8:
raise ValidationError("La contraseña debe tener al menos 8 caracteres.") raise ValidationError("La contraseña debe tener al menos 8 caracteres.")
@@ -343,7 +346,7 @@ class ShippingAddressForm(forms.Form):
class ResetPasswordForm(forms.Form): class ResetPasswordForm(forms.Form):
email = forms.EmailField( email = forms.EmailField(
label="Correo Electrónico", label=EMAIL_FORMNAME,
max_length=254, max_length=254,
required=True, required=True,
widget=forms.EmailInput(attrs={'class': 'form-control', 'placeholder': 'tu@email.com'}) widget=forms.EmailInput(attrs={'class': 'form-control', 'placeholder': 'tu@email.com'})
@@ -369,7 +372,7 @@ class ResetPasswordPhase2Form(forms.Form):
password = cleaned_data.get("password") password = cleaned_data.get("password")
verify_password = cleaned_data.get("verify_password") verify_password = cleaned_data.get("verify_password")
if password and verify_password and password != verify_password: if password and verify_password and password != verify_password:
raise ValidationError("Las contraseñas no coinciden.") raise ValidationError(INCORRECT_PASSWORDS)
class ReviewForm(forms.Form): class ReviewForm(forms.Form):