Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/django/views/decorators/http.py: 34%

55 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1""" 

2Decorators for views based on HTTP headers. 

3""" 

4 

5from functools import wraps 

6 

7from django.http import HttpResponseNotAllowed 

8from django.middleware.http import ConditionalGetMiddleware 

9from django.utils import timezone 

10from django.utils.cache import get_conditional_response 

11from django.utils.decorators import decorator_from_middleware 

12from django.utils.http import http_date, quote_etag 

13from django.utils.log import log_response 

14 

15conditional_page = decorator_from_middleware(ConditionalGetMiddleware) 

16 

17 

18def require_http_methods(request_method_list): 

19 """ 

20 Decorator to make a view only accept particular request methods. Usage:: 

21 

22 @require_http_methods(["GET", "POST"]) 

23 def my_view(request): 

24 # I can assume now that only GET or POST requests make it this far 

25 # ... 

26 

27 Note that request methods should be in uppercase. 

28 """ 

29 

30 def decorator(func): 

31 @wraps(func) 

32 def inner(request, *args, **kwargs): 

33 if request.method not in request_method_list: 

34 response = HttpResponseNotAllowed(request_method_list) 

35 log_response( 

36 "Method Not Allowed (%s): %s", 

37 request.method, 

38 request.path, 

39 response=response, 

40 request=request, 

41 ) 

42 return response 

43 return func(request, *args, **kwargs) 

44 

45 return inner 

46 

47 return decorator 

48 

49 

50require_GET = require_http_methods(["GET"]) 

51require_GET.__doc__ = "Decorator to require that a view only accepts the GET method." 

52 

53require_POST = require_http_methods(["POST"]) 

54require_POST.__doc__ = "Decorator to require that a view only accepts the POST method." 

55 

56require_safe = require_http_methods(["GET", "HEAD"]) 

57require_safe.__doc__ = ( 

58 "Decorator to require that a view only accepts safe methods: GET and HEAD." 

59) 

60 

61 

62def condition(etag_func=None, last_modified_func=None): 

63 """ 

64 Decorator to support conditional retrieval (or change) for a view 

65 function. 

66 

67 The parameters are callables to compute the ETag and last modified time for 

68 the requested resource, respectively. The callables are passed the same 

69 parameters as the view itself. The ETag function should return a string (or 

70 None if the resource doesn't exist), while the last_modified function 

71 should return a datetime object (or None if the resource doesn't exist). 

72 

73 The ETag function should return a complete ETag, including quotes (e.g. 

74 '"etag"'), since that's the only way to distinguish between weak and strong 

75 ETags. If an unquoted ETag is returned (e.g. 'etag'), it will be converted 

76 to a strong ETag by adding quotes. 

77 

78 This decorator will either pass control to the wrapped view function or 

79 return an HTTP 304 response (unmodified) or 412 response (precondition 

80 failed), depending upon the request method. In either case, the decorator 

81 will add the generated ETag and Last-Modified headers to the response if 

82 the headers aren't already set and if the request's method is safe. 

83 """ 

84 

85 def decorator(func): 

86 @wraps(func) 

87 def inner(request, *args, **kwargs): 

88 # Compute values (if any) for the requested resource. 

89 def get_last_modified(): 

90 if last_modified_func: 

91 dt = last_modified_func(request, *args, **kwargs) 

92 if dt: 

93 if not timezone.is_aware(dt): 

94 dt = timezone.make_aware(dt, timezone.utc) 

95 return int(dt.timestamp()) 

96 

97 # The value from etag_func() could be quoted or unquoted. 

98 res_etag = etag_func(request, *args, **kwargs) if etag_func else None 

99 res_etag = quote_etag(res_etag) if res_etag is not None else None 

100 res_last_modified = get_last_modified() 

101 

102 response = get_conditional_response( 

103 request, 

104 etag=res_etag, 

105 last_modified=res_last_modified, 

106 ) 

107 

108 if response is None: 

109 response = func(request, *args, **kwargs) 

110 

111 # Set relevant headers on the response if they don't already exist 

112 # and if the request method is safe. 

113 if request.method in ("GET", "HEAD"): 

114 if res_last_modified and not response.has_header("Last-Modified"): 

115 response.headers["Last-Modified"] = http_date(res_last_modified) 

116 if res_etag: 

117 response.headers.setdefault("ETag", res_etag) 

118 

119 return response 

120 

121 return inner 

122 

123 return decorator 

124 

125 

126# Shortcut decorators for common cases based on ETag or Last-Modified only 

127def etag(etag_func): 

128 return condition(etag_func=etag_func) 

129 

130 

131def last_modified(last_modified_func): 

132 return condition(last_modified_func=last_modified_func)