本文整理汇总了Python中django.contrib.auth.forms.AuthenticationForm.get_user方法的典型用法代码示例。如果您正苦于以下问题:Python AuthenticationForm.get_user方法的具体用法?Python AuthenticationForm.get_user怎么用?Python AuthenticationForm.get_user使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.contrib.auth.forms.AuthenticationForm
的用法示例。
在下文中一共展示了AuthenticationForm.get_user方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: openid_connect
# 需要导入模块: from django.contrib.auth.forms import AuthenticationForm [as 别名]
# 或者: from django.contrib.auth.forms.AuthenticationForm import get_user [as 别名]
def openid_connect(request):
if "openid" not in request.session:
return HttpResponseRedirect(reverse("chameleon_openid:openid_login"))
openid = request.session["openid"]
if request.method == "POST":
form = AuthenticationForm(request, data=request.POST)
if form.is_valid():
user = form.get_user()
openid_identity = OpenIDUserIdentity()
openid_identity.uid = openid["url"]
openid_identity.user = form.get_user()
openid_identity.save()
user = authenticate(openid_identity=openid["url"])
if user:
login(request, user)
messages.success(
request,
"You have successfully connected your OpenID account with your "
"Chameleon account. You are now logged in.",
)
return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
else:
form = AuthenticationForm(request)
try:
user = get_user_model().objects.filter(email=openid["sreg"]["email"])
except Exception, e:
user = None
logger.error(e)
示例2: post
# 需要导入模块: from django.contrib.auth.forms import AuthenticationForm [as 别名]
# 或者: from django.contrib.auth.forms.AuthenticationForm import get_user [as 别名]
def post(self, request):
"""
Process form data on POST requests
"""
auth_form = AuthenticationForm(data=request.POST)
if auth_form.is_valid():
login(request, auth_form.get_user())
# obtain JW token
jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
payload = jwt_payload_handler(auth_form.get_user())
user_token = jwt_encode_handler(payload)
response = HttpResponseRedirect(reverse('home'))
response.set_cookie('user_token', user_token)
response.set_cookie('user_id', request.user.id)
request.session.set_expiry(600)
return response
else:
for key in auth_form.errors:
for error in auth_form.errors[key]:
messages.add_message(request, messages.INFO, error)
args = {}
args.update(csrf(request))
args.update({'form': RegisterForm})
return render(
request, 'authenticate.html', {'form': RegisterForm}
)
示例3: post
# 需要导入模块: from django.contrib.auth.forms import AuthenticationForm [as 别名]
# 或者: from django.contrib.auth.forms.AuthenticationForm import get_user [as 别名]
def post(self, request, format):
form = AuthenticationForm(data=request.POST)
if form.is_valid():
# Okay, security checks complete. Log the user in.
# authenticate() called in AuthenticationForm
auth_login(request, form.get_user())
if request.session.test_cookie_worked():
request.session.delete_test_cookie()
redirect_to = _clean_redirect_uri(
request.REQUEST.get('next', ''),
request)
if 'json' == format:
return HttpResponse(json.dumps({
'done': True,
'user_id': form.get_user().id,
'is_signedin': True,
'go_to': redirect_to
}),
content_type='application/json')
elif 'html' == format:
return HttpResponseRedirect(redirect_to)
else:
if 'json' == format:
return HttpResponse(json.dumps({
'done': False,
'errors': form.errors
}),
content_type='application/json',
status=500)
elif 'html' == format:
return HttpResponse(form.errors.as_ul(), status=500)
示例4: login
# 需要导入模块: from django.contrib.auth.forms import AuthenticationForm [as 别名]
# 或者: from django.contrib.auth.forms.AuthenticationForm import get_user [as 别名]
def login(request):
if request.method == 'POST':
authform = AuthenticationForm(data=request.POST)
if authform.is_valid():
auth_login(request, authform.get_user())
return HttpResponseRedirect(reverse('users:user', args=(authform.get_user().username,)))
else:
authform = AuthenticationForm()
return render(request, 'users/login.html', { 'form': authform })
示例5: login_user
# 需要导入模块: from django.contrib.auth.forms import AuthenticationForm [as 别名]
# 或者: from django.contrib.auth.forms.AuthenticationForm import get_user [as 别名]
def login_user(request):
form = AuthenticationForm()
if request.method == "POST":
form = AuthenticationForm(request, data=request.POST)
if form.is_valid():
authenticated_user = form.get_user()
login(request, form.get_user())
return redirect('/', authenticated_user)
return render(request, 'login.html', {"form": form})
示例6: signin
# 需要导入模块: from django.contrib.auth.forms import AuthenticationForm [as 别名]
# 或者: from django.contrib.auth.forms.AuthenticationForm import get_user [as 别名]
def signin(request):
context = {}
if request.method == "GET":
form = AuthenticationForm
if request.method == "POST":
form = AuthenticationForm(data=request.POST)
if form.is_valid():
login(request, form.get_user())
print form.get_user()
print type(form.get_user())
return redirect(to="index")
context['form'] = form
return render(request, 'login.html', context)
示例7: login_user
# 需要导入模块: from django.contrib.auth.forms import AuthenticationForm [as 别名]
# 或者: from django.contrib.auth.forms.AuthenticationForm import get_user [as 别名]
def login_user(request):
form = AuthenticationForm(data=request.POST)
if form.is_valid():
login(request, form.get_user())
return JsonResponse({
'verified': True,
'feedback': 'Hello %s' % form.get_user()
})
else:
return JsonResponse({
'verified': False,
'feedback': 'Username or password is incorrect.'
})
示例8: login
# 需要导入模块: from django.contrib.auth.forms import AuthenticationForm [as 别名]
# 或者: from django.contrib.auth.forms.AuthenticationForm import get_user [as 别名]
def login(request, template_name="lfs/checkout/login.html"):
"""Displays a form to login or register/login the user within the check out
process.
The form's post request goes to lfs.customer.views.login where all the logic
happens - see there for more.
"""
# If the user is already authenticate we don't want to show this view at all
if request.user.is_authenticated():
return HttpResponseRedirect(reverse("lfs_checkout"))
shop = lfs.core.utils.get_default_shop(request)
# If only anonymous checkout allowed we don't want to show this view at all.
if shop.checkout_type == CHECKOUT_TYPE_ANON:
return HttpResponseRedirect(reverse("lfs_checkout"))
# Using Djangos default AuthenticationForm
login_form = AuthenticationForm()
login_form.fields["username"].label = _(u"E-Mail")
register_form = RegisterForm()
if request.POST.get("action") == "login":
login_form = AuthenticationForm(data=request.POST)
login_form.fields["username"].label = _(u"E-Mail")
if login_form.is_valid():
from django.contrib.auth import login
login(request, login_form.get_user())
return lfs.core.utils.set_message_cookie(reverse("lfs_checkout"),
msg=_(u"You have been logged in."))
elif request.POST.get("action") == "register":
register_form = RegisterForm(data=request.POST)
if register_form.is_valid():
email = register_form.data.get("email")
password = register_form.data.get("password_1")
# Create user
user = User.objects.create_user(
username=create_unique_username(email), email=email, password=password)
# Notify
lfs.core.signals.customer_added.send(user)
# Log in user
from django.contrib.auth import authenticate
user = authenticate(username=email, password=password)
from django.contrib.auth import login
login(request, user)
return lfs.core.utils.set_message_cookie(reverse("lfs_checkout"),
msg=_(u"You have been registered and logged in."))
return render_to_response(template_name, RequestContext(request, {
"login_form": login_form,
"register_form": register_form,
"anonymous_checkout": shop.checkout_type != CHECKOUT_TYPE_AUTH,
}))
示例9: authenticate_user
# 需要导入模块: from django.contrib.auth.forms import AuthenticationForm [as 别名]
# 或者: from django.contrib.auth.forms.AuthenticationForm import get_user [as 别名]
def authenticate_user(username, password):
# reuse form-model logic in AuthenticationForm
auth_dict = {"username": username, "password": password}
auth_form = AuthenticationForm(None, auth_dict)
is_valid = auth_form.is_valid() # just run the validation
auth_user = auth_form.get_user()
return auth_user
示例10: login
# 需要导入模块: from django.contrib.auth.forms import AuthenticationForm [as 别名]
# 或者: from django.contrib.auth.forms.AuthenticationForm import get_user [as 别名]
def login(request):
'''
Small wrapper around the django login view
'''
context = {}
if request.REQUEST.get('next'):
context['next'] = request.REQUEST.get('next')
# Sane login, with POST
if request.method == "POST":
return django_loginview(request,
template_name='user/login.html',
extra_context=context)
# log in with GET parameters so users can easily get in by scanning a QR code
elif request.REQUEST.get('get_login'):
form = AuthenticationForm(request, data=request.GET)
if form.is_valid():
auth_login(request, form.get_user())
return HttpResponseRedirect(reverse('oracle:dashboard'))
return django_loginview(request,
template_name='user/login.html',
extra_context=context)
示例11: is_authenticated
# 需要导入模块: from django.contrib.auth.forms import AuthenticationForm [as 别名]
# 或者: from django.contrib.auth.forms.AuthenticationForm import get_user [as 别名]
def is_authenticated(self, request):
"""
if user is_authenticated: return True
else try to authenticate with django and return true/false dependent of
result
"""
# is authenticated
if request.user.is_authenticated():
return True
# not authenticated, call authentication form
f = AuthenticationForm(data={
'username': request.REQUEST.get('username',''),
'password': request.REQUEST.get('password',''),
})
# if authenticated log the user in.
if f.is_valid():
auth_login(request, f.get_user())
# this ** should ** return true
return request.user.is_authenticated()
else:
# fail to auth, save form errors
#self.errors = f.errors
return False
示例12: login
# 需要导入模块: from django.contrib.auth.forms import AuthenticationForm [as 别名]
# 或者: from django.contrib.auth.forms.AuthenticationForm import get_user [as 别名]
def login(request):
result = {"status": 0, "apiVersion": "1.0", "msg": "", "data": {}}
form = AuthenticationForm(request, request.POST)
if not form.is_valid():
result['msg'] = u'用户名或者密码错误'
return json_response(result)
user = form.get_user()
if not user.is_active:
result['msg'] = u'该账户未激活,请先使用邮箱链接进行激活'
return json_response(result)
# if not check_user(user):
# result['msg'] = u'该用户不存在'
# return json_response(result)
if is_super_admin(user):
result['data']['next'] = '/superadmin/'
elif is_admin(user):
result['data']['next'] = '/secretary/'
elif is_instructor(user):
result['data']['next'] = '/instructor/'
# return HttpResponseRedirect(reverse('instructor'))
elif is_student(user):
result['data']['next'] = '/student/'
else:
result['msg'] = '该用户不存在'
return json_response(result)
auth_login(request, user)
result['status'] = 1
result['msg'] = u'登录成功'
return json_response(result)
示例13: login
# 需要导入模块: from django.contrib.auth.forms import AuthenticationForm [as 别名]
# 或者: from django.contrib.auth.forms.AuthenticationForm import get_user [as 别名]
def login(request, species_wid=None):
next = request.REQUEST.get('next', '')
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)
else:
form = AuthenticationForm(request)
request.session.set_test_cookie()
return render_queryset_to_response(
species_wid = species_wid,
request = request,
templateFile = 'public/login.html',
data = {
'form': form,
'next': next,
})
示例14: login
# 需要导入模块: from django.contrib.auth.forms import AuthenticationForm [as 别名]
# 或者: from django.contrib.auth.forms.AuthenticationForm import get_user [as 别名]
def login(request):
form = AuthenticationForm(data=request.POST)
valid_form = form.is_valid()
token_key = None
user_details = {}
if valid_form:
form.clean() # Calls authenticate(); will through ValidationException if form is invalid
user = form.get_user()
token, created = Token.objects.get_or_create(user=user)
utc_now = timezone.now()
if not created and token.created < utc_now - timedelta(hours=24):
token.delete()
token = Token.objects.create(user=user)
token.created = utc_now
token.save()
token_key = token.key
logger.info("Login Success: u:{0} e:{0}".format(user.username, user.email))
user_details = _getUserDetails(user)
else:
logger.info("Login Failed: {0}".format(_flattenFormErrors(form)))
return JsonResponse({
'success': valid_form,
'errors': form.errors,
'token': token_key,
'user': user_details,
})
示例15: login
# 需要导入模块: from django.contrib.auth.forms import AuthenticationForm [as 别名]
# 或者: from django.contrib.auth.forms.AuthenticationForm import get_user [as 别名]
def login(request):
from django.contrib.auth.forms import AuthenticationForm
from sentry.conf import settings
if request.user.is_authenticated():
return login_redirect(request)
form = AuthenticationForm(request, request.POST or None)
if form.is_valid():
login_user(request, form.get_user())
return login_redirect(request)
request.session.set_test_cookie()
context = csrf(request)
context.update(
{
"form": form,
"next": request.session.get("_next"),
"CAN_REGISTER": settings.ALLOW_REGISTRATION or request.session.get("can_register"),
"AUTH_PROVIDERS": get_auth_providers(),
"SOCIAL_AUTH_CREATE_USERS": dj_settings.SOCIAL_AUTH_CREATE_USERS,
}
)
return render_to_response("sentry/login.html", context, request)