Skip to content

Commit

Permalink
Actual request warning for Django cache_page
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowone committed Jun 8, 2018
1 parent 810292b commit 70acd15
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ MANIFEST
.*.swo
/build
.coverage
.pytest_cache
25 changes: 17 additions & 8 deletions ring/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
from __future__ import absolute_import

import warnings
from typing import Any, Optional, Tuple
from django.core import cache as django_cache
from django.http.request import HttpRequest
Expand Down Expand Up @@ -50,16 +51,18 @@ def transform_cache_page_args(wire, rules, args, kwargs):
raise TypeError
request = HttpRequest()
request.__dict__.update(template_request.__dict__)
request._fake_request = True
request.method = 'GET'
try:
path = reverse(path_hint)
except NoReverseMatch:
path = path_hint
request.path = path
if path_hint is not None:
try:
path = reverse(path_hint)
except NoReverseMatch:
path = path_hint
request.path = path
else:
request = raw_request # type error?

return (request,) + args[1:], kwargs
return (request, ) + args[1:], kwargs


class CachePageUserInterface(fbase.BaseUserInterface):
Expand Down Expand Up @@ -157,8 +160,14 @@ def get_or_update(self, wire, request, *args, **kwargs):

@fbase.interface_attrs(
transform_args=transform_cache_page_args, return_annotation=None)
def delete(self, *args, **kwargs):
key_get, key_head = self.key(*args, **kwargs)
def delete(self, wire, request, *args, **kwargs):
if not getattr(request, '_fake_request', None):
warnings.warn(
"A request is given as first argument. If this is intended "
"try '(request, None)'. Otherwise, Use '(request, path)' "
"instead of 'request' to convert the actual request to have "
"the target path.")
key_get, key_head = self.key(wire, request, *args, **kwargs)
if key_get:
self.middleware.cache.delete(key_get)
if key_head:
Expand Down
10 changes: 5 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
def get_version():
with open('ring/__version__.py') as f:
empty, version = f.read().split('__version__ = ')
assert empty == ''
version = version.strip()
assert empty == ''
version = version.strip().strip("'")
assert version.startswith('0.')
return version


install_requires = [
Expand Down Expand Up @@ -93,9 +95,7 @@ def get_readme():
packages=(
'ring',
),
package_data={
'ring': ['version.txt'],
},
package_data={},
install_requires=install_requires,
tests_require=tests_require + ['tox', 'tox-pyenv'],
extras_require={
Expand Down
5 changes: 2 additions & 3 deletions tests/test_django.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def view_func(request):
request_factory = RequestFactory()
request_main = functools.partial(request_factory.get, '/ring')

view_func.delete(request_main())
view_func.delete((request_main(), None))
response = view_func.get(request_main())
assert response is None # not cached

Expand Down Expand Up @@ -85,7 +85,7 @@ def view_func(request):
with pytest.raises(NotImplementedError):
view_func.has(request_main())

view_func.delete(request_main()) # delete
view_func.delete((request_main(), None)) # delete

response = view_func.get(request_main())
assert response is None # no cache anymore
Expand Down Expand Up @@ -139,7 +139,6 @@ def test_django_chaining():


def test_django_invalid_reverse():

request_factory = RequestFactory()
request = request_factory.get('/chain/new')

Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = py27,py33,py34,py35,py36,pypy2,pypy3
envlist = py27,py34,py35,py36,pypy2,pypy3
[testenv]
deps=
pytest
Expand Down

0 comments on commit 70acd15

Please sign in to comment.