From 091f8175b51231a78d6b78da8424d415d5071a19 Mon Sep 17 00:00:00 2001 From: Izalia Mae Date: Sat, 12 Oct 2024 11:03:50 -0400 Subject: [PATCH] create `Response.new_template` method --- relay/application.py | 6 ++---- relay/misc.py | 15 ++++++++++++++- relay/views/api.py | 21 +++++++-------------- relay/views/frontend.py | 29 ++++++++++------------------- 4 files changed, 33 insertions(+), 38 deletions(-) diff --git a/relay/application.py b/relay/application.py index 0e702c7..3ba73f9 100644 --- a/relay/application.py +++ b/relay/application.py @@ -298,14 +298,12 @@ class CacheCleanupThread(Thread): 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', ''): return Response.new({'error': error.message}, error.status, ctype = 'json') else: - body = app.template.render('page/error.haml', request, e = error) - return Response.new(body, error.status, ctype = 'html') + context = {"e": error} + return Response.new_template(error.status, "page/error.haml", request, context) @web.middleware diff --git a/relay/misc.py b/relay/misc.py index 3fa0b7d..f5899da 100644 --- a/relay/misc.py +++ b/relay/misc.py @@ -5,7 +5,7 @@ import json import os import platform -from aiohttp.web import Response as AiohttpResponse +from aiohttp.web import Request, Response as AiohttpResponse from collections.abc import Sequence from datetime import datetime from typing import TYPE_CHECKING, Any, TypedDict, TypeVar @@ -207,6 +207,19 @@ class Response(AiohttpResponse): 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 def location(self) -> str: return self.headers.get('Location', '') diff --git a/relay/views/api.py b/relay/views/api.py index 1023147..8b05fc6 100644 --- a/relay/views/api.py +++ b/relay/views/api.py @@ -75,19 +75,16 @@ class OauthAuthorize(View): raise HttpError(400, 'Application has already been authorized') if app.auth_code is not None: - context = {'application': app} - html = self.template.render( - 'page/authorize_show.haml', self.request, **context - ) + page = "page/authorization_show.haml" - return Response.new(html, ctype = 'html') + else: + page = "page/authorize_new.haml" - if data['redirect_uri'] != app.redirect_uri: - raise HttpError(400, 'redirect_uri does not match application') + if data['redirect_uri'] != app.redirect_uri: + raise HttpError(400, 'redirect_uri does not match application') context = {'application': app} - html = self.template.render('page/authorize_new.haml', self.request, **context) - return Response.new(html, ctype = 'html') + return Response.new_template(200, page, request, context) async def post(self, request: Request) -> Response: @@ -108,11 +105,7 @@ class OauthAuthorize(View): if app.redirect_uri == DEFAULT_REDIRECT: context = {'application': app} - html = self.template.render( - 'page/authorize_show.haml', self.request, **context - ) - - return Response.new(html, ctype = 'html') + return Response.new_template(200, "page/authorize_show.haml", request, context) return Response.new_redir(f'{app.redirect_uri}?code={app.auth_code}') diff --git a/relay/views/frontend.py b/relay/views/frontend.py index 5e12ee8..eff93c7 100644 --- a/relay/views/frontend.py +++ b/relay/views/frontend.py @@ -51,15 +51,13 @@ async def handle_home(app: Application, request: Request) -> Response: 'instances': tuple(conn.get_inboxes()) } - data = app.template.render('page/home.haml', request, **context) - return Response.new(data, ctype='html') + return Response.new_template(200, "page/home.haml", request, context) @register_route(HttpMethod.GET, '/login') async def handle_login(app: Application, request: Request) -> Response: - redir = unquote(request.query.get('redir', '/')) - data = app.template.render('page/login.haml', request, redir = redir) - return Response.new(data, ctype = 'html') + context = {"redir": unquote(request.query.get("redir", "/"))} + return Response.new_template(200, "page/login.haml", request, context) @register_route(HttpMethod.GET, '/logout') @@ -96,8 +94,7 @@ async def handle_admin_instances( if message: context['message'] = message - data = app.template.render('page/admin-instances.haml', request, **context) - return Response.new(data, ctype = 'html') + return Response.new_template(200, "page/admin-instances.haml", request, context) @register_route(HttpMethod.GET, '/admin/whitelist') @@ -118,8 +115,7 @@ async def handle_admin_whitelist( if message: context['message'] = message - data = app.template.render('page/admin-whitelist.haml', request, **context) - return Response.new(data, ctype = 'html') + return Response.new_template(200, "page/admin-whitelist.haml", request, context) @register_route(HttpMethod.GET, '/admin/domain_bans') @@ -140,8 +136,7 @@ async def handle_admin_instance_bans( if message: context['message'] = message - data = app.template.render('page/admin-domain_bans.haml', request, **context) - return Response.new(data, ctype = 'html') + return Response.new_template(200, "page/admin-domain_bans.haml", request, context) @register_route(HttpMethod.GET, '/admin/software_bans') @@ -162,8 +157,7 @@ async def handle_admin_software_bans( if message: context['message'] = message - data = app.template.render('page/admin-software_bans.haml', request, **context) - return Response.new(data, ctype = 'html') + return Response.new_template(200, "page/admin-software_bans.haml", request, context) @register_route(HttpMethod.GET, '/admin/users') @@ -184,8 +178,7 @@ async def handle_admin_users( if message: context['message'] = message - data = app.template.render('page/admin-users.haml', request, **context) - return Response.new(data, ctype = 'html') + return Response.new_template(200, "page/admin-users.haml", request, context) @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(data, ctype = 'html') + return Response.new_template(200, "page/admin-config.haml", request, context) @register_route(HttpMethod.GET, '/manifest.json') @@ -247,5 +239,4 @@ async def handle_theme(app: Application, request: Request, theme: str) -> Respon except KeyError: return Response.new('Invalid theme', 404) - data = app.template.render('variables.css', request, **context) - return Response.new(data, ctype = 'css') + return Response.new_template(200, "variables.css", request, context, ctype = "css")