def signin(request): next = request.GET.get('next', '') signin_form = SigninForm() if request.method != 'POST': return render(request, 'signin.html', locals()) signin_form = SigninForm(request.POST) if not signin_form.is_valid(): login_errors = True return render(request, 'signin.html', locals()) username_or_email = signin_form.cleaned_data['username_or_email'] password = signin_form.cleaned_data['password'] key = 'email__iexact' if '@' in username_or_email else 'username__iexact' if User.objects.filter(**{key: username_or_email}).exists(): user = User.objects.get(**{key: username_or_email}) user = authenticate(username=user.username, password=password) if user is None: login_errors = True return render(request, 'signin.html', locals()) else: login_errors = True return render(request, 'signin.html', locals()) django_login(request, user) if next == '': next = '/' return HttpResponseRedirect(next)
def test_user_valid_signinform(self): """ Tests if the user tries to signin to an account with valid values. """ data = { 'email': self.email, 'password': self.password } form = SigninForm(data=data) self.assertTrue(form.is_valid())
def test_user_wrong_pwd(self): """ Tests if the user tries to signin to an account with a wrong password. """ data = { 'email': self.email, 'password': self.pwd_confirm } form = SigninForm(data=data) self.assertTrue(form.is_valid()) with self.assertRaises(MaieuclicUser.DoesNotExist): MaieuclicUser.objects.get(email=form.cleaned_data["email"], password=form.cleaned_data["password"])
def test_user_wrong_email(self): """ Tests if the user tries to signin to an account with not known email. """ data = { 'email': self.signup_email, 'password': self.password } form = SigninForm(data=data) self.assertTrue(form.is_valid()) with self.assertRaises(MaieuclicUser.DoesNotExist): MaieuclicUser.objects.get(email=form.cleaned_data["email"])
def signin(request): if request.user.is_authenticated: return redirect('/') if request.method == 'POST': form = SigninForm(request.POST) if form.is_valid(): user = form.user login(request, user) return redirect('/profile/') else: form = SigninForm() context = {'form': form} return render(request, 'user/auth/signin.html', context)
def signin(request): auth_error = False form = SigninForm(data=request.POST) if form.is_valid(): email = form.cleaned_data.get('email') password = form.cleaned_data.get('password') user = authenticate(username=email, password=password) if user is not None and user.is_active: login(request, user) return redirect('/') else: auth_error = True return render(request, 'user/signin.html', { 'form': form, 'authentication_error': auth_error })