diff --git a/app/rollerverbrauch/__init__.py b/app/rollerverbrauch/__init__.py index 664e15d..9828fbe 100644 --- a/app/rollerverbrauch/__init__.py +++ b/app/rollerverbrauch/__init__.py @@ -9,7 +9,6 @@ from flask.ext.security import Security, SQLAlchemyUserDatastore, \ from flask.ext.security import user_registered from flask_security.core import current_user from flask_sqlalchemy import SQLAlchemy -import logging app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////data/rollerverbrauch.db' @@ -162,7 +161,7 @@ def create_pit_stop_form(vid): if len(vehicle.pitstops) > 0: last_pitstop = vehicle.pitstops[-1] else: - last_pitstop = Pitstop(0, 0, date.today()) + last_pitstop = Pitstop(0, 0, date(1970, 1, 1)) form = CreatePitstopForm() form.set_pitstop(last_pitstop) @@ -179,7 +178,11 @@ def create_pit_stop_form(vid): form.litres.default = last_pitstop.litres form.date.default = date.today() form.process() - return render_template('newPitStopForm.html', form=form, vehicle=vehicle) + messages = { + 'date': 'Date must be between %s and %s (including).' % (str(last_pitstop.date), str(date.today())), + 'odometer': 'Odometer must be greater than %s km.' % (str(last_pitstop.odometer)) + } + return render_template('newPitStopForm.html', form=form, vehicle=vehicle, messages = messages) @app.route('/pitstops', methods=['GET']) diff --git a/app/rollerverbrauch/forms.py b/app/rollerverbrauch/forms.py index 2f2b87f..7e5bd3c 100644 --- a/app/rollerverbrauch/forms.py +++ b/app/rollerverbrauch/forms.py @@ -1,16 +1,19 @@ from flask_wtf import Form from wtforms import DateField, IntegerField, DecimalField, StringField, SelectField, SubmitField from wtforms.validators import ValidationError, Length +from datetime import date def date_check(form, field): - if field.data < form.pitstop.date: - raise ValidationError('The new date must after %s' % form.pitstop.date) + if field.data < form.last_pitstop.date: + raise ValidationError('The new date must not be before %s' % form.last_pitstop.date) + if field.data > date.today(): + raise ValidationError('The new date must not be after %s' % date.today()) 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) + if field.data <= form.last_pitstop.odometer: + raise ValidationError('The new odometer value must be higher than %i km' % form.last_pitstop.odometer) def litres_check(form, field): @@ -28,10 +31,10 @@ class CreatePitstopForm(Form): odometer = IntegerField('Odometer (km)', validators=[odometer_check]) litres = DecimalField('Litres (l)', places=2, validators=[litres_check]) submit = SubmitField(label='Do it!') - pitstop = None + last_pitstop = None - def set_pitstop(self, pitstop): - self.pitstop = pitstop + def set_pitstop(self, last_pitstop): + self.last_pitstop = last_pitstop class EditVehicleForm(Form): diff --git a/app/rollerverbrauch/templates/layout.html b/app/rollerverbrauch/templates/layout.html index 8b7e982..6e888d9 100644 --- a/app/rollerverbrauch/templates/layout.html +++ b/app/rollerverbrauch/templates/layout.html @@ -31,17 +31,17 @@ {% endfor %} {% elif field.type == 'BooleanField' %} - + {% elif field.type == 'StringField' %} - + {% elif field.type == 'PasswordField' %} - + {% elif field.type == 'DateField' %} - + {% elif field.type == 'IntegerField' %} - + {% elif field.type == 'DecimalField' %} - + {% else %} {{ field(**kwargs)|safe }} {% endif %} diff --git a/app/rollerverbrauch/templates/newPitStopForm.html b/app/rollerverbrauch/templates/newPitStopForm.html index 849432e..df25716 100644 --- a/app/rollerverbrauch/templates/newPitStopForm.html +++ b/app/rollerverbrauch/templates/newPitStopForm.html @@ -5,7 +5,13 @@
{{ form.hidden_tag() }} {{ render_field_with_errors(form.date) }} + + {{messages['date']}} + {{ render_field_with_errors(form.odometer) }} + + {{messages['odometer']}} + {{ render_field_with_errors(form.litres) }} {{ render_field_with_errors(form.submit) }}
diff --git a/app/rollerverbrauch/templates/statistics.html b/app/rollerverbrauch/templates/statistics.html index acbb91e..5e8f245 100644 --- a/app/rollerverbrauch/templates/statistics.html +++ b/app/rollerverbrauch/templates/statistics.html @@ -82,7 +82,7 @@ {% endif %}
- {% if vehicle.pitstop_count > 0 %} + {% if vehicle.pitstop_count > 1 %}