aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Schadt <kingdread@gmx.de>2023-02-08 23:51:46 +0100
committerDaniel Schadt <kingdread@gmx.de>2023-02-08 23:51:46 +0100
commitdb470d5f6f9c543a105ef34553beaa74870c3fa8 (patch)
treead8d0283faa85ff3e613ba931ce82229621ca2b3
parent94f2f0758a79f797233cfd7e7db241e3eca2e700 (diff)
downloadfietsboek-db470d5f6f9c543a105ef34553beaa74870c3fa8.tar.gz
fietsboek-db470d5f6f9c543a105ef34553beaa74870c3fa8.tar.bz2
fietsboek-db470d5f6f9c543a105ef34553beaa74870c3fa8.zip
fix transformer handling when uploading a file
-rw-r--r--fietsboek/actions.py39
-rw-r--r--fietsboek/models/track.py3
-rw-r--r--fietsboek/templates/edit_form.jinja22
-rw-r--r--fietsboek/transformers/__init__.py28
-rw-r--r--fietsboek/views/upload.py3
-rw-r--r--tests/playwright/conftest.py1
-rw-r--r--tests/playwright/test_basic.py4
7 files changed, 53 insertions, 27 deletions
diff --git a/fietsboek/actions.py b/fietsboek/actions.py
index 350faaf..7a69913 100644
--- a/fietsboek/actions.py
+++ b/fietsboek/actions.py
@@ -16,7 +16,9 @@ from pyramid.request import Request
from sqlalchemy import select
from sqlalchemy.orm.session import Session
-from . import models, transformers, util
+from . import models
+from . import transformers as mod_transformers
+from . import util
from .data import DataManager
from .models.track import TrackType, Visibility
@@ -35,6 +37,7 @@ def add_track(
badges: List[models.Badge],
tagged_people: List[models.User],
tags: List[str],
+ transformers: list[mod_transformers.Transformer],
gpx_data: bytes,
) -> models.Track:
"""Adds a track to the database.
@@ -56,6 +59,7 @@ def add_track(
:param badges: Badges to attach to the track.
:param tagged_people: List of people to tag.
:param tags: List of text tags for the track.
+ :param transformers: List of :class:`~fietsboek.transformers.Transformer` to apply.
:param gpx_data: Actual GPX data (uncompressed, straight from the source).
:return: The track object that has been inserted into the database. Useful
for its ``id`` attribute.
@@ -88,6 +92,12 @@ def add_track(
manager.compress_gpx(gpx_data)
manager.backup()
+ gpx = gpxpy.parse(gpx_data)
+ for transformer in transformers:
+ LOGGER.debug("Running %s with %r", transformer, transformer.parameters)
+ transformer.execute(gpx)
+ track.transformers = [[tfm.identifier(), tfm.parameters.dict()] for tfm in transformers]
+
manager.engrave_metadata(
title=track.title,
description=track.description,
@@ -170,23 +180,9 @@ def execute_transformers(request: Request, track: models.Track):
# pylint: disable=too-many-locals
LOGGER.debug("Executing transformers for %d", track.id)
- settings = []
- for tfm in transformers.list_transformers():
- ident = tfm.identifier()
- prefix = f"transformer[{ident}]"
- req_params = {}
- for name, val in request.params.items():
- if name.startswith(prefix):
- name = name[len(prefix) :]
- name = name.strip("[]")
- req_params[name] = val
-
- if req_params.get("") == "on":
- params = tfm().parameters
- params.read_from_request(req_params)
- settings.append((ident, params))
-
- serialized = [[tfm_id, params.dict()] for tfm_id, params in settings]
+ settings = mod_transformers.extract_from_request(request)
+
+ serialized = [[tfm.identifier(), tfm.parameters.dict()] for tfm in settings]
if serialized == track.transformers:
LOGGER.debug("Applied transformations mach on %d, skipping", track.id)
return
@@ -199,11 +195,8 @@ def execute_transformers(request: Request, track: models.Track):
gpx_bytes = brotli.decompress(gpx_bytes)
gpx = gpxpy.parse(gpx_bytes)
- tfms = {tfm.identifier(): tfm for tfm in transformers.list_transformers()}
- for tfm_id, params in settings:
- transformer = tfms[tfm_id]()
- transformer.parameters = params
- LOGGER.debug("Running %s with %r", transformer, params)
+ for transformer in settings:
+ LOGGER.debug("Running %s with %r", transformer, transformer.parameters)
transformer.execute(gpx)
LOGGER.debug("Saving transformed file for %d", track.id)
diff --git a/fietsboek/models/track.py b/fietsboek/models/track.py
index a7217bf..9342cff 100644
--- a/fietsboek/models/track.py
+++ b/fietsboek/models/track.py
@@ -415,6 +415,9 @@ class Track(Base):
:param transformer_id: The string ID of the transformer.
:return: The settings as a dictionary.
"""
+ if not self.transformers:
+ return None
+
for t_id, settings in self.transformers:
if t_id == transformer_id:
return settings
diff --git a/fietsboek/templates/edit_form.jinja2 b/fietsboek/templates/edit_form.jinja2
index 491db98..8f6e092 100644
--- a/fietsboek/templates/edit_form.jinja2
+++ b/fietsboek/templates/edit_form.jinja2
@@ -155,7 +155,7 @@
</div>
<div id="transformer-{{ loop.index}}" class="accordion-collapse collapse" aria-labelledby="transformer-heading-{{ loop.index }}">
<div class="accordion-body">
- {% set params = track.transformer_params_for(transformer.identifier()) %}
+ {% set params = track.transformer_params_for(transformer.identifier()) if track else None %}
<!-- Short transformer description -->
<p>{{ _(transformer.description()) }}</p>
diff --git a/fietsboek/transformers/__init__.py b/fietsboek/transformers/__init__.py
index 1a9517e..b22c492 100644
--- a/fietsboek/transformers/__init__.py
+++ b/fietsboek/transformers/__init__.py
@@ -17,6 +17,7 @@ from gpxpy.gpx import GPX
from markupsafe import Markup
from pydantic import BaseModel
from pyramid.i18n import TranslationString
+from pyramid.request import Request
_ = TranslationString
@@ -157,4 +158,31 @@ def list_transformers() -> list[type[Transformer]]:
]
+def extract_from_request(request: Request) -> list[Transformer]:
+ """Extracts the list of transformers to execute from the given request.
+
+ Note that this sets up the transformers with the right parameters.
+
+ :param request: The pyramid request.
+ :return: The list of prepared transformers.
+ """
+ transformers = []
+ for tfm in list_transformers():
+ ident = tfm.identifier()
+ prefix = f"transformer[{ident}]"
+ req_params = {}
+ for name, val in request.params.items():
+ if name.startswith(prefix):
+ name = name[len(prefix) :]
+ name = name.strip("[]")
+ req_params[name] = val
+
+ if req_params.get("") == "on":
+ instance = tfm()
+ instance.parameters.read_from_request(req_params)
+ transformers.append(instance)
+
+ return transformers
+
+
__all__ = ["Parameters", "Transformer", "list_transformers"]
diff --git a/fietsboek/views/upload.py b/fietsboek/views/upload.py
index e172339..15fa990 100644
--- a/fietsboek/views/upload.py
+++ b/fietsboek/views/upload.py
@@ -9,7 +9,7 @@ from pyramid.response import Response
from pyramid.view import view_config
from sqlalchemy import select
-from .. import actions, models, util
+from .. import actions, models, transformers, util
from ..models.track import TrackType, Visibility
LOGGER = logging.getLogger(__name__)
@@ -170,6 +170,7 @@ def do_finish_upload(request):
tagged_people=tagged_people,
date=date,
tags=request.params.getall("tag[]"),
+ transformers=transformers.extract_from_request(request),
gpx_data=upload.gpx_data,
)
request.dbsession.delete(upload)
diff --git a/tests/playwright/conftest.py b/tests/playwright/conftest.py
index 18b7ad0..f57aca7 100644
--- a/tests/playwright/conftest.py
+++ b/tests/playwright/conftest.py
@@ -126,6 +126,7 @@ class Helper:
tags=[],
badges=[],
tagged_people=[],
+ transformers=[],
gpx_data=load_gpx_asset(track_name),
)
self.dbaccess.commit()
diff --git a/tests/playwright/test_basic.py b/tests/playwright/test_basic.py
index 3b3329a..a98e52d 100644
--- a/tests/playwright/test_basic.py
+++ b/tests/playwright/test_basic.py
@@ -51,7 +51,7 @@ def test_upload(page: Page, playwright_helper, tmp_path, dbaccess):
page.locator(".bi-upload").click()
# We now fill in most of the data
- page.get_by_label("Title").fill("An awesome track!")
+ page.get_by_label("Title", exact=True).fill("An awesome track!")
page.get_by_label("Date").type("07302022")
page.get_by_label("Date").press("Tab")
page.get_by_label("Date").type("12:41")
@@ -82,7 +82,7 @@ def test_edit(page: Page, playwright_helper, dbaccess):
page.locator(".btn", has_text="Edit").click()
# We now fill in most of the data
- page.get_by_label("Title").fill("Not so awesome anymore!")
+ page.get_by_label("Title", exact=True).fill("Not so awesome anymore!")
page.get_by_label("Date").type("09232019")
page.get_by_label("Date").press("Tab")
page.get_by_label("Date").type("15:28")