Coverage for jutil/auth.py: 91%

# Coverage for jutil/auth.py : 91%

## 19 statements

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1 import logging

2 from typing import Union, Optional

3 from django.contrib.auth.models import User

4 from django.http.request import HttpRequest

5 from rest_framework.exceptions import NotAuthenticated

6 from rest_framework.request import Request

7

8 logger = logging.getLogger(__name__)

9

10

11 def get_auth_user(request: Union[Request, HttpRequest]) -> User:

12 """

13 Returns authenticated User.

14 :param request: HttpRequest

15 :return: User

16 """

17 if not request.user or not request.user.is_authenticated:

18 raise NotAuthenticated()

19 return request.user # type: ignore

20

21

22 def get_auth_user_or_none(request: Union[Request, HttpRequest]) -> Optional[User]:

23 """

24 Returns authenticated User or None if not authenticated.

25 :param request: HttpRequest

26 :return: User

27 """

28 if not request.user or not request.user.is_authenticated: 28 ↛ 30line 28 didn't jump to line 30, because the condition on line 28 was never false

29 return None

30 return request.user # type: ignore

31

32

33 class AuthUserMixin:

34 @property

35 def auth_user(self) -> User:

36 """

37 Returns authenticated user.

38 :return: User

39 """

40 return get_auth_user(self.request) # type: ignore