diff --git a/app/main.py b/app/main.py index c4aa102..d5fc88a 100644 --- a/app/main.py +++ b/app/main.py @@ -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 diff --git a/app/templates/layout.html b/app/templates/layout.html index b26e23f..a71985e 100644 --- a/app/templates/layout.html +++ b/app/templates/layout.html @@ -12,11 +12,11 @@
{{ field(**kwargs)|safe }} {% if field.errors %} - +

{% endif %}
diff --git a/app/templates/newPitStopForm.html b/app/templates/newPitStopForm.html index 47ff31d..7cb043b 100644 --- a/app/templates/newPitStopForm.html +++ b/app/templates/newPitStopForm.html @@ -3,28 +3,9 @@ {% block body %}
{{ form.hidden_tag() }} - -
- -
- {{ form.date(size=20) }} -
-
- -
- -
- {{ form.odometer(size=20) }} -
-
- -
- -
- {{ form.litres(size=20) }} -
-
- + {{ render_field(form.date) }} + {{ render_field(form.odometer) }} + {{ render_field(form.litres) }}