-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.py
163 lines (143 loc) · 5.22 KB
/
backend.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# -*- coding: utf-8 -*-
from pathlib import Path
from litestar import Litestar, Request, get, post
from litestar.config.compression import CompressionConfig
from litestar.config.cors import CORSConfig
from litestar.contrib.jinja import JinjaTemplateEngine
from litestar.response import Template
from litestar.static_files import create_static_files_router
from litestar.template.config import TemplateConfig
from missions import MissionGatherer
from notification import send_notification
cors_config = CORSConfig(allow_origins=["*"])
ASSETS_DIR = Path("assets")
def initialization():
global mission_gatherer
mission_gatherer = MissionGatherer()
@post("/send_report")
async def send_report(request: Request) -> None:
source_site = request.headers.get("hx-current-url", "")
form_data = await request.form()
username = form_data.get("report_author", None)
content = form_data.get("report_content", None)
if not username:
username = "Anonymous"
if content:
send_notification(username, content, source_site)
@get("/")
async def index(request: Request) -> Template:
entry_point = request.query_params.get(
"entry_point", f"http://{request.headers.get('host')}"
)
language = request.query_params.get("language", "en")
if language == "en":
from translations.website.english import (
BOOKS,
DIFFICULTIES,
EVENT_FILTERS,
FILTERS,
MAPS,
UI_TRANSLATIONS,
)
elif language == "zh-tw":
from translations.website.traditional_chinese import (
BOOKS,
DIFFICULTIES,
EVENT_FILTERS,
FILTERS,
MAPS,
UI_TRANSLATIONS,
)
elif language == "zh-cn":
from translations.website.simplified_chinese import (
BOOKS,
DIFFICULTIES,
EVENT_FILTERS,
FILTERS,
MAPS,
UI_TRANSLATIONS,
)
context = {
"server_entry_point": entry_point.strip("/"),
"language": language,
"ui_translations": UI_TRANSLATIONS,
"difficulties": DIFFICULTIES,
"filters": FILTERS,
"event_filters": EVENT_FILTERS,
"maps": MAPS,
"books": BOOKS,
}
return Template(template_name="index.html", context=context)
@post("/get_missions")
async def get_missions(request: Request) -> Template:
try:
if request.headers.get("hx-request", None) == "true":
form_data = await request.form()
language = form_data.get("language", "en")
if language == "zh-tw":
mission_gatherer.language = "zh-tw"
from translations.website.traditional_chinese import (
FILTERS,
UI_TRANSLATIONS,
)
elif language == "zh-cn":
mission_gatherer.language = "zh-cn"
from translations.website.simplified_chinese import (
FILTERS,
UI_TRANSLATIONS,
)
else:
mission_gatherer.language = "en"
from translations.website.english import FILTERS, UI_TRANSLATIONS
filter_keywords = [
key
for key in form_data
if key != "language"
and key != "auric_only"
and key != "auric_maelstrom_only"
and key != "entry_point"
]
mission_gatherer.filter_keywords = filter_keywords
if form_data.get("auric_maelstrom_only", "false") == "on":
auric_maelstrom_only = True
mission_gatherer.filter_keywords.append(FILTERS["Hi-Intensity"])
else:
auric_maelstrom_only = False
mission_data = mission_gatherer.get_requested_missions(
auric_maelstrom_only=auric_maelstrom_only
)
if form_data.get("auric_only", "false") == "on":
mission_data = [
mission for mission in mission_data if mission["is_auric"] is True
]
context = {
"missions": mission_data,
"ui_translations": UI_TRANSLATIONS,
"server_entry_point": form_data.get("entry_point", ""),
}
if mission_data:
return Template(template_name="mission.html", context=context)
else:
return Template(template_name="empty_result.html", context=context)
except Exception as e:
source_site = request.headers.get("hx-current-url", "")
error_form_data = "".join(
[f"- {key}: {value}\n" for key, value in form_data.items()]
)
send_notification(
"System",
f"{e}\n\nForm data:\n{error_form_data}",
source_site,
)
app = Litestar(
[
index,
get_missions,
create_static_files_router(path="/assets", directories=[ASSETS_DIR]),
send_report,
],
template_config=TemplateConfig(directory="templates", engine=JinjaTemplateEngine),
cors_config=cors_config,
on_startup=[initialization],
compression_config=CompressionConfig(backend="gzip", gzip_compress_level=9),
)