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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
"""Script to do maintenance work on a Fietsboek instance."""
# pylint: disable=too-many-arguments
import logging
from typing import Optional
import click
from click_option_group import RequiredMutuallyExclusiveOptionGroup, optgroup
from pyramid.paster import bootstrap, setup_logging
from pyramid.scripting import AppEnvironment
from sqlalchemy import select
from .. import __VERSION__, hittekaart, models, util
from ..data import DataManager
from . import config_option
LOGGER = logging.getLogger("fietsctl")
EXIT_OKAY = 0
EXIT_FAILURE = 1
FG_USER_NAME = "yellow"
FG_USER_EMAIL = "yellow"
FG_USER_TAG = "red"
FG_SIZE = "cyan"
FG_TRACK_TITLE = "green"
FG_TRACK_SIZE = "bright_red"
def human_bool(value: bool) -> str:
"""Formats a boolean for CLI output.
:param value: The value to format.
:return: The string representing the bool.
"""
return "yes" if value else "no"
def setup(config_path: str) -> AppEnvironment:
"""Sets up the logging and app environment for the scripts.
This - for example - sets up a transaction manager in
``setup(...)["request"]``.
:param config_path: Path to the configuration file.
:return: The prepared environment.
"""
setup_logging(config_path)
return bootstrap(config_path)
@click.group(
help=__doc__,
context_settings={"help_option_names": ["-h", "--help"]},
)
@click.version_option(package_name="fietsboek")
def cli():
"""CLI main entry point."""
@cli.group("user")
def cmd_user():
"""Management functions for user accounts."""
@cmd_user.command("add")
@config_option
@click.option("--email", help="Email address of the user.", prompt=True)
@click.option("--name", help="Name of the user.", prompt=True)
@click.option(
"--password",
help="password of the user",
prompt=True,
hide_input=True,
confirmation_prompt=True,
)
@click.option("--admin", help="Make the new user an admin.", is_flag=True)
@click.pass_context
def cmd_user_add(
ctx: click.Context,
config: str,
email: str,
name: str,
password: str,
admin: bool,
):
"""Create a new user.
This user creation bypasses the "enable_account_registration" setting. It
also immediately sets the new user's account to being verified.
If email, name or password are not given as command line arguments, they
will be asked for interactively.
On success, the created user's unique ID will be printed.
Note that this function does less input validation and should therefore be used with care!
"""
env = setup(config)
# The UNIQUE constraint only prevents identical emails from being inserted,
# but does not take into account the case insensitivity. The least we
# should do here to not brick log ins for the user is to check if the email
# already exists.
query = models.User.query_by_email(email)
with env["request"].tm:
result = env["request"].dbsession.execute(query).scalar_one_or_none()
if result is not None:
click.echo("Error: The given email already exists!", err=True)
ctx.exit(EXIT_FAILURE)
user = models.User(name=name, email=email, is_verified=True, is_admin=admin)
user.set_password(password)
with env["request"].tm:
dbsession = env["request"].dbsession
dbsession.add(user)
dbsession.flush()
user_id = user.id
click.echo(user_id)
@cmd_user.command("del")
@config_option
@click.option("--force", "-f", help="Override the safety check.", is_flag=True)
@optgroup.group("User selection", cls=RequiredMutuallyExclusiveOptionGroup)
@optgroup.option("--id", "-i", "id_", help="Database ID of the user.", type=int)
@optgroup.option("--email", "-e", help="Email address of the user.")
@click.pass_context
def user_del(
ctx: click.Context,
config: str,
force: bool,
id_: Optional[int],
email: Optional[str],
):
"""Delete a user.
This command deletes the user's account as well as any tracks associated
with it.
This command is destructive and irreversibly deletes data.
"""
env = setup(config)
if id_ is not None:
query = select(models.User).filter_by(id=id_)
else:
query = models.User.query_by_email(email)
with env["request"].tm:
dbsession = env["request"].dbsession
user = dbsession.execute(query).scalar_one_or_none()
if user is None:
click.echo("Error: No such user found.", err=True)
ctx.exit(EXIT_FAILURE)
click.secho(user.name, fg=FG_USER_NAME)
click.echo(user.email)
if not force:
if not click.confirm("Really delete this user?"):
click.echo("Aborted by user.")
ctx.exit(EXIT_FAILURE)
dbsession.delete(user)
click.echo("User deleted")
@cmd_user.command("list")
@config_option
def cmd_user_list(config: str):
"""Prints a listing of all user accounts.
The format is:
[av] {ID} - {email} - {name}
with one line per user. The 'a' is added for admin accounts, the 'v' is added
for verified users.
"""
env = setup(config)
with env["request"].tm:
dbsession = env["request"].dbsession
users = dbsession.execute(select(models.User).order_by(models.User.id)).scalars()
for user in users:
# pylint: disable=consider-using-f-string
tag = "[{}{}]".format(
"a" if user.is_admin else "-",
"v" if user.is_verified else "-",
)
tag = click.style(tag, fg=FG_USER_TAG)
user_email = click.style(user.email, fg=FG_USER_EMAIL)
user_name = click.style(user.name, fg=FG_USER_NAME)
click.echo(f"{tag} {user.id} - {user_email} - {user_name}")
@cmd_user.command("passwd")
@config_option
@click.option("--password", help="Password of the user.")
@optgroup.group("User selection", cls=RequiredMutuallyExclusiveOptionGroup)
@optgroup.option("--id", "-i", "id_", help="Database ID of the user,", type=int)
@optgroup.option("--email", "-e", help="Email address of the user.")
@click.pass_context
def cms_user_passwd(
ctx: click.Context,
config: str,
password: Optional[str],
id_: Optional[int],
email: Optional[str],
):
"""Change the password of a user."""
env = setup(config)
if id_ is not None:
query = select(models.User).filter_by(id=id_)
else:
query = models.User.query_by_email(email)
with env["request"].tm:
dbsession = env["request"].dbsession
user = dbsession.execute(query).scalar_one_or_none()
if user is None:
click.echo("Error: No such user found.", err=True)
ctx.exit(EXIT_FAILURE)
if not password:
password = click.prompt("Password", hide_input=True, confirmation_prompt=True)
user.set_password(password)
user_name = click.style(user.name, fg=FG_USER_NAME)
user_email = click.style(user.email, fg=FG_USER_EMAIL)
click.echo(f"Changed password of {user_name} ({user_email})")
@cmd_user.command("modify")
@config_option
@click.option("--admin/--no-admin", help="Make the user an admin.", default=None)
@click.option("--verified/--no-verified", help="Set the user verification status.", default=None)
@optgroup.group("User selection", cls=RequiredMutuallyExclusiveOptionGroup)
@optgroup.option("--id", "-i", "id_", help="Database ID of the user.", type=int)
@optgroup.option("--email", "-e", help="Email address of the user.")
@click.pass_context
def cms_user_modify(
ctx: click.Context,
config: str,
admin: Optional[bool],
verified: Optional[bool],
id_: Optional[int],
email: Optional[str],
):
"""Modify a user."""
env = setup(config)
if id_ is not None:
query = select(models.User).filter_by(id=id_)
else:
query = models.User.query_by_email(email)
with env["request"].tm:
dbsession = env["request"].dbsession
user = dbsession.execute(query).scalar_one_or_none()
if user is None:
click.echo("Error: No such user found.", err=True)
ctx.exit(EXIT_FAILURE)
if admin is not None:
user.is_admin = admin
if verified is not None:
user.is_verified = verified
user_name = click.style(user.name, fg=FG_USER_NAME)
user_email = click.style(user.email, fg=FG_USER_EMAIL)
click.echo(f"{user_name} - {user_email}")
click.echo(f"Is admin: {human_bool(user.is_admin)}")
click.echo(f"Is verified: {human_bool(user.is_verified)}")
@cmd_user.command("hittekaart")
@config_option
@click.option(
"--mode",
"modes",
help="Heatmap type to generate.",
type=click.Choice([mode.value for mode in hittekaart.Mode]),
multiple=True,
default=["heatmap"],
)
@click.option("--delete", help="Delete the specified heatmap.", is_flag=True)
@optgroup.group("User selection", cls=RequiredMutuallyExclusiveOptionGroup)
@optgroup.option("--id", "-i", "id_", help="Database ID of the user.", type=int)
@optgroup.option("--email", "-e", help="Email address of the user.")
@click.pass_context
def cmd_user_hittekaart(
ctx: click.Context,
config: str,
modes: list[str],
delete: bool,
id_: Optional[int],
email: Optional[str],
):
"""Generate heatmap for a user."""
env = setup(config)
modes = [hittekaart.Mode(mode) for mode in modes]
if id_ is not None:
query = select(models.User).filter_by(id=id_)
else:
query = models.User.query_by_email(email)
exe_path = env["request"].config.hittekaart_bin
threads = env["request"].config.hittekaart_threads
with env["request"].tm:
dbsession = env["request"].dbsession
data_manager: DataManager = env["request"].data_manager
user = dbsession.execute(query).scalar_one_or_none()
if user is None:
click.echo("Error: No such user found.", err=True)
ctx.exit(EXIT_FAILURE)
if delete:
try:
user_manager = data_manager.open_user(user.id)
except FileNotFoundError:
return
if hittekaart.Mode.HEATMAP in modes:
user_manager.heatmap_path().unlink(missing_ok=True)
if hittekaart.Mode.TILEHUNTER in modes:
user_manager.tilehunt_path().unlink(missing_ok=True)
return
click.echo(f"Generating overlay maps for {click.style(user.name, fg=FG_USER_NAME)}...")
for mode in modes:
hittekaart.generate_for(
user, dbsession, data_manager, mode, exe_path=exe_path, threads=threads
)
click.echo(f"Generated {mode.value}")
@cli.group("track")
def cmd_track():
"""Management functions for tracks."""
@cmd_track.command("list")
@config_option
def cmd_track_list(config: str):
"""List all tracks that are present in the system."""
env = setup(config)
total_size = 0
total_tracks = 0
with env["request"].tm:
dbsession = env["request"].dbsession
data_manager: DataManager = env["request"].data_manager
tracks = dbsession.execute(select(models.Track)).scalars()
for track in tracks:
total_tracks += 1
try:
track_size = data_manager.open(track.id).size()
except FileNotFoundError:
size = "---"
else:
total_size += track_size
size = util.human_size(track_size)
size = click.style(f"{size:>10}", fg=FG_TRACK_SIZE)
owner_name = click.style(track.owner.name, fg=FG_USER_NAME)
owner_email = click.style(track.owner.email, fg=FG_USER_EMAIL)
track_title = click.style(track.title, fg=FG_TRACK_TITLE)
click.echo(f"{track.id:>4} - {size} - {owner_name} <{owner_email}> - {track_title}")
click.echo("-" * 80)
click.echo(
f"Total: {total_tracks} - {click.style(util.human_size(total_size), fg=FG_TRACK_SIZE)}"
)
@cmd_track.command("del")
@config_option
@click.option("--force", "-f", help="Override the safety check.", is_flag=True)
@click.option("--id", "-i", "id_", help="Database ID of the track.", type=int, required=True)
@click.pass_context
def cmd_track_del(
ctx: click.Context,
config: str,
force: bool,
id_: Optional[int],
):
"""Delete a track.
This command deletes the track as well as any images and comments
associated with it.
This command is destructive and irreversibly deletes data.
"""
env = setup(config)
query = select(models.Track).filter_by(id=id_)
with env["request"].tm:
dbsession = env["request"].dbsession
track = dbsession.execute(query).scalar_one_or_none()
if track is None:
click.echo("Error: No such track found.", err=True)
ctx.exit(EXIT_FAILURE)
click.secho(track.title, fg=FG_TRACK_TITLE)
if not force:
if not click.confirm("Really delete this track?"):
click.echo("Aborted by user.")
ctx.exit(EXIT_FAILURE)
try:
data = env["request"].data_manager.open(track.id)
except FileNotFoundError:
LOGGER.warning("Data directory not found for track - ignoring")
else:
data.purge()
dbsession.delete(track)
click.echo("Track deleted")
@cli.command("maintenance-mode")
@config_option
@click.option("--disable", help="Disable the maintenance mode.", is_flag=True)
@click.argument("reason", required=False)
@click.pass_context
def cmd_maintenance_mode(ctx: click.Context, config: str, disable: bool, reason: Optional[str]):
"""Controls the maintenance mode.
When REASON is given, enables the maintenance mode with the given text as
reason.
With neither --disable nor REASON given, just checks the state of the
maintenance mode.
"""
env = setup(config)
data_manager = env["request"].data_manager
if disable and reason:
click.echo("Cannot enable and disable maintenance mode at the same time", err=True)
ctx.exit(EXIT_FAILURE)
elif not disable and not reason:
maintenance = data_manager.maintenance_mode()
if maintenance is None:
click.echo("Maintenance mode is disabled")
else:
click.echo(f"Maintenance mode is enabled: {maintenance}")
elif disable:
(data_manager.data_dir / "MAINTENANCE").unlink()
else:
(data_manager.data_dir / "MAINTENANCE").write_text(reason, encoding="utf-8")
@cli.command("version")
def cmd_version():
"""Show the installed fietsboek version."""
name = __name__.split(".", 1)[0]
print(f"{name} {__VERSION__}")
__all__ = ["cli"]
|