本文整理汇总了Python中users.forms.AuthenticationForm.is_valid方法的典型用法代码示例。如果您正苦于以下问题:Python AuthenticationForm.is_valid方法的具体用法?Python AuthenticationForm.is_valid怎么用?Python AuthenticationForm.is_valid使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类users.forms.AuthenticationForm
的用法示例。
在下文中一共展示了AuthenticationForm.is_valid方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_only_active
# 需要导入模块: from users.forms import AuthenticationForm [as 别名]
# 或者: from users.forms.AuthenticationForm import is_valid [as 别名]
def test_only_active(self):
# Verify with active user
form = AuthenticationForm(data={'username': self.active_user.username,
'password': 'testpass'})
assert form.is_valid()
# Verify with inactive user
form = AuthenticationForm(data={
'username': self.inactive_user.username,
'password': 'testpass'})
assert not form.is_valid()
示例2: test_recaptcha_errors_only
# 需要导入模块: from users.forms import AuthenticationForm [as 别名]
# 或者: from users.forms.AuthenticationForm import is_valid [as 别名]
def test_recaptcha_errors_only(self):
"""Only recaptcha errors should be returned if validation fails.
We don't want any information on the username/password returned if the
captcha is incorrect.
"""
form = AuthenticationForm(
data={"username": "foo", "password": "barpassword", "recaptcha": ""}, use_recaptcha=True
)
form.is_valid()
assert len(form.errors) == 1
assert "recaptcha" in form.errors
示例3: test_recaptcha_errors_only
# 需要导入模块: from users.forms import AuthenticationForm [as 别名]
# 或者: from users.forms.AuthenticationForm import is_valid [as 别名]
def test_recaptcha_errors_only(self):
"""Only recaptcha errors should be returned if validation fails.
We don't want any information on the username/password returned if the
captcha is incorrect.
"""
form = AuthenticationForm(data={'username': 'foo',
'password': 'barpassword',
'recaptcha': ''},
use_recaptcha=True)
form.is_valid()
assert len(form.errors) == 1
assert 'recaptcha' in form.errors
示例4: test_only_active
# 需要导入模块: from users.forms import AuthenticationForm [as 别名]
# 或者: from users.forms.AuthenticationForm import is_valid [as 别名]
def test_only_active(self):
# Verify with active user
u = User.objects.get(username='rrosario')
assert u.is_active
form = AuthenticationForm(data={'username': 'rrosario',
'password': 'testpass'})
assert form.is_valid()
# Verify with inactive user
u.is_active = False
u.save()
u = User.objects.get(username='rrosario')
assert not u.is_active
form = AuthenticationForm(data={'username': 'rrosario',
'password': 'testpass'})
assert not form.is_valid()
示例5: test_allow_inactive
# 需要导入模块: from users.forms import AuthenticationForm [as 别名]
# 或者: from users.forms.AuthenticationForm import is_valid [as 别名]
def test_allow_inactive(self):
# Verify with active user
user = User.objects.get(username='testuser')
assert user.is_active
form = AuthenticationForm(only_active=False,
data={'username': 'testuser',
'password': 'testpass'})
assert form.is_valid()
# Verify with inactive user
user.is_active = False
user.save()
user = User.objects.get(username='testuser')
assert not user.is_active
form = AuthenticationForm(only_active=False,
data={'username': 'testuser',
'password': 'testpass'})
assert form.is_valid()
示例6: login
# 需要导入模块: from users.forms import AuthenticationForm [as 别名]
# 或者: from users.forms.AuthenticationForm import is_valid [as 别名]
def login(request):
if request.method == 'GET':
return {'logged_in': request.user.is_authenticated()}
from users.forms import AuthenticationForm
form = AuthenticationForm(data=request.POST)
if form.is_valid():
auth_login(request, form.get_user())
return {'ok': True}
else:
return {'form_errors': form.errors}
示例7: handle_signin
# 需要导入模块: from users.forms import AuthenticationForm [as 别名]
# 或者: from users.forms.AuthenticationForm import is_valid [as 别名]
def handle_signin(request):
"""Helper function that signs a user in."""
auth.logout(request)
if request.method == 'POST':
form = AuthenticationForm(data=request.POST)
if form.is_valid():
auth.login(request, form.get_user())
if request.session.test_cookie_worked():
request.session.delete_test_cookie()
return form
request.session.set_test_cookie()
return AuthenticationForm()
示例8: handle_login
# 需要导入模块: from users.forms import AuthenticationForm [as 别名]
# 或者: from users.forms.AuthenticationForm import is_valid [as 别名]
def handle_login(request, only_active=True):
if request.method == 'POST':
form = AuthenticationForm(data=request.POST, only_active=only_active)
if form.is_valid():
auth.login(request, form.get_user())
if request.session.test_cookie_worked():
request.session.delete_test_cookie()
return form
request.session.set_test_cookie()
return AuthenticationForm()
示例9: login
# 需要导入模块: from users.forms import AuthenticationForm [as 别名]
# 或者: from users.forms.AuthenticationForm import is_valid [as 别名]
def login(request):
form = AuthenticationForm()
if request.method == "POST":
form = AuthenticationForm(data=request.POST)
if form.is_valid():
user = authenticate(username=request.POST["username"], password=request.POST["password"])
if user is not None:
if user.is_active:
django_login(request, user)
if user.is_teacher:
return redirect("/courses/")
else:
return redirect("/courses/s")
return render(request, "login.html", {"form": form})
示例10: home
# 需要导入模块: from users.forms import AuthenticationForm [as 别名]
# 或者: from users.forms.AuthenticationForm import is_valid [as 别名]
def home(request):
if not request.user.is_authenticated():
form = AuthenticationForm()
if request.method == 'POST':
form = AuthenticationForm(data=request.POST)
if form.is_valid():
user = authenticate(username=request.POST['username'],
password=request.POST['password'])
if user is not None:
if user.is_active:
django_login(request, user)
if user.is_teacher:
return redirect('/courses/')
else:
return redirect('/courses/s')
return render(request, 'home.html', {'form': form})
if request.user.is_teacher:
return redirect('/courses/')
return redirect('/courses/s')
示例11: login
# 需要导入模块: from users.forms import AuthenticationForm [as 别名]
# 或者: from users.forms.AuthenticationForm import is_valid [as 别名]
def login(request):
"""Try to log the user in."""
next_url = _clean_next_url(request) or settings.LOGIN_REDIRECT_URL
if request.user.is_authenticated():
return HttpResponseRedirect(next_url)
if request.method == 'POST':
form = AuthenticationForm(data=request.POST)
if form.is_valid():
auth.login(request, form.get_user())
if request.session.test_cookie_worked():
request.session.delete_test_cookie()
return HttpResponseRedirect(next_url)
else:
form = AuthenticationForm(request)
request.session.set_test_cookie()
return jingo.render(request, 'users/login.html',
{'form': form, 'next_url': next_url})
示例12: browserid_register
# 需要导入模块: from users.forms import AuthenticationForm [as 别名]
# 或者: from users.forms.AuthenticationForm import is_valid [as 别名]
def browserid_register(request):
"""Handle user creation when assertion is valid, but no existing user"""
redirect_to = request.session.get(SESSION_REDIRECT_TO,
getattr(settings, 'LOGIN_REDIRECT_URL', reverse('home')))
email = request.session.get(SESSION_VERIFIED_EMAIL, None)
if not email:
# This is pointless without a verified email.
return HttpResponseRedirect(redirect_to)
# Set up the initial forms
register_form = BrowserIDRegisterForm()
login_form = AuthenticationForm()
if request.method == 'POST':
# If the profile creation form was submitted...
if 'register' == request.POST.get('action', None):
register_form = BrowserIDRegisterForm(request.POST)
if register_form.is_valid():
try:
# If the registration form is valid, then create a new
# Django user, a new MindTouch user, and link the two
# together.
# TODO: This all belongs in model classes
username = register_form.cleaned_data['username']
user = User.objects.create(username=username, email=email)
user.set_unusable_password()
user.save()
profile = UserProfile.objects.create(user=user)
deki_user = DekiUserBackend.post_mindtouch_user(user)
profile.deki_user_id = deki_user.id
profile.save()
user.backend = 'django_browserid.auth.BrowserIDBackend'
auth.login(request, user)
# Bounce to the newly created profile page, since the user
# might want to review & edit.
redirect_to = request.session.get(SESSION_REDIRECT_TO,
profile.get_absolute_url())
return set_browserid_explained(
_redirect_with_mindtouch_login(redirect_to,
user.username))
except MindTouchAPIError:
if user:
user.delete()
return jingo.render(request, '500.html',
{'error_message': "We couldn't "
"register a new account at this time. "
"Please try again later."})
else:
# If login was valid, then set to the verified email
login_form = handle_login(request)
if login_form.is_valid():
if request.user.is_authenticated():
# Change email to new verified email, for next time
user = request.user
user.email = email
user.save()
return _redirect_with_mindtouch_login(redirect_to,
login_form.cleaned_data.get('username'),
login_form.cleaned_data.get('password'))
# HACK: Pretend the session was modified. Otherwise, the data disappears
# for the next request.
request.session.modified = True
return jingo.render(request, 'users/browserid_register.html',
{'login_form': login_form,
'register_form': register_form})
示例13: test_if_not_valid_on_empty_field_except_is_teacher
# 需要导入模块: from users.forms import AuthenticationForm [as 别名]
# 或者: from users.forms.AuthenticationForm import is_valid [as 别名]
def test_if_not_valid_on_empty_field_except_is_teacher(self):
form_data = {'username': 'example', 'password': ''}
form = AuthenticationForm(data=form_data)
self.assertFalse(form.is_valid())
示例14: test_if_valid_on_empty_is_teacher
# 需要导入模块: from users.forms import AuthenticationForm [as 别名]
# 或者: from users.forms.AuthenticationForm import is_valid [as 别名]
def test_if_valid_on_empty_is_teacher(self):
form_data = {'username': 'example', 'is_teacher': '',
'password': 'example'}
form = AuthenticationForm(data=form_data)
self.assertTrue(form.is_valid())