Changed to WTForms including validation
This commit is contained in:
parent
6f218e591f
commit
864b7822ac
40
app/main.py
40
app/main.py
|
@ -7,12 +7,10 @@ from flask import url_for
|
||||||
from flask_sqlalchemy import SQLAlchemy
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
import uuid
|
import uuid
|
||||||
import hashlib
|
import hashlib
|
||||||
import time
|
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
|
||||||
from flask_wtf import Form
|
from flask_wtf import Form
|
||||||
from wtforms import DateField, IntegerField, DecimalField
|
from wtforms import DateField, IntegerField, DecimalField
|
||||||
from wtforms.validators import DataRequired
|
from wtforms.validators import DataRequired, ValidationError
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
DATABASE = '/data/rollerverbrauch.db'
|
DATABASE = '/data/rollerverbrauch.db'
|
||||||
|
@ -110,26 +108,48 @@ def teardown_request(exception):
|
||||||
def index():
|
def index():
|
||||||
return redirect(url_for('get_pit_stops'))
|
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):
|
class CreatePitstopForm(Form):
|
||||||
date = DateField('Date of Pitstop')
|
date = DateField('Date of Pitstop', validators=[date_check])
|
||||||
odometer = IntegerField('Odometer (km)')
|
odometer = IntegerField('Odometer (km)', validators=[odometer_check])
|
||||||
litres = DecimalField('Litres (l)', places=1)
|
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'])
|
@app.route('/pitstops/createForm', methods=['GET', 'POST'])
|
||||||
@requires_auth
|
@requires_auth
|
||||||
def create_pit_stop_form():
|
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 = CreatePitstopForm()
|
||||||
|
form.set_pitstop(last_pitstop)
|
||||||
if form.validate_on_submit():
|
if form.validate_on_submit():
|
||||||
|
|
||||||
new_stop = Pitstop(form.odometer.data, form.litres.data, form.date.data)
|
new_stop = Pitstop(form.odometer.data, form.litres.data, form.date.data)
|
||||||
sqldb.session.add(new_stop)
|
sqldb.session.add(new_stop)
|
||||||
sqldb.session.commit()
|
sqldb.session.commit()
|
||||||
return redirect(url_for('get_pit_stops'))
|
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
|
# dynamically set values
|
||||||
form.odometer.default = last_pitstop.odometer
|
form.odometer.default = last_pitstop.odometer
|
||||||
form.litres.default = last_pitstop.litres
|
form.litres.default = last_pitstop.litres
|
||||||
|
|
|
@ -12,11 +12,11 @@
|
||||||
<div class="col-sm-10">
|
<div class="col-sm-10">
|
||||||
{{ field(**kwargs)|safe }}
|
{{ field(**kwargs)|safe }}
|
||||||
{% if field.errors %}
|
{% if field.errors %}
|
||||||
<ul class=errors>
|
<p class='error'>
|
||||||
{% for error in field.errors %}
|
{% for error in field.errors %}
|
||||||
<li>{{ error }}</li>
|
{{ error }}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -3,28 +3,9 @@
|
||||||
{% block body %}
|
{% block body %}
|
||||||
<form class='form-horizontal' method="POST">
|
<form class='form-horizontal' method="POST">
|
||||||
{{ form.hidden_tag() }}
|
{{ form.hidden_tag() }}
|
||||||
|
{{ render_field(form.date) }}
|
||||||
<div class="form-group">
|
{{ render_field(form.odometer) }}
|
||||||
<label class="col-sm-2 control-label" for="date">{{ form.date.label }}</label>
|
{{ render_field(form.litres) }}
|
||||||
<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>
|
|
||||||
|
|
||||||
<input type="submit" value="Go">
|
<input type="submit" value="Go">
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue