Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix quart signals upgrade #22

Merged
merged 5 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
]

tests_requires = [
"quart >= 0.18.0, < 0.19.0",
"quart >= 0.18.0",
"flask >= 2.1.0",
"fastapi",
"sanic",
Expand Down
44 changes: 36 additions & 8 deletions src/jinja2_fragments/quart.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@
"Install quart before using jinja2_fragments.quart"
) from e

from quart.signals import AsyncNamespace
try:
# Quart >= 0.19.0
from quart.signals import Namespace

CUSTOM_SIGNAL = False
except ImportError:
# Quart < 0.19.0
from quart.signals import AsyncNamespace as Namespace

CUSTOM_SIGNAL = True


from . import render_block_async

jinja2_fragments_signals = AsyncNamespace()
jinja2_fragments_signals = Namespace()
before_render_template_block = jinja2_fragments_signals.signal(
"before-render-template-block"
)
Expand All @@ -30,13 +40,31 @@ async def render_block(
"""
app = current_app # type: ignore[attr-defined]
await app.update_template_context(context)
await before_render_template_block.send(
app, template_name=template_name, block_name=block_name, context=context
)
if CUSTOM_SIGNAL:
await before_render_template_block.send(
app, template_name=template_name, block_name=block_name, context=context
)
else:
await before_render_template_block.send_async(
app,
_sync_wrapper=app.ensure_async,
template_name=template_name,
block_name=block_name,
context=context,
)
rendered = await render_block_async(
app.jinja_env, template_name, block_name, **context
)
await template_block_rendered.send(
app, template_name=template_name, block_name=block_name, context=context
)
if CUSTOM_SIGNAL:
await template_block_rendered.send(
app, template_name=template_name, block_name=block_name, context=context
)
else:
await template_block_rendered.send_async(
app,
_sync_wrapper=app.ensure_async,
template_name=template_name,
block_name=block_name,
context=context,
)
return rendered
11 changes: 11 additions & 0 deletions tests/test_quart.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sys

import pytest

from jinja2_fragments import BlockNotFoundError
Expand All @@ -13,6 +15,9 @@ class TestQuartRenderBlock:
],
)
@pytest.mark.asyncio
@pytest.mark.skipif(
sys.version_info < (3, 8), reason="Quart requires Python 3.8.0 or higher"
)
async def test_simple_page(self, quart_client, get_html, only_content, html_name):
response = await quart_client.get(
"/simple_page", query_string={"only_content": only_content}
Expand All @@ -30,6 +35,9 @@ async def test_simple_page(self, quart_client, get_html, only_content, html_name
],
)
@pytest.mark.asyncio
@pytest.mark.skipif(
sys.version_info < (3, 8), reason="Quart requires Python 3.8.0 or higher"
)
async def test_nested_page(self, quart_client, get_html, route, html_name):
response = await quart_client.get(route)
response_text = await response.get_data(True)
Expand All @@ -38,6 +46,9 @@ async def test_nested_page(self, quart_client, get_html, route, html_name):
assert html == response_text

@pytest.mark.asyncio
@pytest.mark.skipif(
sys.version_info < (3, 8), reason="Quart requires Python 3.8.0 or higher"
)
async def test_exception(self, quart_app):
with pytest.raises(BlockNotFoundError) as exc:
async with quart_app.app_context():
Expand Down