This is an automated email from the ASF dual-hosted git repository.
sbp pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tooling-trusted-release.git
The following commit(s) were added to refs/heads/main by this push:
new f27bcf2 Add descriptions to htpy rendered forms
f27bcf2 is described below
commit f27bcf257938f59fdea2ce2992f3918c64da2867
Author: Sean B. Palmer <[email protected]>
AuthorDate: Tue Aug 5 20:42:48 2025 +0100
Add descriptions to htpy rendered forms
---
atr/forms.py | 93 ++++++++++++++++++++++++------------------------
atr/routes/distribute.py | 16 ++++-----
2 files changed, 54 insertions(+), 55 deletions(-)
diff --git a/atr/forms.py b/atr/forms.py
index 7c7a2c1..41fbe92 100644
--- a/atr/forms.py
+++ b/atr/forms.py
@@ -212,12 +212,14 @@ def render_columns(
action: str,
form_classes: str = ".atr-canary",
submit_classes: str = "btn-primary",
+ descriptions: bool = False,
) -> htpy.Element:
label_classes = "col-sm-3 col-form-label text-sm-end"
- elements = render_elements(
+ elements = _render_elements(
form,
label_classes=label_classes,
submit_classes=submit_classes,
+ descriptions=descriptions,
)
field_rows: list[htpy.Element] = []
@@ -236,56 +238,14 @@ def render_columns(
return htpy.form(form_classes, action=action, method="post")[form_children]
-def render_elements(
- form: Typed,
- label_classes: str = "col-sm-3 col-form-label text-sm-end",
- submit_classes: str = "btn-primary",
- small: bool = False,
-) -> Elements:
- hidden_elements: list[markupsafe.Markup] = []
- field_elements: list[tuple[markupsafe.Markup, markupsafe.Markup]] = []
- submit_element: markupsafe.Markup | None = None
-
- for field in form:
- if isinstance(field, wtforms.HiddenField):
- hidden_elements.append(markupsafe.Markup(str(field)))
- continue
-
- if isinstance(field, wtforms.StringField):
- label = markupsafe.Markup(str(field.label(class_=label_classes)))
- widget_class = "form-control"
- if small is True:
- widget_class += " form-control-sm"
- widget = markupsafe.Markup(str(field(class_=widget_class)))
- field_elements.append((label, widget))
- continue
-
- if isinstance(field, wtforms.SelectField):
- label = markupsafe.Markup(str(field.label(class_=label_classes)))
- widget_class = "form-select"
- if small is True:
- widget_class += " form-select-sm"
- widget = markupsafe.Markup(str(field(class_=widget_class)))
- field_elements.append((label, widget))
- continue
-
- if isinstance(field, wtforms.SubmitField):
- button_class = "btn " + submit_classes
- submit_element = markupsafe.Markup(str(field(class_=button_class)))
- continue
-
- raise TypeError(f"Unsupported field type: {type(field).__name__}")
-
- return Elements(hidden_elements, field_elements, submit_element)
-
-
def render_simple(
form: Typed,
action: str,
form_classes: str = "",
submit_classes: str = "btn-primary",
+ descriptions: bool = False,
) -> htpy.Element:
- elements = render_elements(form, submit_classes=submit_classes)
+ elements = _render_elements(form, submit_classes=submit_classes,
descriptions=descriptions)
field_rows: list[htpy.Element] = []
for label, widget in elements.fields:
@@ -309,9 +269,10 @@ def render_table(
form_classes: str = "",
table_classes: str = ".table.table-striped.table-bordered",
submit_classes: str = "btn-primary",
+ descriptions: bool = False,
) -> htpy.Element:
# Small elements in Bootstrap
- elements = render_elements(form, submit_classes=submit_classes, small=True)
+ elements = _render_elements(form, submit_classes=submit_classes,
small=True, descriptions=descriptions)
field_rows: list[htpy.Element] = []
for label, widget in elements.fields:
@@ -409,3 +370,43 @@ def url(
kwargs["render_kw"] = {}
kwargs["render_kw"]["placeholder"] = placeholder
return wtforms.URLField(label, validators=validators, **kwargs)
+
+
+def _render_elements(
+ form: Typed,
+ label_classes: str = "col-sm-3 col-form-label text-sm-end",
+ submit_classes: str = "btn-primary",
+ small: bool = False,
+ descriptions: bool = False,
+) -> Elements:
+ hidden_elements: list[markupsafe.Markup] = []
+ field_elements: list[tuple[markupsafe.Markup, markupsafe.Markup]] = []
+ submit_element: markupsafe.Markup | None = None
+
+ for field in form:
+ if isinstance(field, wtforms.HiddenField):
+ hidden_elements.append(markupsafe.Markup(str(field)))
+ continue
+
+ if isinstance(field, wtforms.StringField) or isinstance(field,
wtforms.SelectField):
+ label = markupsafe.Markup(str(field.label(class_=label_classes)))
+
+ widget_class = "form-control" if isinstance(field,
wtforms.StringField) else "form-select"
+ widget_classes = widget_class if (small is False) else
f"{widget_class}-sm"
+ widget = markupsafe.Markup(str(field(class_=widget_classes)))
+
+ if descriptions is True and field.description:
+ desc =
htpy.div(".form-text.text-muted")[str(field.description)]
+ widget += markupsafe.Markup(str(desc))
+
+ field_elements.append((label, widget))
+ continue
+
+ if isinstance(field, wtforms.SubmitField):
+ button_class = "btn " + submit_classes
+ submit_element = markupsafe.Markup(str(field(class_=button_class)))
+ continue
+
+ raise TypeError(f"Unsupported field type: {type(field).__name__}")
+
+ return Elements(hidden_elements, field_elements, submit_element)
diff --git a/atr/routes/distribute.py b/atr/routes/distribute.py
index 448c544..fa661ee 100644
--- a/atr/routes/distribute.py
+++ b/atr/routes/distribute.py
@@ -67,14 +67,12 @@ async def distribute_post(session: routes.CommitterSession,
project: str, versio
async def _distribute_page(*, project: str, version: str) -> str:
form = await DistributeForm.create_form(data={"package": project,
"version": version})
- form_content = forms.render_columns(form, action=quart.request.path)
- help_text = htpy.p[
- htpy.strong["Owner or Namespace"],
- """: this field in the form below describes who owns or names the
- package (Maven groupId, npm @scope, Docker namespace, GitHub owner,
- ArtifactHub repo). Leave blank if not used.""",
+ form_content = forms.render_columns(form, action=quart.request.path,
descriptions=True)
+ introduction = htpy.p[
+ """Record a manual distribution using the form below. Please note that
+ this form is a work in progress and not fully functional."""
]
- content = _page("Distribute", htpy.div[help_text, form_content])
+ content = _page("Distribute", introduction, form_content)
return await template.blank("Distribute", content=content)
@@ -111,5 +109,5 @@ def _distribute_post_table(data: dict[str, str]) ->
htpy.Element:
return htpy.table(".table.table-striped.table-bordered")[tbody]
-def _page(title_str: str, content: htpy.Element) -> htpy.Element:
- return htpy.div[htpy.h1[title_str], content]
+def _page(title_str: str, *content: htpy.Element) -> htpy.Element:
+ return htpy.div[htpy.h1[title_str], *content]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]