aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Schadt <kingdread@gmx.de>2023-02-21 00:00:34 +0100
committerDaniel Schadt <kingdread@gmx.de>2023-02-21 00:00:34 +0100
commitb423cc4a2deaa54cf529a1823f5977b5b5ef9b89 (patch)
tree8224421e5d0dc404ab34ed2cf46d42d0e87004b2
parent4a3ebb1a0a71c02c1057b6fd6c6054afe3bfa876 (diff)
downloadfietsboek-b423cc4a2deaa54cf529a1823f5977b5b5ef9b89.tar.gz
fietsboek-b423cc4a2deaa54cf529a1823f5977b5b5ef9b89.tar.bz2
fietsboek-b423cc4a2deaa54cf529a1823f5977b5b5ef9b89.zip
rework transformer parameter UI logic
I don't like interweaving the HTML code into the business logic, so now we can have that in the Jinja template. Ideally, the list of definitions would be generated automatically from the model attributes.
-rw-r--r--fietsboek/templates/edit_form.jinja216
-rw-r--r--fietsboek/transformers/__init__.py34
2 files changed, 36 insertions, 14 deletions
diff --git a/fietsboek/templates/edit_form.jinja2 b/fietsboek/templates/edit_form.jinja2
index 8f6e092..1abe076 100644
--- a/fietsboek/templates/edit_form.jinja2
+++ b/fietsboek/templates/edit_form.jinja2
@@ -169,9 +169,9 @@
<!-- Parameters as defined by the transformer -->
{% if params is not none %}
- {{ transformer.parameter_model().parse_obj(params).html_ui("transformer[{}][%%]".format(transformer.identifier())) }}
+ {{ render_parameters(transformer.identifier(), transformer.parameter_model().parse_obj(params).html_ui()) }}
{% else %}
- {{ transformer().parameters.html_ui("transformer[{}][%%]".format(transformer.identifier())) }}
+ {{ render_parameters(transformer.identifier(), transformer().parameters.html_ui()) }}
{% endif %}
</div>
</div>
@@ -179,3 +179,15 @@
</div>
</div>
{% endmacro %}
+
+{% macro render_parameters(identifier, params) %}
+{% for param in params %}
+{% if param.type == "int" %}
+<label for="transformer[{{ identifier }}][{{ param.name }}" class="form-label">{{ _(param.label) }}</label>
+<input id="transformer[{{ identifier }}][{{ param.name }}]" name="transformer[{{ identifier }}][{{ param.name }}]" type="number" value="{{ param.value }}" class="form-control" />
+{% elif param.type == "str" %}
+<label for="transformer[{{ identifier }}][{{ param.name }}" class="form-label">{{ _(param.label) }}</label>
+<input id="transformer[{{ identifier }}][{{ param.name }}]" name="transformer[{{ identifier }}][{{ param.name }}]" type="text" value="{{ param.value }}" class="form-control" />
+{% endif %}
+{% endfor %}
+{% endmacro %}
diff --git a/fietsboek/transformers/__init__.py b/fietsboek/transformers/__init__.py
index 1f084a0..f3afb57 100644
--- a/fietsboek/transformers/__init__.py
+++ b/fietsboek/transformers/__init__.py
@@ -12,10 +12,9 @@ function to load and apply transformers.
from abc import ABC, abstractmethod
from collections.abc import Mapping
from itertools import chain, islice
-from typing import TypeVar
+from typing import Literal, NamedTuple, TypeVar
from gpxpy.gpx import GPX
-from markupsafe import Markup
from pydantic import BaseModel
from pyramid.i18n import TranslationString
from pyramid.request import Request
@@ -25,6 +24,25 @@ _ = TranslationString
T = TypeVar("T", bound="Transformer")
+class ParameterDefinition(NamedTuple):
+ """A parameter definition for the UI to render."""
+
+ type: Literal["int", "str"]
+ """Type of the parameter."""
+
+ name: str
+ """Name of the parameter.
+
+ This is the machine-readable identifier, not the human readable name.
+ """
+
+ label: TranslationString
+ """Human-readable label of the parameter."""
+
+ value: str
+ """The serialized value of the parameter."""
+
+
class Parameters(BaseModel):
"""Parameters for a transformer.
@@ -32,26 +50,18 @@ class Parameters(BaseModel):
parameters to be serialized from and to POST request parameters.
"""
- def html_ui(self, name_template: str) -> Markup:
+ def html_ui(self) -> list[ParameterDefinition]:
"""Renders a HTML UI for this parameter set.
- :param name_template: The template for the HTML form element names,
- with a %% placeholder for the parameter name.
:return: The rendered UI, ready for inclusion.
"""
- # TODO: Implement this based on the model's fields
- # This is probably done better in the actual template, we shouldn't
- # return Markup straight away.
- # Also think of the localization.
- return Markup()
+ return []
def read_from_request(self, data: Mapping[str, str]):
"""Parses the parameters from the given request data.
- :param prefix: The prefix of the input parameter names.
:param data: The request data, e.g. from the POST values.
"""
- # TODO: Implement parsing
class Transformer(ABC):