improved design of the form elements

This commit is contained in:
Joachim Lusiardi 2016-04-27 07:23:28 +02:00
parent ca7166f731
commit 3e2d62b28f
13 changed files with 73 additions and 30 deletions

View File

@ -38,6 +38,9 @@ from rollerverbrauch.entities import \
Pitstop, \ Pitstop, \
Vehicle Vehicle
# required to activate the filters
import rollerverbrauch.filters
user_datastore = SQLAlchemyUserDatastore(db, User, Role) user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore) security = Security(app, user_datastore)
@ -82,7 +85,7 @@ def edit_vehicle(vid):
form = EditVehicleForm() form = EditVehicleForm()
if form.validate_on_submit(): if form.validate_on_submit():
vehicle.name = form.name.data; vehicle.name = form.name.data
db.session.commit() db.session.commit()
return redirect(url_for('get_account_page')) return redirect(url_for('get_account_page'))

View File

@ -0,0 +1,9 @@
from rollerverbrauch import app
@app.template_filter('none_filter')
def none_filter(value):
if value is None:
return ''
else:
return value

View File

@ -1,5 +1,5 @@
from flask_wtf import Form from flask_wtf import Form
from wtforms import DateField, IntegerField, DecimalField, StringField, SelectField from wtforms import DateField, IntegerField, DecimalField, StringField, SelectField, SubmitField
from wtforms.validators import ValidationError, Length from wtforms.validators import ValidationError, Length
@ -20,12 +20,14 @@ def litres_check(form, field):
class SelectVehicleForm(Form): class SelectVehicleForm(Form):
vehicle = SelectField('Vehicle', coerce=int) vehicle = SelectField('Vehicle', coerce=int)
submit = SubmitField(label='Do it!')
class CreatePitstopForm(Form): class CreatePitstopForm(Form):
date = DateField('Date of Pitstop', validators=[date_check]) date = DateField('Date of Pitstop', validators=[date_check])
odometer = IntegerField('Odometer (km)', validators=[odometer_check]) odometer = IntegerField('Odometer (km)', validators=[odometer_check])
litres = DecimalField('Litres (l)', places=1, validators=[litres_check]) litres = DecimalField('Litres (l)', places=2, validators=[litres_check])
submit = SubmitField(label='Do it!')
pitstop = None pitstop = None
def set_pitstop(self, pitstop): def set_pitstop(self, pitstop):
@ -34,7 +36,8 @@ class CreatePitstopForm(Form):
class EditVehicleForm(Form): class EditVehicleForm(Form):
name = StringField('Name', validators=[Length(1, 255)]) name = StringField('Name', validators=[Length(1, 255)])
submit = SubmitField(label='Do it!')
class DeleteVehicleForm(Form): class DeleteVehicleForm(Form):
pass submit = SubmitField(label='Do it!')

View File

@ -5,7 +5,7 @@
<form class='form-horizontal' method="POST"> <form class='form-horizontal' method="POST">
{{ form.hidden_tag() }} {{ form.hidden_tag() }}
{{ render_field_with_errors(form.name) }} {{ render_field_with_errors(form.name) }}
<input type="submit" value="Go"> {{ render_field_with_errors(form.submit) }}
</form> </form>

View File

@ -4,7 +4,7 @@
<h3>Delete vehicle '{{vehicle.name}}'</h3> <h3>Delete vehicle '{{vehicle.name}}'</h3>
<form class='form-horizontal' method="POST"> <form class='form-horizontal' method="POST">
{{ form.hidden_tag() }} {{ form.hidden_tag() }}
<input type="submit" value="Go"> {{ render_field_with_errors(form.submit) }}
</form> </form>

View File

@ -3,9 +3,9 @@
{% block body %} {% block body %}
<h3>Edit vehicle</h3> <h3>Edit vehicle</h3>
<form class='form-horizontal' method="POST"> <form class='form-horizontal' method="POST">
{{ form.hidden_tag() }} {{ form.hidden_tag() }}
{{ render_field_with_errors(form.name) }} {{ render_field_with_errors(form.name) }}
<input type="submit" value="Go"> {{ render_field_with_errors(form.submit) }}
</form> </form>

View File

