# django-middleware-global-request
Django middleware that keep request instance for every thread.
## Install
```shell
pip install django-middleware-global-request
```
## Note
- It's NOT good to use global request, you should pass the request instance from view to anywhere you want to use it.
- If you use the global request in Model layout, it means when you call the Model method from Django Shell, you get a None request.
## Usage
1. Add django application `django_middleware_global_request` to INSTALLED_APPS in `pro/settings.py`:
```python
INSTALLED_APPS = [
...
'django_middleware_global_request',
...
]
```
2. Add `GlobalRequestMiddleware` to MIDDLEWARE in `pro/settings.py`:
```python
MIDDLEWARE = [
...
'django_middleware_global_request.middleware.GlobalRequestMiddleware',
...
]
```
3. Use `get_request` to get the global request instance from a function in `pro/models.py`:
```python
from django.db import models
from django.conf import settings
from django_middleware_global_request import get_request
class Book(models.Model):
name = models.CharField(max_length=64)
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True, blank=True)
def __str__(self):
return self.name
def save(self, *args, **kwargs):
if not self.author:
request = get_request()
if request:
self.author = request.user
return super().save(*args, **kwargs)
```
4. Use `with GlobalRequest(xxx): pass` to set the global request to a new value in NON-USER-REQUEST context, e.g. in `management command` context or in `python manage.py shell` context. Out of the `with scope`, the global request instance will be reset to the value when entering into the `with scope`.
```python
import djclick as click
from django.contrib.auth import get_user_model
from django_middleware_global_request_example.models import Book
from django_middleware_global_request import GlobalRequest
@click.command()
@click.option("-n", "--number", type=int, default=10)
def create(number):
admin = get_user_model().objects.all()[0]
with GlobalRequest(user=admin):
for i in range(number):
book = Book(name="book{idx}".format(idx=i+1))
book.save()
print(i, book.name, book.author.username)
```
5. Use `GlobalRequestStorage` to set the global request instance for current thread context. The global request instance will all time exists until you changed it by another value.
*example.py*
```
from django.contrib.auth import get_user_model
from django_middleware_global_request_example.models import Book
from django_middleware_global_request import GlobalRequestStorage
b1 = Book(name="b1")
b1.save()
print(b1, b1.author)
admin = get_user_model().objects.get(username="admin")
GlobalRequestStorage().set_user(admin)
b2 = Book(name="b2")
b2.save()
print(b2, b2.author)
b3 = Book(name="b3")
b3.save()
print(b3, b3.author)
```
*example result in python3 manage.py shell context*
```
test@test django-middleware-global-request % python3.9 manage.py shell
Python 3.9.13 (main, Jun 8 2022, 15:40:49)
Type 'copyright', 'credits' or 'license' for more information
IPython 8.3.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import example
b1 None
b2 admin
b3 admin
```
## Test Passed
- python27:~=django1.11.29
- python34:~=django1.11.29
- python34:~=django2.0.13
- python35:~=django1.11.29
- python35:~=django2.0.13
- python35:~=django2.1.15
- python35:~=django2.2.28
- python36:~=django2.0.13
- python36:~=django2.1.15
- python36:~=django2.2.28
- python36:~=django3.0.14
- python36:~=django3.1.14
- python36:~=django3.2.21
- python37:~=django2.0.13
- python37:~=django2.1.15
- python37:~=django2.2.28
- python37:~=django3.0.14
- python37:~=django3.1.14
- python37:~=django3.2.21
- python38:~=django2.0.13
- python38:~=django2.1.15
- python38:~=django2.2.28
- python38:~=django3.0.14
- python38:~=django3.1.14
- python38:~=django3.2.21
- python38:~=django4.0.10
- python38:~=django4.1.11
- python38:~=django4.2.5
- python39:~=django2.0.13
- python39:~=django2.1.15
- python39:~=django2.2.28
- python39:~=django3.0.14
- python39:~=django3.1.14
- python39:~=django3.2.21
- python39:~=django4.0.10
- python39:~=django4.1.11
- python39:~=django4.2.5
- python310:~=django2.1.15
- python310:~=django2.2.28
- python310:~=django3.0.14
- python310:~=django3.1.14
- python310:~=django3.2.21
- python310:~=django4.0.10
- python310:~=django4.1.11
- python310:~=django4.2.5
- python311:~=django2.2.28
- python311:~=django3.0.14
- python311:~=django3.1.14
- python311:~=django3.2.21
- python311:~=django4.0.10
- python311:~=django4.1.11
- python311:~=django4.2.5
## Releases
### v0.1.0
- First release.
### v0.1.1
- Some changes.
### v0.1.2
- Some changes.
### v0.2.0
- Rename the core package from django_global_request to django_middleware_global_request so that it matches with the package name. **Note:** It's NOT backward compatible, all applications that using old name MUST do changes.
### v0.3.0
- Add `GlobalRequest` and `GlobalRequestStorage` to set the global request instance value for NON-USER-REQUEST context.
### v0.3.1
- Add `app_middleware_requires` to module \_\_init\_\_.py to work with `django-app-requires`.
### v0.3.2
- Doc update.
### v0.3.3
- All unit test passed.
### v0.3.4
- Add unit tests for python12 and django-5.0.6.
Raw data
{
"_id": null,
"home_page": null,
"name": "django-middleware-global-request",
"maintainer": "Wang Liang",
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "django extensions, django middleware global request",
"author": "Wang Liang",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/de/ec/b73de9a6a40f03593f06edaf085316576ac7854e76f05b62f7771c951fb8/django-middleware-global-request-0.3.4.tar.gz",
"platform": null,
"description": "# django-middleware-global-request\n\n\nDjango middleware that keep request instance for every thread.\n\n## Install\n\n\n```shell\npip install django-middleware-global-request\n```\n## Note\n\n- It's NOT good to use global request, you should pass the request instance from view to anywhere you want to use it.\n- If you use the global request in Model layout, it means when you call the Model method from Django Shell, you get a None request.\n\n## Usage\n\n1. Add django application `django_middleware_global_request` to INSTALLED_APPS in `pro/settings.py`:\n\n ```python\n INSTALLED_APPS = [\n ...\n 'django_middleware_global_request',\n ...\n ]\n ```\n\n2. Add `GlobalRequestMiddleware` to MIDDLEWARE in `pro/settings.py`:\n\n\n ```python\n MIDDLEWARE = [\n ...\n 'django_middleware_global_request.middleware.GlobalRequestMiddleware',\n ...\n ]\n ```\n\n3. Use `get_request` to get the global request instance from a function in `pro/models.py`:\n\n ```python\n from django.db import models\n from django.conf import settings\n from django_middleware_global_request import get_request\n\n class Book(models.Model):\n name = models.CharField(max_length=64)\n author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True, blank=True)\n\n def __str__(self):\n return self.name\n\n def save(self, *args, **kwargs):\n if not self.author:\n request = get_request()\n if request:\n self.author = request.user\n return super().save(*args, **kwargs)\n ```\n\n4. Use `with GlobalRequest(xxx): pass` to set the global request to a new value in NON-USER-REQUEST context, e.g. in `management command` context or in `python manage.py shell` context. Out of the `with scope`, the global request instance will be reset to the value when entering into the `with scope`.\n\n ```python\n import djclick as click\n from django.contrib.auth import get_user_model\n from django_middleware_global_request_example.models import Book\n from django_middleware_global_request import GlobalRequest\n\n\n @click.command()\n @click.option(\"-n\", \"--number\", type=int, default=10)\n def create(number):\n admin = get_user_model().objects.all()[0]\n with GlobalRequest(user=admin):\n for i in range(number):\n book = Book(name=\"book{idx}\".format(idx=i+1))\n book.save()\n print(i, book.name, book.author.username)\n ```\n\n5. Use `GlobalRequestStorage` to set the global request instance for current thread context. The global request instance will all time exists until you changed it by another value.\n\n*example.py*\n\n```\nfrom django.contrib.auth import get_user_model\nfrom django_middleware_global_request_example.models import Book\nfrom django_middleware_global_request import GlobalRequestStorage\n\nb1 = Book(name=\"b1\")\nb1.save()\nprint(b1, b1.author)\n\nadmin = get_user_model().objects.get(username=\"admin\")\nGlobalRequestStorage().set_user(admin)\n\nb2 = Book(name=\"b2\")\nb2.save()\nprint(b2, b2.author)\n\nb3 = Book(name=\"b3\")\nb3.save()\nprint(b3, b3.author)\n```\n\n*example result in python3 manage.py shell context*\n\n```\ntest@test django-middleware-global-request % python3.9 manage.py shell\nPython 3.9.13 (main, Jun 8 2022, 15:40:49) \nType 'copyright', 'credits' or 'license' for more information\nIPython 8.3.0 -- An enhanced Interactive Python. Type '?' for help.\n\nIn [1]: import example\nb1 None\nb2 admin\nb3 admin\n```\n\n## Test Passed\n\n- python27:~=django1.11.29\n- python34:~=django1.11.29\n- python34:~=django2.0.13\n- python35:~=django1.11.29\n- python35:~=django2.0.13\n- python35:~=django2.1.15\n- python35:~=django2.2.28\n- python36:~=django2.0.13\n- python36:~=django2.1.15\n- python36:~=django2.2.28\n- python36:~=django3.0.14\n- python36:~=django3.1.14\n- python36:~=django3.2.21\n- python37:~=django2.0.13\n- python37:~=django2.1.15\n- python37:~=django2.2.28\n- python37:~=django3.0.14\n- python37:~=django3.1.14\n- python37:~=django3.2.21\n- python38:~=django2.0.13\n- python38:~=django2.1.15\n- python38:~=django2.2.28\n- python38:~=django3.0.14\n- python38:~=django3.1.14\n- python38:~=django3.2.21\n- python38:~=django4.0.10\n- python38:~=django4.1.11\n- python38:~=django4.2.5\n- python39:~=django2.0.13\n- python39:~=django2.1.15\n- python39:~=django2.2.28\n- python39:~=django3.0.14\n- python39:~=django3.1.14\n- python39:~=django3.2.21\n- python39:~=django4.0.10\n- python39:~=django4.1.11\n- python39:~=django4.2.5\n- python310:~=django2.1.15\n- python310:~=django2.2.28\n- python310:~=django3.0.14\n- python310:~=django3.1.14\n- python310:~=django3.2.21\n- python310:~=django4.0.10\n- python310:~=django4.1.11\n- python310:~=django4.2.5\n- python311:~=django2.2.28\n- python311:~=django3.0.14\n- python311:~=django3.1.14\n- python311:~=django3.2.21\n- python311:~=django4.0.10\n- python311:~=django4.1.11\n- python311:~=django4.2.5\n\n## Releases\n\n### v0.1.0\n\n- First release.\n\n### v0.1.1\n\n- Some changes.\n\n### v0.1.2\n\n- Some changes.\n\n### v0.2.0\n\n- Rename the core package from django_global_request to django_middleware_global_request so that it matches with the package name. **Note:** It's NOT backward compatible, all applications that using old name MUST do changes.\n\n### v0.3.0\n\n- Add `GlobalRequest` and `GlobalRequestStorage` to set the global request instance value for NON-USER-REQUEST context.\n\n### v0.3.1\n\n- Add `app_middleware_requires` to module \\_\\_init\\_\\_.py to work with `django-app-requires`.\n\n### v0.3.2\n\n- Doc update.\n\n### v0.3.3\n\n- All unit test passed.\n\n### v0.3.4\n\n- Add unit tests for python12 and django-5.0.6.\n\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Django middleware that keep request instance for every thread.",
"version": "0.3.4",
"project_urls": null,
"split_keywords": [
"django extensions",
" django middleware global request"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "69ed300d08430e5ce98455a0a3c3ceb80335791f2793a5c367b7a3617f1dd94d",
"md5": "3a80499b16c228b1a98958a1b20e7ede",
"sha256": "461d0c56ffb2dce272ec79b812c7ea5738a30da69629a2b7de0a909b09de3527"
},
"downloads": -1,
"filename": "django_middleware_global_request-0.3.4-py3-none-any.whl",
"has_sig": false,
"md5_digest": "3a80499b16c228b1a98958a1b20e7ede",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 8004,
"upload_time": "2024-05-22T13:38:57",
"upload_time_iso_8601": "2024-05-22T13:38:57.370834Z",
"url": "https://files.pythonhosted.org/packages/69/ed/300d08430e5ce98455a0a3c3ceb80335791f2793a5c367b7a3617f1dd94d/django_middleware_global_request-0.3.4-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "deecb73de9a6a40f03593f06edaf085316576ac7854e76f05b62f7771c951fb8",
"md5": "af6de08ebe6c7ec8b7048606c7c11ab3",
"sha256": "c3955495a2f4c05d9f50788b503196d7c590f443f980175e9be0e5d25c0f3c90"
},
"downloads": -1,
"filename": "django-middleware-global-request-0.3.4.tar.gz",
"has_sig": false,
"md5_digest": "af6de08ebe6c7ec8b7048606c7c11ab3",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 6508,
"upload_time": "2024-05-22T13:38:58",
"upload_time_iso_8601": "2024-05-22T13:38:58.575376Z",
"url": "https://files.pythonhosted.org/packages/de/ec/b73de9a6a40f03593f06edaf085316576ac7854e76f05b62f7771c951fb8/django-middleware-global-request-0.3.4.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-05-22 13:38:58",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "django-middleware-global-request"
}