auth/tauth/views/helper.py

39 lines
962 B
Python
Raw Normal View History

2024-12-20 08:59:39 -05:00
import random
from django.core.handlers.wsgi import WSGIRequest
from django.http import HttpResponse
from django.template import loader
2024-12-20 23:44:31 -05:00
from tauth.settings import config
2024-12-20 08:59:39 -05:00
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 = {
2024-12-20 23:44:31 -05:00
"accent": random.choice(COLORS),
2024-12-22 00:07:17 -05:00
"config": config,
2024-12-23 22:38:56 -05:00
"login_token": f"/auth/?sessionid={request.COOKIES.get('session_id')}"
2024-12-20 08:59:39 -05:00
}
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