From 008598a2adca058c55170aa2d2999cf1f930e71b Mon Sep 17 00:00:00 2001 From: Ilona Podliashanyk Date: Thu, 19 Sep 2024 17:47:23 +0200 Subject: [PATCH 1/5] Prepare edit form for uncrispyfying --- python/nav/web/templates/seeddb/edit.html | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/python/nav/web/templates/seeddb/edit.html b/python/nav/web/templates/seeddb/edit.html index 0bceef9664..fe76665408 100644 --- a/python/nav/web/templates/seeddb/edit.html +++ b/python/nav/web/templates/seeddb/edit.html @@ -41,9 +41,14 @@

{{ title }}
Attributes - {% block crispyfields %} - {{ form|crispy }} - {% endblock %} + {% if form.no_crispy %} + {% block formfields %} + {% endblock %} + {% else %} + {% block crispyfields %} + {{ form|crispy }} + {% endblock %} + {% endif %}
From 927bb8486134923359947478d330b7ffec55f24c Mon Sep 17 00:00:00 2001 From: Ilona Podliashanyk Date: Thu, 19 Sep 2024 17:55:07 +0200 Subject: [PATCH 2/5] Add template for form fields Is mostly universal, yet fields that require extra classes (like f.e. select2) need extra logic. Meaning that with few tweaks this template has a potential to fix all add/edit templates in /seeddb. Will finish and showcase this in a separate PR fixup --- .../nav/web/templates/seeddb/_form_fields.html | 18 ++++++++++++++++++ python/nav/web/templates/seeddb/edit.html | 17 +++++++++-------- 2 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 python/nav/web/templates/seeddb/_form_fields.html diff --git a/python/nav/web/templates/seeddb/_form_fields.html b/python/nav/web/templates/seeddb/_form_fields.html new file mode 100644 index 0000000000..31fd5483cd --- /dev/null +++ b/python/nav/web/templates/seeddb/_form_fields.html @@ -0,0 +1,18 @@ +{% for field in form %} +
+ + {{ field }} + {% for error in field.field.errors %} + + {{ error }} + + {% endfor %} +
+{% endfor %} diff --git a/python/nav/web/templates/seeddb/edit.html b/python/nav/web/templates/seeddb/edit.html index fe76665408..b411845138 100644 --- a/python/nav/web/templates/seeddb/edit.html +++ b/python/nav/web/templates/seeddb/edit.html @@ -41,14 +41,15 @@

