def test_inactive_user(self): data = { 'username': '******', 'password': '******', } form = AdminAuthenticationForm(None, data) self.assertEqual(form.non_field_errors(), ['This account is inactive.'])
def test_can_login_to_admin_panel(self): data = { 'username': self.user.email, 'password': self.password, } form = AdminAuthenticationForm(None, data) self.assertEquals(form.is_valid(), True)
def test_inactive_user(self): data = { "username": "******", "password": "******", } form = AdminAuthenticationForm(None, data) self.assertEqual(form.non_field_errors(), ["This account is inactive."])
def login(request): if request.method == 'GET': form = AdminAuthenticationForm(request) return render(request, 'login.html', { 'app_path': "/login", 'form': form }) elif request.method == 'POST': username = request.POST['username'] password = request.POST['password'] user = authenticate(username=username, password=password) if user is not None: if user.is_active: django.contrib.auth.login(request, user) request.session['username'] = username # Redirect to a success page. index_path = "/" # from django.shortcuts import redirect # return redirect(index_path) return HttpResponseRedirect(index_path) else: # Return a 'disabled account' error message return HttpResponseForbidden() else: # Return an 'invalid login' error message. return HttpResponseForbidden()
def login(request): """ Display the login form for the given HttpRequest. """ self = AdminSite() if request.method == 'GET' and request.user.is_active and request.user.is_staff: # Already logged-in, redirect to admin index index_path = reverse('admin:index', current_app=self.name) return HttpResponseRedirect(index_path) request.current_app = self.name user = authenticate(username=request.POST.get('username'), password=request.POST.get('password')) form_instance = AdminAuthenticationForm() if user is not None and user.is_staff: django_login(request, user) index_path = reverse('admin:index', current_app=self.name) return HttpResponseRedirect(index_path) elif (user is not None) or (user is None and request.method == 'POST'): error = form_instance.get_invalid_login_error() form_instance.cleaned_data = [] form_instance.add_error(field=None, error=error) return render(request, 'admin/login.html', {'form': form_instance, 'title': 'Log in'})