-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch request handling to thread locals (#125)
* Switch request handling to thread locals * Add test for deprecation warning This is safer in general, and especially in ASGI environment. If ASGI (which ships its own version) is not available, we use threading local Move to pytest runner to run async tests (though not on ASGI)
- Loading branch information
Showing
9 changed files
with
141 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Switch request handling to thread locals |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import contextlib | ||
|
||
try: | ||
from asgiref.local import Local | ||
except ImportError: | ||
from threading import local as Local # noqa: N812 | ||
_thread_locals = Local() | ||
|
||
|
||
@contextlib.contextmanager | ||
def set_request(request): | ||
""" | ||
Context processor that sets the request on the current instance | ||
""" | ||
_thread_locals._request = request | ||
yield | ||
|
||
|
||
def get_request(): | ||
""" | ||
Retrieve request from current instance | ||
""" | ||
return getattr(_thread_locals, "_request", None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,7 @@ coveralls>=2.0 | |
mock>=1.0.1 | ||
pillow | ||
django-app-helper>=2.0.1 | ||
|
||
pytest | ||
pytest-django | ||
pytest-asyncio |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
from datetime import timedelta | ||
|
||
import django | ||
import pytest | ||
from django.utils.text import slugify | ||
from django.utils.timezone import now | ||
|
||
from tests.example_app.models import Post | ||
|
||
try: | ||
from asgiref.sync import sync_to_async | ||
from django.test import AsyncRequestFactory | ||
except ImportError: | ||
# stub to avoid decorator failures | ||
def sync_to_async(__): | ||
return | ||
|
||
pytestmark = pytest.mark.skip("asgiref not installed, skipping async tests") | ||
|
||
minversion = pytest.mark.skipif(django.VERSION < (3, 1), reason="at least Django 3.1 required") | ||
|
||
|
||
@sync_to_async | ||
def get_post(title): | ||
post, __ = Post.objects.get_or_create( | ||
title=title, | ||
og_title="og {title}".format(title=title), | ||
twitter_title="twitter {title}".format(title=title), | ||
schemaorg_title="schemaorg {title}".format(title=title), | ||
slug=slugify(title), | ||
abstract="post abstract", | ||
meta_description="post meta", | ||
meta_keywords="post keyword1,post keyword 2", | ||
date_published_end=now() + timedelta(days=2), | ||
text="post text", | ||
image_url="/path/to/image", | ||
) | ||
print(post.og_title) | ||
return post | ||
|
||
|
||
@sync_to_async | ||
def delete_post(post): | ||
post.delete() | ||
|
||
|
||
@sync_to_async | ||
def get_meta(post, request=None): | ||
return post.as_meta(request) | ||
|
||
|
||
@minversion | ||
@pytest.mark.asyncio | ||
@pytest.mark.django_db | ||
async def test_mixin_on_asgi(): | ||
post = await get_post("first post") | ||
meta = await get_meta(post) | ||
assert meta.title == "first post" | ||
assert meta.og_title == "og first post" | ||
|
||
|
||
@minversion | ||
@pytest.mark.asyncio | ||
@pytest.mark.django_db | ||
async def test_mixin_on_asgi_request(): | ||
request = AsyncRequestFactory().get("/") | ||
post = await get_post("first post") | ||
meta = await get_meta(post, request) | ||
assert meta.title == "first post" | ||
assert meta.og_title == "og first post" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters