From 158592863db1662f7a828fe4ee7f34eaf713ffbf Mon Sep 17 00:00:00 2001 From: Daniil Mironenko Date: Sun, 8 Dec 2024 16:56:27 +0300 Subject: [PATCH 1/5] redirect response --- web/responses.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/web/responses.py b/web/responses.py index 895e548..5d602b3 100644 --- a/web/responses.py +++ b/web/responses.py @@ -1,4 +1,5 @@ from enum import Enum +from http import HTTPStatus from webob import Response as _Response from jinja2 import Environment, FileSystemLoader @@ -26,3 +27,15 @@ def __init__(self, template_path: str, context: dict, *args, templates_dir: str template = env.get_template(template_path) rendered_html = template.render(context) super().__init__(*args, body=rendered_html, content_type=ContentType.HTML.value, **kwargs) + + +class RedirectResponse(Response): + def __init__(self, location: str, *args, body: dict = None, status: HTTPStatus = 302, **kwargs): + super().__init__(*args, **kwargs) + self.status = HTTPStatus.MOVED_PERMANENTLY + self.headers['Location'] = location + + if body: + self.json_body = body + else: + self.body = b"" From 6e8a53eed699d020ca93c484e05462bb8c4d0b7e Mon Sep 17 00:00:00 2001 From: Daniil Mironenko Date: Sun, 8 Dec 2024 16:56:39 +0300 Subject: [PATCH 2/5] redirect response --- playground/main.py | 3 ++- web/responses.py | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/playground/main.py b/playground/main.py index 7d556d5..4a31331 100644 --- a/playground/main.py +++ b/playground/main.py @@ -35,7 +35,8 @@ def html_example(_request): @router.get('/hello') def get_example(request): logger.info(f'got {request=}') - return web.responses.JsonResponse({'message': 'hello, world!'}) + # return web.responses.JsonResponse({'message': 'hello, world!'}) + return web.responses.RedirectResponse('/main') @router.post('/hello', middlewares=[ExampleMiddleware]) diff --git a/web/responses.py b/web/responses.py index 5d602b3..defb1f3 100644 --- a/web/responses.py +++ b/web/responses.py @@ -30,9 +30,9 @@ def __init__(self, template_path: str, context: dict, *args, templates_dir: str class RedirectResponse(Response): - def __init__(self, location: str, *args, body: dict = None, status: HTTPStatus = 302, **kwargs): + def __init__(self, location: str, *args, body: dict = None, status: HTTPStatus = HTTPStatus.MOVED_PERMANENTLY, **kwargs): super().__init__(*args, **kwargs) - self.status = HTTPStatus.MOVED_PERMANENTLY + self.status = HTTPStatus.status self.headers['Location'] = location if body: From de5eb3161d65647aa96bb57e7cc553a092da7f21 Mon Sep 17 00:00:00 2001 From: Daniil Mironenko Date: Sun, 8 Dec 2024 16:57:13 +0300 Subject: [PATCH 3/5] update playground --- playground/main.py | 1 - 1 file changed, 1 deletion(-) diff --git a/playground/main.py b/playground/main.py index 4a31331..6817b88 100644 --- a/playground/main.py +++ b/playground/main.py @@ -35,7 +35,6 @@ def html_example(_request): @router.get('/hello') def get_example(request): logger.info(f'got {request=}') - # return web.responses.JsonResponse({'message': 'hello, world!'}) return web.responses.RedirectResponse('/main') From 0702f19fdb6ae28b727a98758e33b4f9efd66743 Mon Sep 17 00:00:00 2001 From: Daniil Mironenko Date: Sun, 8 Dec 2024 16:57:57 +0300 Subject: [PATCH 4/5] fix linting error --- web/responses.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/web/responses.py b/web/responses.py index defb1f3..89d36ef 100644 --- a/web/responses.py +++ b/web/responses.py @@ -30,9 +30,16 @@ def __init__(self, template_path: str, context: dict, *args, templates_dir: str class RedirectResponse(Response): - def __init__(self, location: str, *args, body: dict = None, status: HTTPStatus = HTTPStatus.MOVED_PERMANENTLY, **kwargs): + def __init__( + self, + location: str, + *args, + body: dict = None, + status: HTTPStatus = HTTPStatus.MOVED_PERMANENTLY, + **kwargs, + ): super().__init__(*args, **kwargs) - self.status = HTTPStatus.status + self.status = status self.headers['Location'] = location if body: From 6924bd413513ba24e8538f6aea582cdb54f32611 Mon Sep 17 00:00:00 2001 From: Daniil Mironenko Date: Sun, 8 Dec 2024 16:58:43 +0300 Subject: [PATCH 5/5] fix linting error --- web/responses.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/responses.py b/web/responses.py index 89d36ef..d218d18 100644 --- a/web/responses.py +++ b/web/responses.py @@ -34,7 +34,7 @@ def __init__( self, location: str, *args, - body: dict = None, + body: dict | None = None, status: HTTPStatus = HTTPStatus.MOVED_PERMANENTLY, **kwargs, ):