Hide keyboard shortcuts

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

1import logging 

2import traceback 

3from django.conf import settings 

4from django.http import HttpRequest 

5from django.utils import timezone 

6from ipware.ip import get_real_ip # type: ignore # pytype: disable=import-error 

7from jutil.email import send_email 

8 

9 

10logger = logging.getLogger(__name__) 

11 

12 

13class EnsureOriginMiddleware: 

14 """ 

15 Ensures that META HTTP_ORIGIN is set. 

16 """ 

17 

18 def __init__(self, get_response=None): 

19 self.get_response = get_response 

20 

21 def __call__(self, request): 

22 # Code to be executed for each request before 

23 # the view (and later middleware) are called. 

24 hostname = request.get_host() 

25 if not request.META.get('HTTP_ORIGIN', None): 

26 request.META['HTTP_ORIGIN'] = hostname 

27 

28 # get response 

29 response = self.get_response(request) 

30 

31 # Code to be executed for each request/response after 

32 # the view is called. 

33 return response 

34 

35 

36class LogExceptionMiddleware: 

37 """ 

38 Logs exception and sends email to admins about it. 

39 Uses list of emails from settings.ADMINS. 

40 """ 

41 

42 def __init__(self, get_response=None): 

43 self.get_response = get_response 

44 

45 def __call__(self, request): 

46 return self.get_response(request) 

47 

48 def process_exception(self, request, e): 

49 """ 

50 Logs exception error message and sends email to ADMINS if hostname is not testserver and DEBUG=False. 

51 :param request: HttpRequest 

52 :param e: Exception 

53 """ 

54 assert isinstance(request, HttpRequest) 

55 full_path = request.get_full_path() 

56 user = request.user 

57 msg = '{full_path}\n{err} (IP={ip}, user={user}) {trace}'.format(full_path=full_path, user=user, 

58 ip=get_real_ip(request), err=e, 

59 trace=str(traceback.format_exc())) 

60 logger.error(msg) 

61 hostname = request.get_host() 

62 if not settings.DEBUG and hostname != 'testserver': 

63 send_email(settings.ADMINS, 'Error @ {}'.format(hostname), msg) 

64 

65 

66class EnsureLanguageCookieMiddleware: 

67 """ 

68 Ensures language cookie (by name settings.LANGUAGE_COOKIE_NAME) is set. 

69 Sets it as settings.LANGUAGE_CODE if missing. 

70 Allows changing settings by passing querystring parameter named settings.LANGUAGE_COOKIE_NAME 

71 (default: django_language). 

72 """ 

73 def __init__(self, get_response=None): 

74 self.get_response = get_response 

75 

76 def __call__(self, request): 

77 lang_cookie_name = settings.LANGUAGE_COOKIE_NAME if hasattr(settings, 'LANGUAGE_COOKIE_NAME') else 'django_language' 

78 lang_cookie = request.COOKIES.get(lang_cookie_name) 

79 lang = request.GET.get(lang_cookie_name) 

80 if not lang: 

81 lang = lang_cookie 

82 if not lang or lang not in [lang[0] for lang in settings.LANGUAGES]: 

83 lang = settings.LANGUAGE_CODE if hasattr(settings, 'LANGUAGE_CODE') else 'en' 

84 request.COOKIES[lang_cookie_name] = lang 

85 

86 res = self.get_response(request) 

87 if request.COOKIES[lang_cookie_name] != lang_cookie: 

88 secure = hasattr(settings, 'LANGUAGE_COOKIE_SECURE') and settings.LANGUAGE_COOKIE_SECURE 

89 httponly = hasattr(settings, 'LANGUAGE_COOKIE_HTTPONLY') and settings.LANGUAGE_COOKIE_HTTPONLY 

90 res.set_cookie(lang_cookie_name, lang, secure=secure, httponly=httponly) 

91 return res 

92 

93 

94class ActivateUserProfileTimezone: 

95 """ 

96 Uses 'timezone' string in request.user.profile to activate user-specific timezone. 

97 """ 

98 def __init__(self, get_response=None): 

99 self.get_response = get_response 

100 

101 def __call__(self, request): 

102 # Code to be executed for each request before 

103 # the view (and later middleware) are called. 

104 if request.user.is_authenticated: 

105 timezone.activate(request.user.profile.timezone) 

106 

107 # get response 

108 response = self.get_response(request) 

109 

110 # Code to be executed for each request/response after 

111 # the view is called. 

112 timezone.deactivate() 

113 return response