From 8160c375f448f4e6e32518d632e3bc0d139f0130 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Tue, 16 May 2023 20:13:44 +0200 Subject: add fietsctl track del --- doc/man/fietsctl.rst | 17 ++++++++++++++++- fietsboek/scripts/fietsctl.py | 44 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/doc/man/fietsctl.rst b/doc/man/fietsctl.rst index 4d3683f..67cbed4 100644 --- a/doc/man/fietsctl.rst +++ b/doc/man/fietsctl.rst @@ -11,7 +11,7 @@ SYNPOSIS .. code-block:: text fietsctl maintenance-mode - fietsctl track list + fietsctl track {del|list} fietsctl user {add|del|hittekaart|list|modify|passwd} fietsctl version @@ -181,6 +181,21 @@ For each track, the following information is shown: * The track's owner (both name and email address) * The track's title. +REMOVING A TRACK +################ + +.. code-block:: text + + fietsctl track del [-c CONFIG] -i/--id ID [-f/--force] + +Deletes the specified track. The right ID can be found via the ``track list`` +command, or via the web interface. + +This command deletes the track including its pictures and comments. + +By default, the command will ask for confirmation. You can specify the +``-f``/``--force`` flag to override this check. + MAINTENANCE MODE **************** diff --git a/fietsboek/scripts/fietsctl.py b/fietsboek/scripts/fietsctl.py index 4950f7e..59462df 100644 --- a/fietsboek/scripts/fietsctl.py +++ b/fietsboek/scripts/fietsctl.py @@ -1,5 +1,6 @@ """Script to do maintenance work on a Fietsboek instance.""" # pylint: disable=too-many-arguments +import logging from typing import Optional import click @@ -12,6 +13,8 @@ from .. import __VERSION__, hittekaart, models, util from ..data import DataManager from . import config_option +LOGGER = logging.getLogger("fietsctl") + EXIT_OKAY = 0 EXIT_FAILURE = 1 @@ -361,6 +364,47 @@ def cmd_track_list(config: str): ) +@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) -- cgit v1.2.3