From fe6af96a31b4157b51a79e15bd4fd19e8ddaa025 Mon Sep 17 00:00:00 2001 From: Petri Saarinen Date: Sun, 29 Nov 2026 14:53:03 +0200 Subject: [PATCH] =?UTF-8?q?Korjaa=20monivalintalistat=20(palveluyksik?= =?UTF-8?q?=C3=B6t,=20muut=20j=C3=A4senet)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CheckboxSelectMultiple on RadioSelectin aliluokka, joten valintaruudut saivat daisyUI:n radio-luokan, ja sama luokka meni myös listan ympärillä olevalle divillä: lista kutistui pieneksi ympyräksi. - Widget-luokkien järjestys korjattu (checkbox ennen radiota). - Valintaryhmät renderöidään fieldset + legend -rakenteena, jokaisella vaihtoehdolla oma label; puhelimella yksi sarake, leveämmällä kaksi. - Testit rakenteelle, virheen liitokselle ja näppäimistövalinnalle. Co-Authored-By: Claude Opus 5.5 --- apps/jobs/tests/integration/test_jobs.py | 17 ++++++++++++++++ apps/shared/presentation/forms.py | 3 ++- templates/components/field.html | 25 ++++++++++++++++++++++++ tests/browser/test_jobs.py | 16 ++++++++++++++- 4 files changed, 59 insertions(+), 2 deletions(-) diff --git a/apps/jobs/tests/integration/test_jobs.py b/apps/jobs/tests/integration/test_jobs.py index be56cfc..61ecf8d 100644 --- a/apps/jobs/tests/integration/test_jobs.py +++ b/apps/jobs/tests/integration/test_jobs.py @@ -126,6 +126,23 @@ class TestCreateAndEdit: assert "Aili Alihankkija" in detail assert "Alustava" in detail and "Normaali" in detail + def test_choice_lists_are_checkbox_groups( + self, employee_client: Client, customer: CustomerModel, unit: ServiceUnitModel + ) -> None: + html = employee_client.get("/jobs/new/").content.decode() + assert "= 2 + assert f'type="checkbox" name="service_units" value="{unit.pk}" class="checkbox"' in html + assert 'type="checkbox" name="members"' in html + assert 'class="radio"' not in html + assert 'for="id_service_units"' not in html # no label pointing at the wrapper + + response = create(employee_client, customer, unit, service_units=[]) + assert response.status_code == 422 + page = response.content.decode() + assert 'aria-describedby="id_service_units_error"' in page + assert f'name="service_units" value="{unit.pk}" class="checkbox"' in page + assert "Valitse vähintään yksi palveluyksikkö." in page + def test_existing_tag_is_reused_ignoring_case( self, employee_client: Client, customer: CustomerModel, unit: ServiceUnitModel ) -> None: diff --git a/apps/shared/presentation/forms.py b/apps/shared/presentation/forms.py index 94d87b6..c0970d3 100644 --- a/apps/shared/presentation/forms.py +++ b/apps/shared/presentation/forms.py @@ -17,10 +17,11 @@ class DaisyFormRenderer(TemplatesSetting): field_template_name = "components/field.html" +# First isinstance match wins: CheckboxSelectMultiple is a subclass of RadioSelect. _WIDGET_CLASSES: list[tuple[type[forms.Widget], str]] = [ (forms.CheckboxInput, "checkbox"), - (forms.RadioSelect, "radio"), (forms.CheckboxSelectMultiple, "checkbox"), + (forms.RadioSelect, "radio"), (forms.Select, "select w-full"), (forms.Textarea, "textarea w-full"), (Input, "input w-full"), diff --git a/templates/components/field.html b/templates/components/field.html index 6718a11..99f3312 100644 --- a/templates/components/field.html +++ b/templates/components/field.html @@ -1,4 +1,28 @@ {% load i18n %}{% with widget_type=field.widget_type %} +{% if widget_type == "checkboxselectmultiple" or widget_type == "radioselect" %} +{# A group of choices: fieldset + legend, one labelled input per option (no wrapper div). #} +
+ + {{ field.label }}{% if field.field.required %} {% translate "(required)" %}{% endif %} + +
+ {% for option in field %} + + {% endfor %} +
+ {% if field.help_text %} +

{{ field.help_text }}

+ {% endif %} + {% if field.errors %} +

+ {% for error in field.errors %}{{ error }}{% if not forloop.last %} {% endif %}{% endfor %} +

+ {% endif %} +
+{% else %}
{% if widget_type == "checkbox" %}
+{% endif %} {% endwith %} diff --git a/tests/browser/test_jobs.py b/tests/browser/test_jobs.py index 76c49f7..aadd0fe 100644 --- a/tests/browser/test_jobs.py +++ b/tests/browser/test_jobs.py @@ -103,7 +103,19 @@ def test_create_job_with_keyboard_and_quick_added_customer( ) page.fill("#id_name", "Messuosasto") - page.locator("input[name='service_units']").first.check() + # The choice lists are real checkboxes: visible, large enough, toggled with Space. + unit_box = page.locator("input[name='service_units']").first + box = unit_box.bounding_box() + assert box is not None and box["width"] >= 16 and box["height"] >= 16 + unit_box.focus() + page.keyboard.press("Space") + assert unit_box.is_checked() + member_box = page.get_by_role("group", name="Muut jäsenet").get_by_role( + "checkbox", name=admin_user.name + ) + member_box.focus() + page.keyboard.press("Space") + assert member_box.is_checked() tag = page.locator("#tag-input") tag.fill("messut") tag.press("Enter") @@ -112,4 +124,6 @@ def test_create_job_with_keyboard_and_quick_added_customer( page.wait_for_url(f"{live_server.url}/jobs/*/") job = CustomerJobModel.objects.get(name="Messuosasto") assert job.customer.name == "Uusi Asiakas Oy" + assert list(job.service_unit_links.values_list("service_unit_id", flat=True)) == [unit.pk] + assert list(job.member_links.values_list("user_id", flat=True)) == [admin_user.pk] assert page.get_by_text("messut").count() >= 1