Skip to content

Commit

Permalink
Move a few functions around and have label after input field.
Browse files Browse the repository at this point in the history
  • Loading branch information
CoolCat467 committed Feb 8, 2024
1 parent 98c1b15 commit d4e8f5b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 41 deletions.
70 changes: 35 additions & 35 deletions src/sanescansrv/htmlgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,40 +186,6 @@ def contain_in_box(inside: str, name: str | None = None) -> str:
)


def radio_select_dict(
submit_name: str,
options: dict[str, str],
default: str | None = None,
) -> str:
"""Create radio select from dictionary."""
lines = []
for count, (display, value) in enumerate(options.items()):
cid = f"{submit_name}_{count}"
args = {
"type": "radio",
"id": cid,
"name": submit_name,
"value": value,
}
if value == default:
args["checked"] = "checked"
lines.append(tag("input", **args))
lines.append(wrap_tag("label", display, False, **{"for": cid}))
lines.append("<br>")
return "\n".join(lines)


def radio_select_box(
submit_name: str,
options: dict[str, str],
default: str | None = None,
box_title: str | None = None,
) -> str:
"""Create radio select value box from dictionary and optional names."""
radios = radio_select_dict(submit_name, options, default)
return contain_in_box("<br>\n" + radios, box_title)


def input_field(
field_id: str,
field_title: str | None,
Expand Down Expand Up @@ -250,12 +216,46 @@ def input_field(
raise ValueError(
f"Attribute {key!r} conflicts with an internal attribute",
)
lines.append(tag("input", **args))
if field_title is not None:
lines.append(wrap_tag("label", field_title, False, for_=field_id))
lines.append(tag("input", **args))
return "\n".join(lines)


def radio_select_dict(
submit_name: str,
options: dict[str, str],
default: str | None = None,
) -> str:
"""Create radio select from dictionary."""
lines = []
for count, (display, value) in enumerate(options.items()):
cid = f"{submit_name}_{count}"
args = {
"type": "radio",
"id": cid,
"name": submit_name,
"value": value,
}
if value == default:
args["checked"] = "checked"
lines.append(tag("input", **args))
lines.append(wrap_tag("label", display, False, **{"for": cid}))
lines.append("<br>")
return "\n".join(lines)


def radio_select_box(
submit_name: str,
options: dict[str, str],
default: str | None = None,
box_title: str | None = None,
) -> str:
"""Create radio select value box from dictionary and optional names."""
radios = radio_select_dict(submit_name, options, default)
return contain_in_box("<br>\n" + radios, box_title)


def bullet_list(values: list[str], **kwargs: TagArg) -> str:
"""Return HTML bulleted list from values."""
display = "\n".join(wrap_tag("li", v, block=False) for v in values)
Expand Down
12 changes: 6 additions & 6 deletions tests/test_htmlgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,16 +236,16 @@ def test_radio_select_box() -> None:
def test_input_field_no_kwarg() -> None:
assert (
htmlgen.input_field("<id>", "woot")
== """<label for="<id>">woot</label>
<input id="<id>" name="<id>">"""
== """<input id="<id>" name="<id>">
<label for="<id>">woot</label>"""
)


def test_input_field_with_type() -> None:
assert (
htmlgen.input_field("<id>", "woot", field_type="types woo")
== """<label for="<id>">woot</label>
<input type="types woo" id="<id>" name="<id>">"""
== """<input type="types woo" id="<id>" name="<id>">
<label for="<id>">woot</label>"""
)


Expand All @@ -257,8 +257,8 @@ def test_input_field_attrs() -> None:
field_type="types woo",
attrs={"autoselect": ""},
)
== """<label for="<id>">woot</label>
<input type="types woo" id="<id>" name="<id>" autoselect="">"""
== """<input type="types woo" id="<id>" name="<id>" autoselect="">
<label for="<id>">woot</label>"""
)


Expand Down

0 comments on commit d4e8f5b

Please sign in to comment.