-
Notifications
You must be signed in to change notification settings - Fork 18
/
views.py
53 lines (36 loc) · 1.5 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import urllib.parse
from datetime import datetime
from pprint import pformat
from henango.http.request import HTTPRequest
from henango.http.response import HTTPResponse
from henango.template.renderer import render
def now(request: HTTPRequest) -> HTTPResponse:
"""
現在時刻を表示するHTMLを生成する
"""
context = {"now": datetime.now()}
body = render("now.html", context)
return HTTPResponse(body=body)
def show_request(request: HTTPRequest) -> HTTPResponse:
"""
HTTPリクエストの内容を表示するHTMLを生成する
"""
context = {"request": request, "headers": pformat(request.headers), "body": request.body.decode("utf-8", "ignore")}
body = render("show_request.html", context)
return HTTPResponse(body=body)
def parameters(request: HTTPRequest) -> HTTPResponse:
"""
POSTパラメータを表示するHTMLを表示する
"""
# GETリクエストの場合は、405を返す
if request.method == "GET":
body = b"<html><body><h1>405 Method Not Allowed</h1></body></html>"
return HTTPResponse(body=body, status_code=405)
elif request.method == "POST":
context = {"params": urllib.parse.parse_qs(request.body.decode())}
body = render("parameters.html", context)
return HTTPResponse(body=body)
def user_profile(request: HTTPRequest) -> HTTPResponse:
context = {"user_id": request.params["user_id"]}
body = render("user_profile.html", context)
return HTTPResponse(body=body)