from django import forms from django.contrib.auth import authenticate from .models import normalize_mobile from apps.customers.models import CustomerProfile from apps.core.iran_locations import validate_location class MobileForm(forms.Form): mobile = forms.CharField(label="شماره موبایل") def clean_mobile(self): try: return normalize_mobile(self.cleaned_data["mobile"]) except ValueError as exc: raise forms.ValidationError(str(exc)) class OTPForm(forms.Form): code = forms.CharField(label="کد تایید", min_length=4, max_length=8, widget=forms.TextInput(attrs={"autocomplete":"one-time-code","inputmode":"numeric","pattern":"[0-9۰-۹]*","data-webotp":""})) class CustomerPasswordLoginForm(forms.Form): mobile=forms.CharField(label="شماره موبایل") password=forms.CharField(label="رمز عبور",widget=forms.PasswordInput) def clean_mobile(self): try:return normalize_mobile(self.cleaned_data["mobile"]) except ValueError as exc:raise forms.ValidationError(str(exc)) def authenticate(self,request): from .models import User user=User.objects.filter(mobile=self.cleaned_data["mobile"],account_type=User.AccountType.CUSTOMER).first() return authenticate(request,username=user.username,password=self.cleaned_data["password"]) if user else None class CustomerProfileForm(forms.ModelForm): password=forms.CharField(label="رمز عبور جدید",required=False,min_length=8,widget=forms.PasswordInput,help_text="برای حفظ رمز فعلی خالی بگذارید.") class Meta: model=CustomerProfile; fields=["first_name","last_name","customer_type","province","city"]; labels={"first_name":"نام","last_name":"نام خانوادگی","customer_type":"نوع مشتری","province":"استان","city":"شهر"} widgets={"province":forms.TextInput(attrs={"list":"profile-province-options","autocomplete":"off","data-province-input":""}),"city":forms.TextInput(attrs={"list":"profile-city-options","autocomplete":"off","data-city-input":""})} def clean(self): data=super().clean(); province,city,error=validate_location(data.get("province"),data.get("city")); data["province"],data["city"]=province,city if error: self.add_error("city" if city else "province",error) return data class StaffLoginForm(forms.Form): username = forms.CharField(label="نام کاربری") password = forms.CharField(label="رمز عبور", widget=forms.PasswordInput) def authenticate(self, request): return authenticate(request, username=self.cleaned_data["username"], password=self.cleaned_data["password"])