-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ef8326
commit d31c755
Showing
12 changed files
with
104 additions
and
4 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
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
Empty file.
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,2 @@ | ||
from .middlewares import LudicMiddleware as LudicMiddleware | ||
from .responses import LudicResponse as LudicResponse |
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,28 @@ | ||
from collections.abc import Callable | ||
|
||
from django.http import HttpRequest, HttpResponse | ||
|
||
from ludic.base import BaseElement | ||
|
||
|
||
class LudicMiddleware: | ||
"""Ludic middleware for Django to clean up the cache for f-strings. | ||
Usage: | ||
# somewhere in django settings.py | ||
MIDDLEWARES = [ | ||
"...", | ||
"ludic.contrib.django.LudicMiddleware", | ||
] | ||
""" | ||
|
||
def __init__(self, get_response: Callable[[HttpRequest], HttpResponse]) -> None: | ||
self.get_response = get_response | ||
|
||
def __call__(self, request: HttpRequest) -> HttpResponse: | ||
with BaseElement.formatter: | ||
response: HttpResponse = self.get_response(request) | ||
|
||
return response |
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,24 @@ | ||
from typing import Any | ||
|
||
from django.http import HttpResponse | ||
|
||
from ludic.types import AnyChildren | ||
|
||
|
||
class LudicResponse(HttpResponse): | ||
"""Class representing Ludic response for Django View. | ||
Usage: | ||
from django.http import HttpRequest | ||
from ludic.html import p | ||
from ludic.contrib.django import LudicResponse | ||
def index(request: HttpRequest) -> LudicResponse: | ||
return LudicResponse(p("Hello, World!")) | ||
""" | ||
|
||
def __init__(self, content: AnyChildren = "", *args: Any, **kwargs: Any) -> None: | ||
super().__init__(str(content), *args, **kwargs) |
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
Empty file.
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,32 @@ | ||
import os | ||
|
||
from django.http import HttpRequest | ||
|
||
from ludic.base import BaseElement | ||
from ludic.contrib.django import LudicMiddleware, LudicResponse | ||
from ludic.html import p | ||
|
||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tests.contrib") | ||
|
||
|
||
def test_django_response() -> None: | ||
assert LudicResponse(p("test")).content == b"<p>test</p>" | ||
|
||
|
||
def test_django_middleware() -> None: | ||
get_response_called = False | ||
|
||
def get_response(_: HttpRequest) -> LudicResponse: | ||
nonlocal get_response_called | ||
get_response_called = True | ||
|
||
assert len(BaseElement.formatter.get()) == 0 | ||
response = LudicResponse(f"{p("does not clean up cache")}") | ||
assert len(BaseElement.formatter.get()) == 1 | ||
return response | ||
|
||
middleware = LudicMiddleware(get_response) | ||
middleware(HttpRequest()) | ||
|
||
assert get_response_called | ||
assert len(BaseElement.formatter.get()) == 0 |
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