36 lines
846 B
Python
36 lines
846 B
Python
|
import random
|
||
|
|
||
|
from django.core.handlers.wsgi import WSGIRequest
|
||
|
from django.http import HttpResponse
|
||
|
from django.template import loader
|
||
|
|
||
|
COLORS = ["rosewater", "flamingo", "pink", "mauve", "red", "maroon", "peach", "yellow", "green", "teal", "sky", "sapphire", "blue", "lavender"]
|
||
|
|
||
|
def render_template(
|
||
|
request: WSGIRequest,
|
||
|
template: str,
|
||
|
/, *,
|
||
|
status: int=200,
|
||
|
headers: dict[str, str]={},
|
||
|
content_type: str="text/html",
|
||
|
**context
|
||
|
) -> HttpResponse:
|
||
|
c = {
|
||
|
"theme": "auto",
|
||
|
"accent": random.choice(COLORS)
|
||
|
}
|
||
|
|
||
|
for key, val in context.items():
|
||
|
c[key] = val
|
||
|
|
||
|
resp = HttpResponse(
|
||
|
loader.get_template(template).render(c, request),
|
||
|
status=status,
|
||
|
content_type=content_type
|
||
|
)
|
||
|
|
||
|
for key, val in headers.items():
|
||
|
resp[key] = val
|
||
|
|
||
|
return resp
|