-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponents.py
265 lines (229 loc) · 7.55 KB
/
components.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
"FastHTML components and functions."
import os
import string
from fasthtml.common import *
import auth
import books
import constants
from errors import *
import users
import utils
from utils import Tx
def get_fast_app(routes=None):
app, rt = fast_app(
live="WRITETHATBOOK_DEVELOPMENT" in os.environ,
static_path="static",
before=users.set_current_user,
hdrs=(Link(rel="stylesheet", href="/mods.css", type="text/css"),),
exception_handlers={
Error: error_handler,
NotAllowed: not_allowed_handler,
},
routes=routes,
)
setup_toasts(app)
return app, rt
def redirect(href):
"Redirect with the usually more appropriate 303 status code."
return RedirectResponse(href, status_code=HTTP.SEE_OTHER)
def blank(width, style=None):
if isinstance(width, (int, float)):
width = str(width) + "em"
if style:
style += f" padding: 0 {width};"
else:
style = f"padding: 0 {width};"
return Span(NotStr(" "), style=style)
def save_button(text="Save"):
return Button(Tx(text), style="width: 10em;")
def cancel_button(href):
return Div(
A(
Tx("Cancel"),
role="button",
href=href,
cls="outline secondary",
style="width: 10em;",
),
style="margin-top: 1em;",
)
def search_form(action, term=None):
return Form(
Input(
name="term",
type="search",
placeholder=Tx("Search"),
value=term,
autofocus=True,
),
Input(type="submit", value=Tx("Search")),
role="search",
action=action,
method="post",
)
def header(request, title, book=None, status=None, actions=None):
"The standard page header with navigation bar."
# General menu items.
menu = [
A(Tx("Books"), href="/"),
A(Tx("References"), href="/refs"),
]
# Links to pages for book.
if book:
if book is books.get_refs():
menu.extend(
[
A(Tx("Keywords"), href="/refs/keywords"),
A(Tx("Recently modified"), href="/refs/recent"),
A(Tx("Download TGZ file"), href="/refs/all.tgz"),
]
)
else:
menu.extend(
[
A(f'{Tx("Search in")} {Tx("book")}', href=f"/search/{book}"),
A(Tx("Index"), href=f"/meta/index/{book}"),
A(Tx("Recently modified"), href=f"/meta/recent/{book}"),
A(Tx("Status list"), href=f"/meta/status/{book}"),
A(Tx("Information"), href=f"/meta/info/{book}"),
A(Tx("Book state (JSON)"), href=f"/state/{book}"),
A(Tx("Download DOCX file"), href=f"/book/{book}.docx"),
A(Tx("Download PDF file"), href=f"/book/{book}.pdf"),
A(Tx("Download TGZ file"), href=f"/book/{book}.tgz"),
]
)
# Links to pages for admin.
if auth.is_admin(request):
menu.extend(
[
A(Tx("All users"), href="/user/list"),
A(Tx("Download dump file"), href="/dump"),
A(Tx("Site state (JSON)"), href="/state"),
A(Tx("System"), href="/meta/system"),
]
)
# Links to general pages.
menu.append(A(Tx("Software"), href="/meta/software"))
if actions:
actions = [A(NotStr(Tx(text)), href=href) for text, href in actions]
else:
actions = []
if auth.is_admin(request):
actions.append(A(Tx("Login"), href=f"/user/login?path={request.url.path}"))
# The first navbar item:
# - Pulldown for links to pages.
# - Pulldown for actions, if any.
# - Link to book, if any.
# - Title of page.
style = "background: white; padding: 4px;"
items = [
Li(
Details(
Summary(
Img(src="/writethatbook.png", style=style),
role="button",
cls="outline",
),
Ul(*[Li(a) for a in menu]),
cls="dropdown",
),
)
]
if actions:
items.append(
Li(
Details(
Summary(Img(src="/actions.svg", style=style)),
Ul(*[Li(a) for a in actions]),
cls="dropdown",
),
)
)
if book:
if book is books.get_refs():
items.append(Li(A(Tx("References"), href="/refs")))
else:
items.append(Li(A(book.title, href=f"/book/{book}")))
# The title of the page.
items.append(Li(Strong(title)))
navs = [Ul(*items)]
# The second navbar item: login button, if not logged in.
if auth.logged_in(request) is None:
navs.append(
Ul(
Li(
A(
Tx("Login"),
href=f"/user/login?path={request.url.path}",
role="button",
)
)
)
)
# Set the color of the nav frame.
nav_style = "outline-color: {color}; outline-width:8px; outline-style:solid; padding:0px 10px; border-radius:5px;"
if status:
nav_style = nav_style.format(color=status.color)
else:
nav_style = nav_style.format(color="black")
return Header(Nav(*navs, style=nav_style), cls="container")
def footer(request, item=None):
if item:
cells = [
Div(Tx(item.status), title=Tx("Status")),
Div(item.modified, title=Tx("Modified")),
]
if item.type in (constants.BOOK, constants.SECTION):
cells.append(
Div(
f'{thousands(item.sum_words)} {Tx("words")}; ',
f'{thousands(item.sum_characters)} {Tx("characters")}',
" (",
f'{thousands(item.n_words)} {Tx("words")}; ',
f'{thousands(item.n_characters)} {Tx("characters")}',
")",
)
)
else:
cells.append(
Div(
f'{thousands(item.n_words)} {Tx("words")}; ',
f'{thousands(item.n_characters)} {Tx("characters")}',
)
)
else:
cells = [Div(), Div(), Div()]
user = auth.logged_in(request)
if user:
if auth.authorized(request, *auth.user_view_rules, user=user):
cells.append(
Div(
A(user.name or user.id, href=f"/user/view/{user.id}"),
title=Tx("Logged in"),
style="text-align: right;",
)
)
else:
cells.append(Div(user.name or user.id), style="text-align: right;")
else:
cells.append(Div())
return Footer(
Hr(),
Div(*cells, cls="grid"),
cls="container",
)
def get_status_field(item):
"Return select input field for status."
status_options = []
for status in constants.STATUSES:
if item.status == status:
status_options.append(
Option(Tx(str(status)), selected=True, value=repr(status))
)
else:
status_options.append(Option(Tx(str(status)), value=repr(status)))
return Select(*status_options, name="status", required=True)
def required():
return Span(NotStr(" *"), style="color: red")
def thousands(i):
return f"{i:,}".replace(",", ".")