class LoginUserForm(forms.Form):
email = forms.CharField(
widget=forms.EmailInput(attrs={'autofocus': True, 'placeholder': 'Email'}),
required=True,
label='Email :',
max_length=255
)
password = forms.CharField(
widget=forms.PasswordInput(attrs={'placeholder': 'Password'}),
label=_('Password :'),
required=True,
max_length=50,
)
def clean(self, *args, **kwargs):
email = self.cleaned_data.get('email', None)
password = self.cleaned_data.get('password', None)
if email and password:
try:
loginuser = User.objects.get(email=email)
except ObjectDoesNotExist:
raise forms.ValidationError(_('Email or Password are Wrong!'))
if not loginuser.is_active:
raise forms.ValidationError(_('This User Does not Active.'))
if not loginuser.check_password(password):
raise forms.ValidationError(_('Email or Password are Wrong!'))
registration = Registration.objects.filter(user=loginuser, is_registered=True)
if not registration.exists():
reg_email(
to_list=[loginuser.email],
code=signing.dumps({'id':
loginuser.id}, salt=salt) + '/?reg_code=' + signing.dumps({'username': loginuser.username, 'id':
loginuser.id, 'email': loginuser.email}, salt=salt)
)
raise forms.ValidationError(_('This Account is not Registered!. Email has been sent. check your email'))
login(self.request, loginuser)
return super(LoginUserForm, self).clean()
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request', None)
super(LoginUserForm, self).__init__(*args, **kwargs)
class LoginUser(FormView):
template_name = 'public/registration/login.html'
form_class = LoginUserForm
success_url = reverse_lazy('home')
def get(self, request, *args, **kwargs):
"""When The Get Request Incoming."""
if request.user.is_authenticated:
return redirect(self.success_url)
return render(request, self.template_name, {'form': self.form_class()})
def post(self, request, *args, **kwargs):
"""When The Post Request Incoming. User Submit Login Form."""
next_url = request.GET.get('next', None)
form = self.form_class(request.POST or None, request=request)
if form.is_valid():
if next_url:
return redirect(next_url)
return redirect(self.success_url)
return render(request, self.template_name, {'form': form})