Changed to WTForms including validation

This commit is contained in:
Joachim Lusiardi 2016-04-18 07:25:40 +02:00
parent 6f218e591f
commit 864b7822ac
3 changed files with 36 additions and 35 deletions

View File

@ -7,12 +7,10 @@ from flask import url_for
from flask_sqlalchemy import SQLAlchemy
import uuid
import hashlib
import time
from functools import wraps
from flask_wtf import Form
from wtforms import DateField, IntegerField, DecimalField
from wtforms.validators import DataRequired
from wtforms.validators import DataRequired, ValidationError
app = Flask(__name__)
DATABASE = '/data/rollerverbrauch.db'
@ -110,26 +108,48 @@ def teardown_request(exception):
def index():
return redirect(url_for('get_pit_stops'))
def date_check(form, field):
if field.data < form.pitstop.date:
raise ValidationError('The new date must after %s' % form.pitstop.date)
def odometer_check(form, field):
if field.data <= form.pitstop.odometer:
raise ValidationError('The new odometer value must be higher than %i km' % form.pitstop.odometer)
def litres_check(form, field):
if field.data is not None and field.data <= 0:
raise ValidationError('You must fuel at least 0.1 l')
class CreatePitstopForm(Form):
date = DateField('Date of Pitstop')
odometer = IntegerField('Odometer (km)')
litres = DecimalField('Litres (l)', places=1)
date = DateField('Date of Pitstop', validators=[date_check])
odometer = IntegerField('Odometer (km)', validators=[odometer_check])
litres = DecimalField('Litres (l)', places=1, validators=[litres_check])
pitstop = None
def set_pitstop(self, pitstop):
self.pitstop = pitstop
@app.route('/pitstops/createForm', methods=['GET', 'POST'])
@requires_auth
def create_pit_stop_form():
last_pitstop = Pitstop.query.order_by(Pitstop.id.desc()).first()
if last_pitstop is None:
last_pitstop = Pitstop(0, 0, datetime.now())
form = CreatePitstopForm()
form.set_pitstop(last_pitstop)
if form.validate_on_submit():
new_stop = Pitstop(form.odometer.data, form.litres.data, form.date.data)
sqldb.session.add(new_stop)
sqldb.session.commit()
return redirect(url_for('get_pit_stops'))
last_pitstop = Pitstop.query.order_by(Pitstop.date.desc()).first()
if last_pitstop is None:
last_pitstop = Pitstop(0, 0, None)
# dynamically set values
form.odometer.default = last_pitstop.odometer
form.litres.default = last_pitstop.litres

View File

@ -12,11 +12,11 @@
<div class="col-sm-10">
{{ field(**kwargs)|safe }}
{% if field.errors %}
<ul class=errors>
<p class='error'>
{% for error in field.errors %}
<li>{{ error }}</li>
{{ error }}
{% endfor %}
</ul>
</p>
{% endif %}
</div>
</div>

View File

@ -3,28 +3,9 @@
{% block body %}
<form class='form-horizontal' method="POST">
{{ form.hidden_tag() }}
<div class="form-group">
<label class="col-sm-2 control-label" for="date">{{ form.date.label }}</label>
<div class="col-sm-10">
{{ form.date(size=20) }}
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="odometer">{{ form.odometer.label }}</label>
<div class="col-sm-10">
{{ form.odometer(size=20) }}
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="litres">{{ form.litres.label }}</label>
<div class="col-sm-10">
{{ form.litres(size=20) }}
</div>
</div>
{{ render_field(form.date) }}
{{ render_field(form.odometer) }}
{{ render_field(form.litres) }}
<input type="submit" value="Go">
</form>