From 6ec0f4e732238acde14d8b2b2ddf9fec608d1b15 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 26 May 2026 10:19:21 +0200 Subject: [PATCH] feat: add constants for image types and error messages in forms --- tienda/constants.py | 3 +++ tienda/forms.py | 21 ++++++++++++--------- 2 files changed, 15 insertions(+), 9 deletions(-) create mode 100644 tienda/constants.py diff --git a/tienda/constants.py b/tienda/constants.py new file mode 100644 index 0000000..2373592 --- /dev/null +++ b/tienda/constants.py @@ -0,0 +1,3 @@ +IMAGE_TYPE = "image/*" +EMAIL_FORMNAME = "Correo Electrónico" +INCORRECT_PASSWORDS = "Las contraseñas no coinciden" \ No newline at end of file diff --git a/tienda/forms.py b/tienda/forms.py index 3701a74..46ae31d 100644 --- a/tienda/forms.py +++ b/tienda/forms.py @@ -2,10 +2,13 @@ from django import forms from django.core.exceptions import ValidationError from django.core.validators import FileExtensionValidator, MinLengthValidator, MaxLengthValidator from .models import Category - +from .constants import * ALLOWED_IMAGE_EXTENSIONS = ['jpg', 'jpeg', 'png', 'gif', 'webp'] ALLOWED_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'] + + + def validate_image_file(value): ext = value.name.split('.')[-1].lower() if ext not in ALLOWED_IMAGE_EXTENSIONS: @@ -76,7 +79,7 @@ class ProductForm(forms.Form): widget = forms.ClearableFileInput( attrs = { 'class': 'form-control', - 'accept': 'image/*' + 'accept': IMAGE_TYPE } ) ) @@ -133,7 +136,7 @@ class SecondaryImageForm(forms.Form): widget = forms.ClearableFileInput( attrs = { 'class': 'form-control', - 'accept': 'image/*' + 'accept': IMAGE_TYPE } ) ) @@ -192,7 +195,7 @@ class UserRegisterForm(forms.Form): ) ) email = forms.EmailField( - label = "Correo Electrónico", + label = EMAIL_FORMNAME, max_length = 255, required = True, widget = forms.TextInput( @@ -236,7 +239,7 @@ class UserRegisterForm(forms.Form): password = cleaned_data.get("password") password_confirm = cleaned_data.get("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): @@ -253,7 +256,7 @@ class EditProfileForm(forms.Form): widget=forms.TextInput(attrs={'class': 'form-control'}) ) email = forms.EmailField( - label="Correo Electrónico", + label=EMAIL_FORMNAME, max_length=254, required=True, widget=forms.EmailInput(attrs={'class': 'form-control'}) @@ -285,7 +288,7 @@ class ChangePasswordForm(forms.Form): new_password = cleaned_data.get("new_password") confirm_password = cleaned_data.get("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: raise ValidationError("La contraseña debe tener al menos 8 caracteres.") @@ -343,7 +346,7 @@ class ShippingAddressForm(forms.Form): class ResetPasswordForm(forms.Form): email = forms.EmailField( - label="Correo Electrónico", + label=EMAIL_FORMNAME, max_length=254, required=True, widget=forms.EmailInput(attrs={'class': 'form-control', 'placeholder': 'tu@email.com'}) @@ -369,7 +372,7 @@ class ResetPasswordPhase2Form(forms.Form): password = cleaned_data.get("password") verify_password = cleaned_data.get("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):