@ -15,19 +15,46 @@
{% macro render_field_with_errors(field) %} {% macro render_field_with_errors(field) %}
<div class="form-group"> <div class="form-group">
<label class="col-sm-6 control-label"> {% if field.type == 'SubmitField' %}
{{ field.label }} <div class="col-sm-12" style="align:center">
</label> <input id="{{ field.id }}" name="{{ field.id }}" class="btn btn-default" type="submit" value="{{ field.label.text }}">
<div class="col-sm-2"> </div>
{{ field(**kwargs)|safe }} {% else %}
{% if field.errors %} <label class="col-sm-6 control-label">
<p class='error'> {{ field.label }}
{% for error in field.errors %} </label>
{{ error }} <div class="col-sm-2">
{% endfor %} {% if field.type == 'SelectField' %}
</p> <select id="{{ field.id }}" name="{{ field.id }}" class="form-control">
{% endif %} {% for choice in field.choices %}
</div> <option value="{{ choice[0] }}">{{ choice[1] }}</option>
{% endfor %}
</select>
{% elif field.type == 'BooleanField' %}
<input class="form-control" type="checkbox" id="{{ field.id }}" name="{{ field.id }}" value="{{ field.default|none_filter }}" />
{% elif field.type == 'StringField' %}
<input class="form-control" type="text" id="{{ field.id }}" name="{{ field.id }}" value="{{ field.default|none_filter }}" />
{% elif field.type == 'PasswordField' %}
<input class="form-control" type="password" id="{{ field.id }}" name="{{ field.id }}" value="{{ field.default|none_filter }}" />
{% elif field.type == 'DateField' %}
<input class="form-control" type="date" id="{{ field.id }}" name="{{ field.id }}" value="{{ field.default|none_filter }}" />
{% elif field.type == 'IntegerField' %}
<input class="form-control" type="number" id="{{ field.id }}" name="{{ field.id }}" value="{{ field.default|none_filter }}" step="1" />
{% elif field.type == 'DecimalField' %}
<input class="form-control" type="number" id="{{ field.id }}" name="{{ field.id }}" value="{{ field.default|none_filter }}" step="{{ 1 / 10 ** field.places}}" />
{% else %}
{{ field(**kwargs)|safe }}
{% endif %}
{% if field.errors %}
<p class='error'>
{% for error in field.errors %}
{{ error }}
{% endfor %}
</p>
{% endif %}
</div>
{% endif %}
</div> </div>
{% endmacro %} {% endmacro %}

View File

@ -3,11 +3,11 @@
{% block body %} {% block body %}
<h3>New Pitstop for '{{ vehicle.name }}'</h3> <h3>New Pitstop for '{{ vehicle.name }}'</h3>
<form class='form-horizontal' method="POST"> <form class='form-horizontal' method="POST">
{{ form.hidden_tag() }} {{ form.hidden_tag() }}
{{ render_field_with_errors(form.date) }} {{ render_field_with_errors(form.date) }}
{{ render_field_with_errors(form.odometer) }} {{ render_field_with_errors(form.odometer) }}
{{ render_field_with_errors(form.litres) }} {{ render_field_with_errors(form.litres) }}
<input type="submit" value="Go"> {{ render_field_with_errors(form.submit) }}
</form> </form>

View File

@ -8,6 +8,6 @@
{{ render_field_with_errors(change_password_form.password) }} {{ render_field_with_errors(change_password_form.password) }}
{{ render_field_with_errors(change_password_form.new_password) }} {{ render_field_with_errors(change_password_form.new_password) }}
{{ render_field_with_errors(change_password_form.new_password_confirm) }} {{ render_field_with_errors(change_password_form.new_password_confirm) }}
{{ render_field(change_password_form.submit) }} {{ render_field_with_errors(change_password_form.submit) }}
</form> </form>
{% endblock %} {% endblock %}

View File

@ -6,6 +6,6 @@
<form class='form-horizontal' action="{{ url_for_security('forgot_password') }}" method="POST" name="forgot_password_form"> <form class='form-horizontal' action="{{ url_for_security('forgot_password') }}" method="POST" name="forgot_password_form">
{{ forgot_password_form.hidden_tag() }} {{ forgot_password_form.hidden_tag() }}
{{ render_field_with_errors(forgot_password_form.email) }} {{ render_field_with_errors(forgot_password_form.email) }}
{{ render_field(forgot_password_form.submit) }} {{ render_field_with_errors(forgot_password_form.submit) }}
</form> </form>
{% endblock %} {% endblock %}

View File

@ -9,7 +9,7 @@
{{ render_field_with_errors(login_user_form.password) }} {{ render_field_with_errors(login_user_form.password) }}
{{ render_field_with_errors(login_user_form.remember) }} {{ render_field_with_errors(login_user_form.remember) }}
{{ render_field(login_user_form.next) }} {{ render_field(login_user_form.next) }}
{{ render_field(login_user_form.submit) }} {{ render_field_with_errors(login_user_form.submit) }}
{% if security.recoverable %} {% if security.recoverable %}
<a href="{{ url_for_security('forgot_password') }}">Forgot password</a> <a href="{{ url_for_security('forgot_password') }}">Forgot password</a>
{% endif %} {% endif %}

View File

@ -10,6 +10,6 @@
{% if register_user_form.password_confirm %} {% if register_user_form.password_confirm %}
{{ render_field_with_errors(register_user_form.password_confirm) }} {{ render_field_with_errors(register_user_form.password_confirm) }}
{% endif %} {% endif %}
{{ render_field(register_user_form.submit) }} {{ render_field_with_errors(register_user_form.submit) }}
</form> </form>
{% endblock %} {% endblock %}

View File

@ -1,10 +1,11 @@
{% extends "layout.html" %} {% extends "layout.html" %}
{% block body %} {% block body %}
<h3>Select Vehicle</h3>
<form class='form-horizontal' method="POST"> <form class='form-horizontal' method="POST">
{{ form.hidden_tag() }} {{ form.hidden_tag() }}
{{ render_field_with_errors(form.vehicle) }} {{ render_field_with_errors(form.vehicle) }}
<input type="submit" value="Go"> {{ render_field_with_errors(form.submit) }}
</form> </form>
{% endblock %} {% endblock %}