django-request-vars 2.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- django_request_vars-2.0.0.dist-info/METADATA +78 -0
- django_request_vars-2.0.0.dist-info/RECORD +12 -0
- django_request_vars-2.0.0.dist-info/WHEEL +4 -0
- django_request_vars-2.0.0.dist-info/licenses/LICENSE +25 -0
- request_vars/__init__.py +7 -0
- request_vars/apps.py +5 -0
- request_vars/conf.py +30 -0
- request_vars/decorators.py +24 -0
- request_vars/middleware.py +23 -0
- request_vars/templatetags/__init__.py +0 -0
- request_vars/templatetags/request_vars.py +8 -0
- request_vars/utils.py +26 -0
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: django-request-vars
|
|
3
|
+
Version: 2.0.0
|
|
4
|
+
Summary: Stores current request, user and your defined data in thread local variable.
|
|
5
|
+
Project-URL: Homepage, https://github.com/kindlycat/django-request-vars
|
|
6
|
+
Project-URL: Documentation, https://django-request-vars.readthedocs.io
|
|
7
|
+
Project-URL: Repository, https://github.com/kindlycat/django-request-vars.git
|
|
8
|
+
Project-URL: Issues, https://github.com/kindlycat/django-request-vars/issues
|
|
9
|
+
Author-email: Grigory Mishchenko <grishkokot@gmail.com>
|
|
10
|
+
License-Expression: BSD-3-Clause
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: cache,django,middleware,request,thread,user,variable
|
|
13
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
14
|
+
Classifier: Framework :: Django :: 5.2
|
|
15
|
+
Classifier: Framework :: Django :: 6.0
|
|
16
|
+
Classifier: Framework :: Django :: 6.1
|
|
17
|
+
Classifier: License :: OSI Approved :: BSD License
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
23
|
+
Requires-Python: >=3.11
|
|
24
|
+
Requires-Dist: django>=5.2
|
|
25
|
+
Description-Content-Type: text/x-rst
|
|
26
|
+
|
|
27
|
+
Django request vars
|
|
28
|
+
===================
|
|
29
|
+
.. image:: https://github.com/kindlycat/django-request-vars/actions/workflows/tests.yml/badge.svg
|
|
30
|
+
:target: https://github.com/kindlycat/django-request-vars/actions/workflows/tests.yml
|
|
31
|
+
.. image:: https://codecov.io/gh/kindlycat/django-request-vars/graph/badge.svg
|
|
32
|
+
:target: https://codecov.io/gh/kindlycat/django-request-vars
|
|
33
|
+
.. image:: https://readthedocs.org/projects/django-request-vars/badge/?version=latest
|
|
34
|
+
:target: http://django-request-vars.readthedocs.io/en/latest/?badge=latest
|
|
35
|
+
.. image:: https://img.shields.io/pypi/v/django-request-vars.svg
|
|
36
|
+
:target: https://pypi.org/project/django-request-vars/
|
|
37
|
+
|
|
38
|
+
Application that stores current request, user and your defined data in thread
|
|
39
|
+
local variable.
|
|
40
|
+
|
|
41
|
+
Full documentation on `read the docs`_.
|
|
42
|
+
|
|
43
|
+
Requirements
|
|
44
|
+
------------
|
|
45
|
+
|
|
46
|
+
**Django:** 5.2, 6.0, 6.1
|
|
47
|
+
**Python:** 3.11+
|
|
48
|
+
|
|
49
|
+
Installation
|
|
50
|
+
------------
|
|
51
|
+
Install using pip:
|
|
52
|
+
|
|
53
|
+
.. code-block:: sh
|
|
54
|
+
|
|
55
|
+
$ pip install django-request-vars
|
|
56
|
+
|
|
57
|
+
Add to installed apps if you need template tags:
|
|
58
|
+
|
|
59
|
+
.. code-block:: python
|
|
60
|
+
|
|
61
|
+
INSTALLED_APPS = (
|
|
62
|
+
...
|
|
63
|
+
'request_vars',
|
|
64
|
+
...
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
Add middleware:
|
|
68
|
+
|
|
69
|
+
.. code-block:: python
|
|
70
|
+
|
|
71
|
+
MIDDLEWARE = [
|
|
72
|
+
...
|
|
73
|
+
'request_vars.middleware.RequestVarsMiddleware',
|
|
74
|
+
...
|
|
75
|
+
]
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
.. _`read the docs`: https://django-request-vars.readthedocs.io/en/master/
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
request_vars/__init__.py,sha256=YeomI93yTvWPVgmZzBcLBXngJoCY15-lV2oG-sxHwak,135
|
|
2
|
+
request_vars/apps.py,sha256=G3mD6LSPEii5ISMoCYLV2wmqql9sLX9tMk4GLGoxnGE,98
|
|
3
|
+
request_vars/conf.py,sha256=HAn3YtXqTTPdO2hKhwHcPzfkU_rrdeilcGhCb1y2l3s,858
|
|
4
|
+
request_vars/decorators.py,sha256=_oZLPY5sTNaa0_CVp-wu7Tdgkub6tFOOdK90gyF9_jU,622
|
|
5
|
+
request_vars/middleware.py,sha256=xANpk-0ymRyOQ1roCj6XVI0Zt2Xj0iB1y27bcT5zQxE,756
|
|
6
|
+
request_vars/utils.py,sha256=P6ZdZb60rYDrMe9FfWcS1RGzibn1sNgN599hfz19JQA,459
|
|
7
|
+
request_vars/templatetags/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
|
+
request_vars/templatetags/request_vars.py,sha256=9Vk8xKzaOlMG9xqlbTDRBvj_sLAGNCC20kjI-3bcFik,140
|
|
9
|
+
django_request_vars-2.0.0.dist-info/METADATA,sha256=33RJ0iwnhuIy2EB98zpaJ33lw0Jc46Vig3gq-27BVEY,2540
|
|
10
|
+
django_request_vars-2.0.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
11
|
+
django_request_vars-2.0.0.dist-info/licenses/LICENSE,sha256=piSpo3UtvxHSSAuhm9XAMeYWzkZb8E-aWJ-jd82UjNg,1486
|
|
12
|
+
django_request_vars-2.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
copyright (c) 2018 Grigory Mishchenko and individual contributors.
|
|
2
|
+
|
|
3
|
+
All rights reserved.
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
* Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
list of conditions and the following disclaimer.
|
|
10
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
|
11
|
+
this list of conditions and the following disclaimer in the documentation
|
|
12
|
+
and/or other materials provided with the distribution.
|
|
13
|
+
* The names of its contributors may not be used to endorse or promote products
|
|
14
|
+
derived from this software without specific prior written permission.
|
|
15
|
+
|
|
16
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
17
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
18
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
19
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
|
20
|
+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
21
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
22
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
23
|
+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
24
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
25
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
request_vars/__init__.py
ADDED
request_vars/apps.py
ADDED
request_vars/conf.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
|
|
3
|
+
from django.conf import settings as dj_settings
|
|
4
|
+
from django.core.signals import setting_changed
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
DEFAULTS = {'REQUEST_VARS_MIDDLEWARE_CALLBACK': None}
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Settings:
|
|
11
|
+
def __getattr__(self, name) -> Any:
|
|
12
|
+
if name not in DEFAULTS: # pragma: no cover
|
|
13
|
+
msg = "'%s' object has no attribute '%s'"
|
|
14
|
+
raise AttributeError(msg % (self.__class__.__name__, name))
|
|
15
|
+
value = getattr(dj_settings, name, DEFAULTS[name])
|
|
16
|
+
setattr(self, name, value)
|
|
17
|
+
return value
|
|
18
|
+
|
|
19
|
+
def change_setting(self, setting, value, enter, **kwargs) -> None:
|
|
20
|
+
if setting not in DEFAULTS: # pragma: no cover
|
|
21
|
+
return
|
|
22
|
+
|
|
23
|
+
if enter:
|
|
24
|
+
setattr(self, setting, value)
|
|
25
|
+
else:
|
|
26
|
+
delattr(self, setting)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
settings = Settings()
|
|
30
|
+
setting_changed.connect(settings.change_setting)
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
from collections.abc import Callable
|
|
2
|
+
from functools import wraps
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from request_vars.utils import get_variable, set_variable
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
__all__ = ['request_cache']
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def request_cache(func) -> Callable:
|
|
12
|
+
@wraps(func)
|
|
13
|
+
def wrapped(*args, **kwargs) -> Any:
|
|
14
|
+
name = f'rc_{func.__module__}.{func.__name__}'
|
|
15
|
+
cached = get_variable(name)
|
|
16
|
+
if cached:
|
|
17
|
+
return cached
|
|
18
|
+
result = func(*args, **kwargs)
|
|
19
|
+
# Avoid cache forever in tasks, shell or tests.
|
|
20
|
+
if get_variable('request'):
|
|
21
|
+
set_variable(name, result)
|
|
22
|
+
return result
|
|
23
|
+
|
|
24
|
+
return wrapped
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
|
|
3
|
+
from django.utils.module_loading import import_string
|
|
4
|
+
|
|
5
|
+
from request_vars.conf import settings
|
|
6
|
+
from request_vars.utils import clear_thread_variable, set_variable
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class RequestVarsMiddleware:
|
|
10
|
+
def __init__(self, get_response) -> None:
|
|
11
|
+
self.get_response = get_response
|
|
12
|
+
|
|
13
|
+
def __call__(self, request) -> Any:
|
|
14
|
+
set_variable('request', request)
|
|
15
|
+
set_variable('user', request.user)
|
|
16
|
+
if settings.REQUEST_VARS_MIDDLEWARE_CALLBACK:
|
|
17
|
+
import_string(settings.REQUEST_VARS_MIDDLEWARE_CALLBACK)(request)
|
|
18
|
+
response = self.get_response(request)
|
|
19
|
+
clear_thread_variable()
|
|
20
|
+
return response
|
|
21
|
+
|
|
22
|
+
def process_exception(self, request, exception) -> None:
|
|
23
|
+
clear_thread_variable()
|
|
File without changes
|
request_vars/utils.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
from request_vars import REQUEST_VARS
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
__all__ = [
|
|
5
|
+
'clear_thread_variable',
|
|
6
|
+
'del_variable',
|
|
7
|
+
'get_variable',
|
|
8
|
+
'set_variable',
|
|
9
|
+
]
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def set_variable(key, val):
|
|
13
|
+
setattr(REQUEST_VARS, key, val)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def get_variable(key, default=None):
|
|
17
|
+
return getattr(REQUEST_VARS, key, default)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def del_variable(key):
|
|
21
|
+
if hasattr(REQUEST_VARS, key):
|
|
22
|
+
del REQUEST_VARS.__dict__[key]
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def clear_thread_variable():
|
|
26
|
+
REQUEST_VARS.__dict__.clear()
|