Files
santeh-ray/orders/forms.py
Azimkin ad962d946b
All checks were successful
Build and Push Docker Image / build (push) Successful in 43s
feat: update order payment methods and refine profile edit functionality
- Replaced `Order.PaymentMethod` options with "cash" and "card" while adjusting related code and templates
- Set "cash" as the default payment method for orders and checkout forms
- Added profile editing functionality with `ProfileForm`, allowing users to update names
- Improved feedback messages across profile and order management templates
- Updated catalog to handle empty filter results with a "reset filters" option
2026-05-30 12:25:19 +02:00

66 lines
2.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import re
from django import forms
from django.core.exceptions import ValidationError
from orders.models import Order
PHONE_RE = re.compile(r"^\+?[\d\s\-()]{7,20}$")
class CheckoutForm(forms.Form):
delivery_method = forms.ChoiceField(
choices=Order.DeliveryMethod.choices,
initial=Order.DeliveryMethod.COURIER,
widget=forms.RadioSelect,
label="Способ доставки",
)
region = forms.CharField(max_length=255, required=False, label="Регион")
address = forms.CharField(max_length=512, required=False, label="Адрес")
payment_method = forms.ChoiceField(
choices=Order.PaymentMethod.choices,
initial=Order.PaymentMethod.CASH,
widget=forms.RadioSelect,
label="Способ оплаты",
)
customer_name = forms.CharField(max_length=255, label="ФИО")
phone = forms.CharField(max_length=32, label="Телефон")
email = forms.EmailField(label="E-mail")
recipient_matches_customer = forms.BooleanField(required=False, initial=True)
promo_code = forms.CharField(max_length=64, required=False, label="Промокод")
terms_accepted = forms.BooleanField(required=True, label="Согласие с условиями")
def clean_email(self):
return self.cleaned_data["email"].strip().lower()
def clean_promo_code(self):
return self.cleaned_data["promo_code"].strip()
def clean_customer_name(self):
return self.cleaned_data["customer_name"].strip()
def clean_phone(self):
phone = self.cleaned_data["phone"].strip()
if not PHONE_RE.match(phone):
raise ValidationError("Укажите корректный номер телефона.")
return phone
def clean_region(self):
return self.cleaned_data["region"].strip()
def clean_address(self):
return self.cleaned_data["address"].strip()
def clean(self):
cleaned_data = super().clean()
delivery_method = cleaned_data.get("delivery_method")
if delivery_method == Order.DeliveryMethod.COURIER:
if not cleaned_data.get("region"):
self.add_error("region", "Укажите регион доставки.")
if not cleaned_data.get("address"):
self.add_error("address", "Укажите адрес доставки.")
return cleaned_data