from django import forms from accounts.models import Profile from random import randint from accounts.utils import normalize_phone from django.contrib.auth import authenticate class LoginForm(forms.Form): phone = forms.CharField(label='Номер мобильного телефона', max_length=45, required=True) password = forms.CharField(label='Пароль', widget=forms.PasswordInput, max_length=45, required=True) def clean_phone(self): data = normalize_phone(self.cleaned_data['phone']) try: profile = Profile.objects.get(phone=data) except Profile.DoesNotExist: raise forms.ValidationError("Вы еще не совершали покупки в нашем магазине") return data def clean(self): phone = normalize_phone(self.cleaned_data['phone']) password = self.cleaned_data['password'] try: profile = Profile.objects.get(phone=phone) user = authenticate(username=profile.phone, password=password) if not user: raise forms.ValidationError("Такой комбинации номер телефона/пароль не существует") except Profile.DoesNotExist: raise forms.ValidationError("Вы еще не совершали покупки в нашем магазине") return self.cleaned_data class LoginSmsForm(forms.Form): sms = forms.IntegerField(label='Одноразовый пароль', required=True) def clean_sms(self): data = self.cleaned_data['sms'] phone = normalize_phone(self.phone) try: profile = Profile.objects.get(phone=phone, temp_password=data) self.profile = profile # profile.temp_password = randint(1000000, 9999999) # self.temp_password = profile.temp_password # profile.save() except Profile.DoesNotExist: raise forms.ValidationError("Неверный одноразовый пароль") return data