From b423cc4a2deaa54cf529a1823f5977b5b5ef9b89 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Tue, 21 Feb 2023 00:00:34 +0100 Subject: 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. --- fietsboek/templates/edit_form.jinja2 | 16 ++++++++++++++-- fietsboek/transformers/__init__.py | 34 ++++++++++++++++++++++------------ 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 @@ {% 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 %} @@ -179,3 +179,15 @@ {% endmacro %} + +{% macro render_parameters(identifier, params) %} +{% for param in params %} +{% if param.type == "int" %} + + +{% elif param.type == "str" %} + + +{% 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): -- cgit v1.2.3