mirror of
https://git.pleroma.social/pleroma/relay.git
synced 2024-11-13 19:27:58 +00:00
create Response.new_template
method
This commit is contained in:
parent
f9d6d7b18d
commit
091f8175b5
|
@ -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
|
||||
|
|
|
@ -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', '')
|
||||
|
|
|
@ -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}')
|
||||
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in a new issue