create Response.new_template method

This commit is contained in:
Izalia Mae 2024-10-12 11:03:50 -04:00
parent f9d6d7b18d
commit 091f8175b5
4 changed files with 33 additions and 38 deletions

View file

@ -298,14 +298,12 @@ class CacheCleanupThread(Thread):
def format_error(request: web.Request, error: HttpError) -> Response: def format_error(request: web.Request, error: HttpError) -> Response:
app: Application = request.app # type: ignore[assignment]
if request.path.startswith(JSON_PATHS) or 'json' in request.headers.get('accept', ''): if request.path.startswith(JSON_PATHS) or 'json' in request.headers.get('accept', ''):
return Response.new({'error': error.message}, error.status, ctype = 'json') return Response.new({'error': error.message}, error.status, ctype = 'json')
else: else:
body = app.template.render('page/error.haml', request, e = error) context = {"e": error}
return Response.new(body, error.status, ctype = 'html') return Response.new_template(error.status, "page/error.haml", request, context)
@web.middleware @web.middleware

View file

@ -5,7 +5,7 @@ import json
import os import os
import platform import platform
from aiohttp.web import Response as AiohttpResponse from aiohttp.web import Request, Response as AiohttpResponse
from collections.abc import Sequence from collections.abc import Sequence
from datetime import datetime from datetime import datetime
from typing import TYPE_CHECKING, Any, TypedDict, TypeVar from typing import TYPE_CHECKING, Any, TypedDict, TypeVar
@ -207,6 +207,19 @@ class Response(AiohttpResponse):
return cls.new(body, status, {'Location': path}, ctype = 'html') return cls.new(body, status, {'Location': path}, ctype = 'html')
@classmethod
def new_template(cls: type[Self],
status: int,
path: str,
request: Request,
context: dict[str, Any] | None = None,
headers: dict[str, Any] | None = None,
ctype: str = "html") -> Self:
body = get_app().template.render(path, request, **(context or {}))
return cls.new(body, status, headers, ctype)
@property @property
def location(self) -> str: def location(self) -> str:
return self.headers.get('Location', '') return self.headers.get('Location', '')

View file

@ -75,19 +75,16 @@ class OauthAuthorize(View):
raise HttpError(400, 'Application has already been authorized') raise HttpError(400, 'Application has already been authorized')
if app.auth_code is not None: if app.auth_code is not None:
context = {'application': app} page = "page/authorization_show.haml"
html = self.template.render(
'page/authorize_show.haml', self.request, **context
)
return Response.new(html, ctype = 'html') else:
page = "page/authorize_new.haml"
if data['redirect_uri'] != app.redirect_uri: if data['redirect_uri'] != app.redirect_uri:
raise HttpError(400, 'redirect_uri does not match application') raise HttpError(400, 'redirect_uri does not match application')
context = {'application': app} context = {'application': app}
html = self.template.render('page/authorize_new.haml', self.request, **context) return Response.new_template(200, page, request, context)
return Response.new(html, ctype = 'html')
async def post(self, request: Request) -> Response: async def post(self, request: Request) -> Response:
@ -108,11 +105,7 @@ class OauthAuthorize(View):
if app.redirect_uri == DEFAULT_REDIRECT: if app.redirect_uri == DEFAULT_REDIRECT:
context = {'application': app} context = {'application': app}
html = self.template.render( return Response.new_template(200, "page/authorize_show.haml", request, context)
'page/authorize_show.haml', self.request, **context
)
return Response.new(html, ctype = 'html')
return Response.new_redir(f'{app.redirect_uri}?code={app.auth_code}') return Response.new_redir(f'{app.redirect_uri}?code={app.auth_code}')

View file

@ -51,15 +51,13 @@ async def handle_home(app: Application, request: Request) -> Response:
'instances': tuple(conn.get_inboxes()) 'instances': tuple(conn.get_inboxes())
} }
data = app.template.render('page/home.haml', request, **context) return Response.new_template(200, "page/home.haml", request, context)
return Response.new(data, ctype='html')
@register_route(HttpMethod.GET, '/login') @register_route(HttpMethod.GET, '/login')
async def handle_login(app: Application, request: Request) -> Response: async def handle_login(app: Application, request: Request) -> Response:
redir = unquote(request.query.get('redir', '/')) context = {"redir": unquote(request.query.get("redir", "/"))}
data = app.template.render('page/login.haml', request, redir = redir) return Response.new_template(200, "page/login.haml", request, context)
return Response.new(data, ctype = 'html')
@register_route(HttpMethod.GET, '/logout') @register_route(HttpMethod.GET, '/logout')
@ -96,8 +94,7 @@ async def handle_admin_instances(
if message: if message:
context['message'] = message context['message'] = message
data = app.template.render('page/admin-instances.haml', request, **context) return Response.new_template(200, "page/admin-instances.haml", request, context)
return Response.new(data, ctype = 'html')
@register_route(HttpMethod.GET, '/admin/whitelist') @register_route(HttpMethod.GET, '/admin/whitelist')
@ -118,8 +115,7 @@ async def handle_admin_whitelist(
if message: if message:
context['message'] = message context['message'] = message
data = app.template.render('page/admin-whitelist.haml', request, **context) return Response.new_template(200, "page/admin-whitelist.haml", request, context)
return Response.new(data, ctype = 'html')
@register_route(HttpMethod.GET, '/admin/domain_bans') @register_route(HttpMethod.GET, '/admin/domain_bans')
@ -140,8 +136,7 @@ async def handle_admin_instance_bans(
if message: if message:
context['message'] = message context['message'] = message
data = app.template.render('page/admin-domain_bans.haml', request, **context) return Response.new_template(200, "page/admin-domain_bans.haml", request, context)
return Response.new(data, ctype = 'html')
@register_route(HttpMethod.GET, '/admin/software_bans') @register_route(HttpMethod.GET, '/admin/software_bans')
@ -162,8 +157,7 @@ async def handle_admin_software_bans(
if message: if message:
context['message'] = message context['message'] = message
data = app.template.render('page/admin-software_bans.haml', request, **context) return Response.new_template(200, "page/admin-software_bans.haml", request, context)
return Response.new(data, ctype = 'html')
@register_route(HttpMethod.GET, '/admin/users') @register_route(HttpMethod.GET, '/admin/users')
@ -184,8 +178,7 @@ async def handle_admin_users(
if message: if message:
context['message'] = message context['message'] = message
data = app.template.render('page/admin-users.haml', request, **context) return Response.new_template(200, "page/admin-users.haml", request, context)
return Response.new(data, ctype = 'html')
@register_route(HttpMethod.GET, '/admin/config') @register_route(HttpMethod.GET, '/admin/config')
@ -211,8 +204,7 @@ async def handle_admin_config(
} }
} }
data = app.template.render('page/admin-config.haml', request, **context) return Response.new_template(200, "page/admin-config.haml", request, context)
return Response.new(data, ctype = 'html')
@register_route(HttpMethod.GET, '/manifest.json') @register_route(HttpMethod.GET, '/manifest.json')
@ -247,5 +239,4 @@ async def handle_theme(app: Application, request: Request, theme: str) -> Respon
except KeyError: except KeyError:
return Response.new('Invalid theme', 404) return Response.new('Invalid theme', 404)
data = app.template.render('variables.css', request, **context) return Response.new_template(200, "variables.css", request, context, ctype = "css")
return Response.new(data, ctype = 'css')