Skip to content

Commit

Permalink
Release 5.8.8
Browse files Browse the repository at this point in the history
#### Changelog:
* Feature(backend): Add experimental caching check function.
* Chore(deps): Upgrade backend MD dependency.
  • Loading branch information
onegreyonewhite committed Nov 1, 2023
1 parent 0cb4cb4 commit 9065fb9
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 13 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Main packages
configparserc~=2.0.0
Markdown~=3.5.0
Markdown~=3.5.1
django-environ~=0.11.2

# REST API packages
Expand Down
2 changes: 1 addition & 1 deletion test_src/test_proj/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3559,7 +3559,7 @@ def test_cacheable_model(self):
])
self.assertEqual(results[0]['status'], 200)
self.assertEqual(results[0]['data']['count'], 2)
self.assertEqual(results[1]['status'], 304)
self.assertEqual(results[1]['status'], 304, results[1]['data'])
self.assertEqual(results[2]['status'], 200)
self.assertEqual(results[2]['data']['id'], instance.id)
self.assertEqual(results[3]['status'], 304)
Expand Down
2 changes: 1 addition & 1 deletion vstutils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# pylint: disable=django-not-available
__version__: str = '5.8.7'
__version__: str = '5.8.8'
40 changes: 30 additions & 10 deletions vstutils/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,28 @@ class EtagDependency(enum.Flag):
LANG = enum.auto()


def check_request_etag(request, etag_value, header_name="If-None-Match", operation_handler=str.__eq__):
header = request.headers.get(header_name)

if not header:
return etag_value, False

header = str(header)
if header[:2] in {'W/', "w/"}:
header = header[2:]

if header[0] != '"':
header = f'"{header}"'

if etag_value[0] != '"':
etag_value = f'"{etag_value}"'

if operation_handler(etag_value, header):
return etag_value, True

return etag_value, False


class CachableHeadMixin(GenericViewSet):
"""
Mixin which cache GET responses.
Expand Down Expand Up @@ -550,16 +572,14 @@ def check_etag(self, request, model_class=None):
# For non-standart request methods
return # nocv

header = request.headers.get(header_name, None)
if not header:
return
data = self._get_etag(model_class or self.model_class, request)
header = str(header)
if header[:2] in ('W/', "w/"):
header = header[2:]
if header[0] != '"':
header = f'"{header}"'
if operation_handler(data, header):
_, match = check_request_etag(
request,
self.get_etag_value(model_class or self.model_class, request),
header_name,
operation_handler,
)

if match:
raise exception

def finalize_response(self, request, response, *args, **kwargs):
Expand Down
8 changes: 8 additions & 0 deletions vstutils/api/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ class EtagDependency(enum.Flag):
SESSION: enum.auto()
LANG: enum.auto()

def check_request_etag(
request: Request,
etag_value: T,
header_name: _t.Text = "If-None-Match",
operation_handler: _t.Callable[[_t.Text, _t.Text], bool] = str.__eq__
) -> _t.Tuple[T, bool]:
...

class CachableHeadMixin(GenericViewSet):
class NotModifiedException(exceptions.APIException): ...
class PreconditionFailedException(exceptions.APIException): ...
Expand Down

0 comments on commit 9065fb9

Please sign in to comment.