本文整理匯總了Python中django.contrib.auth.get_user方法的典型用法代碼示例。如果您正苦於以下問題:Python auth.get_user方法的具體用法?Python auth.get_user怎麽用?Python auth.get_user使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.contrib.auth
的用法示例。
在下文中一共展示了auth.get_user方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_set_password
# 需要導入模塊: from django.contrib import auth [as 別名]
# 或者: from django.contrib.auth import get_user [as 別名]
def test_set_password(self):
username = 'test_new_user'
user = User.objects.create_user(username)
password = user.password
auth_user = auth.get_user(self.client)
self.assertNotEqual(user, auth_user)
set_password_url = reverse(set_password, args=[username])
response = self.client.post(set_password_url, content_type='application/json', data=json.dumps({}))
self.assertEqual(response.status_code, 400)
self.assertEqual(response.reason_phrase, 'Password is required')
response = self.client.post(set_password_url, content_type='application/json', data=json.dumps({
'password': 'password123', 'firstName': 'Test'}))
self.assertEqual(response.status_code, 200)
user = User.objects.get(username='test_new_user')
self.assertEqual(user.first_name, 'Test')
self.assertFalse(user.password == password)
auth_user = auth.get_user(self.client)
self.assertEqual(user, auth_user)
示例2: logout
# 需要導入模塊: from django.contrib import auth [as 別名]
# 或者: from django.contrib.auth import get_user [as 別名]
def logout(self):
"""
Removes the authenticated user's cookies and session object.
Causes the authenticated user to be logged out.
"""
from django.contrib.auth import get_user, logout
request = HttpRequest()
engine = import_module(settings.SESSION_ENGINE)
if self.session:
request.session = self.session
request.user = get_user(request)
else:
request.session = engine.SessionStore()
logout(request)
self.cookies = SimpleCookie()
示例3: get_cached_user
# 需要導入模塊: from django.contrib import auth [as 別名]
# 或者: from django.contrib.auth import get_user [as 別名]
def get_cached_user(request):
if not hasattr(request, '_cached_user'):
try:
key = CACHE_KEY.format(request.session[SESSION_KEY])
cache = caches['default']
user = cache.get(key)
except KeyError:
user = AnonymousUser()
if user is None:
user = get_user(request)
cache = caches['default']
# 8 hours
cache.set(key, user, 28800)
logger.debug('No User Cache. Setting now: {0}, {1}'.format(key, user.username))
request._cached_user = user
return request._cached_user
示例4: dispatch
# 需要導入模塊: from django.contrib import auth [as 別名]
# 或者: from django.contrib.auth import get_user [as 別名]
def dispatch(self, request, *args, **kwargs):
# get the game by the id
self.game = Game.get_by_id(kwargs['game_id'])
user = get_user(request)
# check to see if the game is open and available for this user
# if this player is the creator, just return
if self.game.creator == user or self.game.opponent == user:
return super(GameView, self).dispatch(request, *args, **kwargs)
# if there is no opponent and the game is not yet completed,
# set the opponent as this user
if not self.game.opponent and not self.game.completed:
self.game.opponent = user
self.game.save()
return super(GameView, self).dispatch(request, *args, **kwargs)
else:
messages.add_message(request, messages.ERROR, 'Sorry, the selected game is not available.')
return redirect('/lobby/')
示例5: test_verify_second_factor_u2f_view
# 需要導入模塊: from django.contrib import auth [as 別名]
# 或者: from django.contrib.auth import get_user [as 別名]
def test_verify_second_factor_u2f_view(self):
session = self.client.session
session['verfied_otp'] = False
session['verfied_u2f'] = False
session.save()
response = self.client.get(
reverse('mfa:verify_second_factor_u2f'), follow=True)
url = reverse('mfa:verify_second_factor') + \
"?next=" + settings.LOGIN_URL
self.assertRedirects(
response, expected_url=url, status_code=302)
session['u2f_pre_verify_user_pk'] = auth.get_user(self.client).id
session['u2f_pre_verify_user_backend'] = 'django.contrib.auth.backends.ModelBackend'
session.save()
response = self.client.get(reverse('mfa:verify_second_factor_u2f'))
self.assertTemplateUsed(response, "u2f/verify_second_factor_u2f.html")
# TODO : Need to Implement for Post Method
示例6: get_user_jwt
# 需要導入模塊: from django.contrib import auth [as 別名]
# 或者: from django.contrib.auth import get_user [as 別名]
def get_user_jwt(request):
"""
Process the request to find the user associated with the request
:param request: the request which is to be authenticated
:return: the user instance if authenticated, AnonymousUser() otherwise
"""
user = get_user(request)
if user.is_authenticated:
return user
try:
user_jwt, _ = JWTAuthentication().authenticate(Request(request))
if user_jwt is not None:
return user_jwt
except (AuthenticationFailed, InvalidToken, TokenError, TypeError):
# There may be other exceptions but these are the only ones I could see
pass
return user or AnonymousUser()
示例7: get_user
# 需要導入模塊: from django.contrib import auth [as 別名]
# 或者: from django.contrib.auth import get_user [as 別名]
def get_user(request):
if not hasattr(request, '_cached_user'):
request._cached_user = auth.get_user(request)
return request._cached_user
示例8: process_request
# 需要導入模塊: from django.contrib import auth [as 別名]
# 或者: from django.contrib.auth import get_user [as 別名]
def process_request(self, request):
assert hasattr(request, 'session'), (
"The Django authentication middleware requires session middleware "
"to be installed. Edit your MIDDLEWARE_CLASSES setting to insert "
"'django.contrib.sessions.middleware.SessionMiddleware' before "
"'django.contrib.auth.middleware.AuthenticationMiddleware'."
)
request.user = SimpleLazyObject(lambda: get_user(request))
示例9: force_login
# 需要導入模塊: from django.contrib import auth [as 別名]
# 或者: from django.contrib.auth import get_user [as 別名]
def force_login(self, user, backend=None):
def get_backend():
from django.contrib.auth import load_backend
for backend_path in settings.AUTHENTICATION_BACKENDS:
backend = load_backend(backend_path)
if hasattr(backend, 'get_user'):
return backend_path
if backend is None:
backend = get_backend()
user.backend = backend
self._login(user, backend)
示例10: logout
# 需要導入模塊: from django.contrib import auth [as 別名]
# 或者: from django.contrib.auth import get_user [as 別名]
def logout(self):
"""Log out the user by removing the cookies and session object."""
from django.contrib.auth import get_user, logout
request = HttpRequest()
engine = import_module(settings.SESSION_ENGINE)
if self.session:
request.session = self.session
request.user = get_user(request)
else:
request.session = engine.SessionStore()
logout(request)
self.cookies = SimpleCookie()
示例11: process_request
# 需要導入模塊: from django.contrib import auth [as 別名]
# 或者: from django.contrib.auth import get_user [as 別名]
def process_request(self, request):
assert hasattr(request, 'session'), (
"The Django authentication middleware requires session middleware "
"to be installed. Edit your MIDDLEWARE%s setting to insert "
"'django.contrib.sessions.middleware.SessionMiddleware' before "
"'django.contrib.auth.middleware.AuthenticationMiddleware'."
) % ("_CLASSES" if settings.MIDDLEWARE is None else "")
request.user = SimpleLazyObject(lambda: get_user(request))
示例12: test_login
# 需要導入模塊: from django.contrib import auth [as 別名]
# 或者: from django.contrib.auth import get_user [as 別名]
def test_login(self):
data = {'email': self.user.email, 'password': self.user.display_name}
response = self.client.post(self.url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(response.data['email'], self.user.email)
user = auth.get_user(self.client)
self.assertTrue(user.is_authenticated)
示例13: test_login_with_similar_email_succeeds
# 需要導入模塊: from django.contrib import auth [as 別名]
# 或者: from django.contrib.auth import get_user [as 別名]
def test_login_with_similar_email_succeeds(self):
data = {'email': 'User98@example.com', 'password': self.user.display_name}
response = self.client.post(self.url, data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(response.data['email'], self.user.email)
user = auth.get_user(self.client)
self.assertTrue(user.is_authenticated)
示例14: test_login_as_disabled_user_fails
# 需要導入模塊: from django.contrib import auth [as 別名]
# 或者: from django.contrib.auth import get_user [as 別名]
def test_login_as_disabled_user_fails(self):
data = {'email': self.disabled_user.email, 'password': self.disabled_user.display_name}
response = self.client.post(self.url, data, format='json')
self.assertEqual(response.data['non_field_errors'], [
'Unable to log in with provided credentials.',
])
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
user = auth.get_user(self.client)
self.assertFalse(user.is_authenticated)
示例15: test_logout
# 需要導入模塊: from django.contrib import auth [as 別名]
# 或者: from django.contrib.auth import get_user [as 別名]
def test_logout(self):
self.client.login(email=self.user.email, password=self.user.display_name)
user = auth.get_user(self.client)
self.assertTrue(user.is_authenticated)
response = self.client.post('/api/auth/logout/')
self.assertEqual(response.status_code, status.HTTP_200_OK)
user = auth.get_user(self.client)
self.assertFalse(user.is_authenticated)