Skip to content

Commit

Permalink
adds widgets and templates for the form renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
mbi committed Mar 26, 2024
1 parent 69e433f commit 0505c7f
Show file tree
Hide file tree
Showing 8 changed files with 364 additions and 30 deletions.
87 changes: 57 additions & 30 deletions project_name/apps/cruncher/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,55 @@ class FileForm(forms.Form):


class CruncherErrorList(ErrorList):
def __init__(self, initlist=None, error_class=None, html_id=None):
def __init__(self, initlist=None, error_class=None, renderer=None, html_id=None):
self._html_id = html_id
super().__init__(initlist=initlist, error_class=error_class)
super().__init__(initlist=initlist, error_class=error_class, renderer=renderer)

def as_flat(self):
return self.as_ul()

def as_ul(self):
if not self.data:
return ''
return ""

return format_html_join(
'{}',
"{}",
'<label {} class="error-label">{}</label>',
(('for={}'.format(self._html_id) if self._html_id else '', e) for e in self),
(
("for={}".format(self._html_id) if self._html_id else "", e)
for e in self
),
)


class CruncherFormRenderer(forms.Form):
def __init__(self, *args, **kwargs):
kwargs.update(error_class=CruncherErrorList)
super().__init__(*args, **kwargs)
def __init__(
self,
data=None,
files=None,
auto_id="id_%s",
prefix=None,
initial=None,
error_class=ErrorList,
label_suffix=None,
empty_permitted=False,
field_order=None,
use_required_attribute=None,
renderer=None,
):
super().__init__(
data=data,
files=files,
auto_id=auto_id,
prefix=prefix,
initial=initial,
error_class=CruncherErrorList,
label_suffix=label_suffix,
empty_permitted=empty_permitted,
field_order=field_order,
use_required_attribute=use_required_attribute,
renderer=renderer,
)

def _html_output(
self,
Expand All @@ -56,15 +83,15 @@ def _html_output(
output, hidden_fields = [], []

for name, field in self.fields.items():
html_class_attr = ''
html_class_attr = ""
bf = self[name]
bf_errors = self.error_class(bf.errors, html_id=bf.id_for_label)
if bf.is_hidden:
if bf_errors:
top_errors.extend(
[
_('(Hidden field %(name)s) %(error)s')
% {'name': name, 'error': str(e)}
_("(Hidden field %(name)s) %(error)s")
% {"name": name, "error": str(e)}
for e in bf_errors
]
)
Expand All @@ -78,28 +105,28 @@ def _html_output(

if bf.label:
label = conditional_escape(bf.label)
label = bf.label_tag(label) or ''
label = bf.label_tag(label) or ""
else:
label = ''
label = ""

if field.help_text:
help_text = mark_safe(
'<label for="%s" class="help-label">%s</label>'
% (bf.id_for_label, field.help_text)
)
else:
help_text = ''
help_text = ""

output.append(
normal_row
% {
'errors': '',
'label': label,
'field': bf,
'help_text': help_text,
'html_class_attr': html_class_attr,
'css_classes': css_classes,
'field_name': bf.html_name,
"errors": "",
"label": label,
"field": bf,
"help_text": help_text,
"html_class_attr": html_class_attr,
"css_classes": css_classes,
"field_name": bf.html_name,
}
)

Expand All @@ -110,7 +137,7 @@ def _html_output(
output.insert(0, error_row % top_errors)

if hidden_fields: # Insert any hidden fields in the last row.
str_hidden = ''.join(hidden_fields)
str_hidden = "".join(hidden_fields)
if output:
last_row = output[-1]
# Chop off the trailing row_ender (e.g. '</td></tr>') and
Expand All @@ -121,18 +148,18 @@ def _html_output(
# not be able to conscript the last row for our purposes,
# so insert a new, empty row.
last_row = normal_row % {
'errors': '',
'label': '',
'field': '',
'help_text': '',
'html_class_attr': html_class_attr,
'css_classes': '',
'field_name': '',
"errors": "",
"label": "",
"field": "",
"help_text": "",
"html_class_attr": html_class_attr,
"css_classes": "",
"field_name": "",
}
output.append(last_row)
output[-1] = last_row[: -len(row_ender)] + str_hidden + row_ender
else:
# If there aren't any rows in the output, just append the
# hidden fields.
output.append(str_hidden)
return mark_safe('\n'.join(output))
return mark_safe("\n".join(output))
2 changes: 2 additions & 0 deletions project_name/apps/cruncher/templates/widgets/checkbox.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{% include "django/forms/widgets/input.html" %}
<label class="check-label {{widget.attrs.label_class}}" for="{{widget.attrs.id}}">{{widget.attrs.label}}</label>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% load l10n %}
<increment-control>
<svg slot="decrement-button" viewbox="0 0 32 32" aria-hidden="true"><use href="#minus"></use></svg>
<svg slot="increment-button" viewbox="0 0 32 32" aria-hidden="true"><use href="#plus"></use></svg>
{% include "django/forms/widgets/number.html" %}
</increment-control>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{% include "django/forms/widgets/input.html" %}
<label class="radio-label {{widget.attrs.label_class}}" for="{{widget.attrs.id}}">{{widget.label}}</label>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% load widget_tweaks_fork %}

{% with id=widget.attrs.id %}
{% for group, options, index in widget.optgroups %}
{% for option in options %}
{% include "./radio-button.html" with widget=option %}
{% endfor %}
{% endfor %}
{% endwith %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% load widget_tweaks_fork %}

<label class="select-button button {{widget.attrs.class|default:""}}" for="{{widget.attrs.id}}">
{% include "django/forms/widgets/select.html" %}
</label>
Loading

0 comments on commit 0505c7f

Please sign in to comment.