-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
71 lines (50 loc) · 1.74 KB
/
main.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import random
from starlette.applications import Starlette
from starlette.responses import Response
from starlette.routing import Route, Mount
from starlette.staticfiles import StaticFiles
from starlette.templating import Jinja2Templates
from translations import translations
templates = Jinja2Templates(directory="templates")
def slugify(string):
return string.lower().replace(" ", "-")
def get_context_data(request, man, woman, perm=False):
permalink = request.url_for("permalink", man=slugify(man), woman=slugify(woman),)
title = "Manslator"
if perm:
title = f"Manslator - {man} is {woman}"
return {
"man": man,
"woman": woman,
"permalink": permalink,
"perm": perm,
"title": title,
"description": "Are you confused with how to talk to and about women? Here is your helpful manslator.",
"request": request,
}
valid_routes = {}
for man, woman in translations:
valid_routes[(slugify(man), slugify(woman))] = (man, woman)
def permalink(request):
man = request.path_params["man"]
woman = request.path_params["woman"]
try:
translation = valid_routes[(man, woman)]
except KeyError:
return Response("Not found", status_code=404)
else:
man, woman = translation
return templates.TemplateResponse(
"translation.html", get_context_data(request, man, woman),
)
def home(request):
man, woman = random.choice(translations)
return templates.TemplateResponse(
"translation.html", get_context_data(request, man, woman),
)
routes = [
Route("/", home),
Route("/{man}-is-{woman}/", permalink),
Mount("/static", StaticFiles(directory="static")),
]
app = Starlette(routes=routes)