From 3e36e89459b563bee78579e628756e8f54afaca2 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Tue, 5 May 2026 23:23:57 +0200 Subject: add fietsctl track add --- fietsboek/scripts/fietsctl.py | 76 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/fietsboek/scripts/fietsctl.py b/fietsboek/scripts/fietsctl.py index fa751b7..d08be17 100644 --- a/fietsboek/scripts/fietsctl.py +++ b/fietsboek/scripts/fietsctl.py @@ -2,6 +2,7 @@ # pylint: disable=too-many-positional-arguments,too-many-arguments import logging +from pathlib import Path from typing import Optional import click @@ -11,8 +12,9 @@ from pyramid.scripting import AppEnvironment from sqlalchemy import select from sqlalchemy.exc import NoResultFound -from .. import __VERSION__, hittekaart, models, util +from .. import __VERSION__, actions, convert, hittekaart, models, util from ..data import DataManager +from ..views.tileproxy import ITileRequester from . import config_option LOGGER = logging.getLogger("fietsctl") @@ -380,6 +382,78 @@ def cmd_track_list(config: str): ) +@cmd_track.command("add") +@config_option +@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.argument("filename", nargs=-1) +@click.pass_context +def cmd_track_add( + ctx: click.Context, + config: str, + id_: int | None, + email: str | None, + filename: list[str], +): + """Add a track. + + The track is added for the specified user. + + The track type is set to "organic" and the visibility to "private". + """ + 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: + request = env["request"] + dbsession = 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) + + for fname in filename: + fpath = Path(fname) + try: + gpx_bytes = fpath.read_bytes() + except Exception as exc: + click.secho("Error", fg="red", nl=False) + click.echo(f" in {fname}: {exc}") + continue + + try: + track = convert.smart_convert(gpx_bytes) + except Exception as exc: + click.secho("Error", fg="red", nl=False) + click.echo(f" in {fname}: {exc}") + continue + + title = track.title or fpath.name + saved = actions.add_track( + dbsession, + request.data_manager, + request.registry.getUtility(ITileRequester), + request.config.public_tile_layers()[0], + user, + title, + track.date, + models.track.Visibility.PRIVATE, + models.track.TrackType.ORGANIC, + "", + [], + [], + [], + [], + gpx_bytes, + ) + + click.secho("Success", fg="green", nl=False) + click.echo(f" {fname} saved as track {saved.id}") + + @cmd_track.command("del") @config_option @click.option("--force", "-f", help="Override the safety check.", is_flag=True) -- cgit v1.2.3 From ec14aee4ac369260506e167c4c35862f6827d381 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Tue, 5 May 2026 23:48:38 +0200 Subject: add visibility/track type option for track add --- fietsboek/scripts/fietsctl.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/fietsboek/scripts/fietsctl.py b/fietsboek/scripts/fietsctl.py index d08be17..7702f05 100644 --- a/fietsboek/scripts/fietsctl.py +++ b/fietsboek/scripts/fietsctl.py @@ -384,6 +384,18 @@ def cmd_track_list(config: str): @cmd_track.command("add") @config_option +@click.option( + "--visibility", + help="Visibility of the added tracks.", + type=click.Choice([vis.name for vis in models.track.Visibility]), + default="PUBLIC", +) +@click.option( + "--track-type", + help="Type of the added tracks.", + type=click.Choice([typ.name for typ in models.track.TrackType]), + default="ORGANIC", +) @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.") @@ -392,6 +404,8 @@ def cmd_track_list(config: str): def cmd_track_add( ctx: click.Context, config: str, + visibility: str, + track_type: str, id_: int | None, email: str | None, filename: list[str], @@ -407,6 +421,8 @@ def cmd_track_add( query = select(models.User).filter_by(id=id_) else: query = models.User.query_by_email(email) + visibility = models.track.Visibility[visibility] + track_type = models.track.TrackType[track_type] with env["request"].tm: request = env["request"] dbsession = request.dbsession @@ -440,8 +456,8 @@ def cmd_track_add( user, title, track.date, - models.track.Visibility.PRIVATE, - models.track.TrackType.ORGANIC, + visibility, + track_type, "", [], [], -- cgit v1.2.3 From 66a245a9a56421a7d7eabf2e529808bc9e4d205b Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Tue, 5 May 2026 23:57:31 +0200 Subject: add manpage entry for fietsctl track add --- doc/man/fietsctl.rst | 20 ++++++++++++++++++++ fietsboek/scripts/fietsctl.py | 4 +--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/doc/man/fietsctl.rst b/doc/man/fietsctl.rst index 1d30b20..8a91408 100644 --- a/doc/man/fietsctl.rst +++ b/doc/man/fietsctl.rst @@ -186,6 +186,26 @@ For each track, the following information is shown: * The track's owner (both name and email address) * The track's title. +ADDING A TRACK +############## + +.. code-block:: text + + fietsctl track add [-c CONFIG] [-i/--id ID] [-e/--email] [--visibility PRIVATE|FRIENDS|FRIENDS_TAGGED|LOGGED_IN|PUBLIC] [--track-type ORGANIC|SYNTHETIC] FILENAME... + +Adds a track to the system. You specify the owner via the ``--id`` or +``--email`` options, similar to the ``user modify`` command. + +One or multiple ``FILENAME`` might be given. Any supported input format may be +used. + +The titles and dates are attempted to be read from the files. Other metadata +(tags, images, ...) is left empty. The ``--visibility`` and ``--track-type`` +options can be used to set the visibility and type of the added tracks. + +For each file read, the resulting ID in the database is printed. If an error +occurs for one file, the error is shown and the next track is read. + REMOVING A TRACK ################ diff --git a/fietsboek/scripts/fietsctl.py b/fietsboek/scripts/fietsctl.py index 7702f05..bd6aae8 100644 --- a/fietsboek/scripts/fietsctl.py +++ b/fietsboek/scripts/fietsctl.py @@ -412,9 +412,7 @@ def cmd_track_add( ): """Add a track. - The track is added for the specified user. - - The track type is set to "organic" and the visibility to "private". + The track is added for the specified user (via --id/--email). """ env = setup(config) if id_ is not None: -- cgit v1.2.3