Skip to content

Commit

Permalink
Form rendering simplified
Browse files Browse the repository at this point in the history
  • Loading branch information
zgoda committed Jul 17, 2020
1 parent 9898cca commit 505a70b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 31 deletions.
8 changes: 6 additions & 2 deletions src/bip/admin/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ class PageForm(ObjectForm):
'tekst', validators=[InputRequired()],
description='treść strony zapisana przy użyciu Markdown',
)
description = TextAreaField('opis')
active = BooleanField('aktywna')
description = TextAreaField(
'opis', description='opis strony indeksowany przez wyszukiwarki internetowe'
)
active = BooleanField(
'aktywna', description='strony nieaktywne są dostępne wyłącznie w archiwum'
)
main = BooleanField(
'główna', description='czy strona ma być widoczna na liście stron w menu'
)
Expand Down
4 changes: 3 additions & 1 deletion src/bip/main/templates/main/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

{% block headpagetitle %}Strona: {{ page.title }}{% endblock %}

{% block pagedescription %}{{ page.description }}{% endblock %}

{% block content %}
<h2>{{ page.title }}</h2>
{{ page.text_html|safe }}
Expand All @@ -19,7 +21,7 @@ <h3>Załączniki</h3>
</ul>
{% endif %}
<hr>
<table class="table table-bordered">
<table class="table table-bordered table-sm">
<tr>
<td>Utworzona</td>
<td>{{ page.created|datetimeformat(format='short') }}, {{ page.created_by.name }}</td>
Expand Down
2 changes: 2 additions & 0 deletions src/bip/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<meta charset="utf-8">
<title>{% block headpagetitle %}{% endblock %}</title>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="{% block pagedescription %}{% endblock %}">
<link rel="stylesheet" href="/static/vendor/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="/static/vendor/fontawesome/css/all.min.css">
<link rel="stylesheet" href="/static/css/app.css">
Expand Down Expand Up @@ -66,6 +67,7 @@ <h1>{{ site.name }}</h1>
{% endif %}
{% endwith %}

<!-- main page content -->
<div class="row my-2 pt-4">

<!-- content area -->
Expand Down
88 changes: 60 additions & 28 deletions src/bip/templates/macros/forms.html
Original file line number Diff line number Diff line change
@@ -1,33 +1,66 @@
{% from 'bootstrap/form.html' import form_errors, render_field %}
{% from 'bootstrap/form.html' import form_errors %}

{% macro render_form_fields(form, form_type="horizontal", horizontal_columns=('sm', 2, 10)) %}
{% set hints = kwargs.pop('hints', None) %}
{% macro field_description(field) %}
{% if field.errors %}
{% for error in field.errors %}
<div class="invalid-feedback d-block">{{ error }}</div>
{% endfor %}
{% else %}
{% if field.description %}
<small class="form-text text-muted">{{ field.description|safe }}</small>
{% endif %}
{% endif %}
{% endmacro %}

{% macro render_field(field) %}
{% set is_checkbox = field.widget.input_type == 'checkbox' %}
{% set is_radio = field.widget.input_type == 'radio' %}
{% set is_file = field.widget.input_type == 'file' %}
{% set is_required = field.flags.required %}
{% set has_errors = field.errors %}
<div class="form-group row{% if is_required %} required{% endif %}">
{% if not (is_radio or is_checkbox) %}
<label for="{{ field.id }}" class="col-sm-2 col-form-label">{{ field.label.text|safe }}</label>
{% endif %}
{% if not is_radio %}
<div class="col-sm-10{% if is_checkbox %} offset-sm-2{% endif %}">
{% if is_checkbox %}
<div class="form-check">
{% if has_errors %}
{{ field(class_='form-check-input is-invalid') }}
{% else %}
{{ field(class_='form-check-input') }}
{% endif %}
<label class="form-check-label" for="{{ field.id }}">{{ field.label.text|safe }}</label>
</div>
{% else %}
{% if is_file %}
{% if has_errors %}
{{ field(class_='form-control-file is-invalid') }}
{% else %}
{{ field(class_='form-control-file is-valid') }}
{% endif %}
{% else %}
{% if has_errors %}
{{ field(class_='form-control is-invalid') }}
{% else %}
{{ field(class_='form-control') }}
{% endif %}
{% endif %}
{% endif %}
{{ field_description(field) }}
</div>
{% endif %}
</div>
{% endmacro %}

{% macro render_form_fields(form) %}
{{ form.hidden_tag() }}
{{ form_errors(form, hiddens='only') }}
{% for field in form %}
{% if not bootstrap_is_hidden_field(field) %}
{% if field.type == 'DecimalField' %}
{{ render_field(
field,
form_type=form_type,
horizontal_columns=horizontal_columns,
button_map=button_map,
step=pow(10, field.places * -1)) }}
{% elif field.type == 'TextAreaWithHintsField' and hints %}
{{ render_field(
field,
form_type=form_type,
horizontal_columns=horizontal_columns,
button_map=button_map,
hints=hints) }}
{% else %}
{{ render_field(
field,
form_type=form_type,
horizontal_columns=horizontal_columns,
button_map=button_map) }}
{% endif %}
{% endif %}
{% if not bootstrap_is_hidden_field(field) %}
{{ render_field(field) }}
{% endif %}
{% endfor %}
{% endmacro %}

Expand All @@ -42,11 +75,10 @@
{% endmacro %}

{% macro render_form(form, action) %}
{% set hints = kwargs.pop('hints', None) %}
{% set enctype = kwargs.pop('enctype', None) %}
{% set method = kwargs.pop('method', 'POST') %}
<form method="{{ method }}" action="{{ action }}" {% if enctype %}enctype="{{ enctype }}"{% endif %} class="form form-horizontal" role="form" novalidate>
{{ render_form_fields(form, hints=hints) }}
{{ render_form_fields(form) }}
{{ render_form_buttons(form.buttons) }}
</form>
{% endmacro %}

0 comments on commit 505a70b

Please sign in to comment.