{{ title }}
Attributes - {% if form.no_crispy %} - {% block formfields %} - {% endblock %} - {% else %} - {% block crispyfields %} - {{ form|crispy }} - {% endblock %} - {% endif %} + {% if form.no_crispy %} + {% block formfields %} + {% include "seeddb/_form_fields.html" %} + {% endblock %} + {% else %} + {% block crispyfields %} + {{ form|crispy }} + {% endblock %} + {% endif %}
From 25ce6c1c477cb8e3111d655828c0e61f670e40b5 Mon Sep 17 00:00:00 2001 From: Ilona Podliashanyk Date: Thu, 19 Sep 2024 18:01:18 +0200 Subject: [PATCH 3/5] Use non-crispy DeviceGroupForm in seeddb --- python/nav/web/seeddb/forms/__init__.py | 11 +++-------- python/nav/web/seeddb/page/netboxgroup.py | 1 - .../nav/web/templates/seeddb/edit_device_group.html | 5 ----- 3 files changed, 3 insertions(+), 14 deletions(-) delete mode 100644 python/nav/web/templates/seeddb/edit_device_group.html diff --git a/python/nav/web/seeddb/forms/__init__.py b/python/nav/web/seeddb/forms/__init__.py index 6f67e1e897..fc866a275f 100644 --- a/python/nav/web/seeddb/forms/__init__.py +++ b/python/nav/web/seeddb/forms/__init__.py @@ -22,7 +22,7 @@ from django.utils.safestring import mark_safe from crispy_forms.helper import FormHelper -from crispy_forms_foundation.layout import Layout, Field, Fieldset, Row, Column +from crispy_forms_foundation.layout import Layout, Fieldset, Row, Column from nav.django.forms import HStoreField from nav.web.crispyforms import LabelSubmit @@ -372,6 +372,8 @@ class DeviceGroupForm(forms.ModelForm): netboxes = forms.ModelMultipleChoiceField( queryset=Netbox.objects.all(), required=False ) + netboxes.widget.attrs.update({"class": "select2"}) + no_crispy = True def __init__(self, *args, **kwargs): # If the form is based on an existing model instance, populate the @@ -380,13 +382,6 @@ def __init__(self, *args, **kwargs): initial = kwargs.setdefault('initial', {}) initial['netboxes'] = [n.pk for n in kwargs['instance'].netboxes.all()] forms.ModelForm.__init__(self, *args, **kwargs) - self.helper = FormHelper() - self.helper.form_tag = False - self.helper.layout = Layout( - 'id', - 'description', - Field('netboxes', css_class='select2'), - ) class Meta(object): model = NetboxGroup diff --git a/python/nav/web/seeddb/page/netboxgroup.py b/python/nav/web/seeddb/page/netboxgroup.py index 22e6fb8ddb..c11e812de1 100644 --- a/python/nav/web/seeddb/page/netboxgroup.py +++ b/python/nav/web/seeddb/page/netboxgroup.py @@ -118,7 +118,6 @@ def netboxgroup_edit(request, netboxgroup_id=None): DeviceGroupForm, netboxgroup_id, 'seeddb-netboxgroup-edit', - template='seeddb/edit_device_group.html', extra_context=extra_context, ) diff --git a/python/nav/web/templates/seeddb/edit_device_group.html b/python/nav/web/templates/seeddb/edit_device_group.html deleted file mode 100644 index 1d57c64635..0000000000 --- a/python/nav/web/templates/seeddb/edit_device_group.html +++ /dev/null @@ -1,5 +0,0 @@ -{% extends "seeddb/edit.html" %} -{% load crispy_forms_tags %} -{% block crispyfields %} -{% crispy form %} -{% endblock %} From a410bb857468454606883adc5950e855e970486d Mon Sep 17 00:00:00 2001 From: Ilona Podliashanyk Date: Fri, 20 Sep 2024 10:11:30 +0200 Subject: [PATCH 4/5] Fix typo so that field errors can be rendered --- python/nav/web/templates/seeddb/_form_fields.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/nav/web/templates/seeddb/_form_fields.html b/python/nav/web/templates/seeddb/_form_fields.html index 31fd5483cd..51b8c89bcb 100644 --- a/python/nav/web/templates/seeddb/_form_fields.html +++ b/python/nav/web/templates/seeddb/_form_fields.html @@ -9,7 +9,7 @@ {% if field.field.required %}*{% endif %} {{ field }} - {% for error in field.field.errors %} + {% for error in field.errors %} {{ error }} From 7fe2f74e04b509a6b613ea03f190702e3e4dff84 Mon Sep 17 00:00:00 2001 From: podliashanyk <60876078+podliashanyk@users.noreply.github.com> Date: Fri, 20 Sep 2024 11:17:56 +0200 Subject: [PATCH 5/5] Update python/nav/web/templates/seeddb/_form_fields.html Co-authored-by: Johanna England --- python/nav/web/templates/seeddb/_form_fields.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/nav/web/templates/seeddb/_form_fields.html b/python/nav/web/templates/seeddb/_form_fields.html index 51b8c89bcb..582491b806 100644 --- a/python/nav/web/templates/seeddb/_form_fields.html +++ b/python/nav/web/templates/seeddb/_form_fields.html @@ -1,6 +1,6 @@ {% for field in form %}