Little Improvements

Help text for create pitstops form
More precise error messages for create pitstops form
Fixed bu in statistics page when only one pitstop was present
This commit is contained in:
Joachim Lusiardi 2016-05-03 22:21:57 +02:00
parent 471f1415d9
commit e1e5fe7725
5 changed files with 29 additions and 17 deletions

View File

@ -9,7 +9,6 @@ from flask.ext.security import Security, SQLAlchemyUserDatastore, \
from flask.ext.security import user_registered from flask.ext.security import user_registered
from flask_security.core import current_user from flask_security.core import current_user
from flask_sqlalchemy import SQLAlchemy from flask_sqlalchemy import SQLAlchemy
import logging
app = Flask(__name__) app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////data/rollerverbrauch.db' app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////data/rollerverbrauch.db'
@ -162,7 +161,7 @@ def create_pit_stop_form(vid):
if len(vehicle.pitstops) > 0: if len(vehicle.pitstops) > 0:
last_pitstop = vehicle.pitstops[-1] last_pitstop = vehicle.pitstops[-1]
else: else:
last_pitstop = Pitstop(0, 0, date.today()) last_pitstop = Pitstop(0, 0, date(1970, 1, 1))
form = CreatePitstopForm() form = CreatePitstopForm()
form.set_pitstop(last_pitstop) form.set_pitstop(last_pitstop)
@ -179,7 +178,11 @@ def create_pit_stop_form(vid):
form.litres.default = last_pitstop.litres form.litres.default = last_pitstop.litres
form.date.default = date.today() form.date.default = date.today()
form.process() 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']) @app.route('/pitstops', methods=['GET'])

View File

@ -1,16 +1,19 @@
from flask_wtf import Form from flask_wtf import Form
from wtforms import DateField, IntegerField, DecimalField, StringField, SelectField, SubmitField from wtforms import DateField, IntegerField, DecimalField, StringField, SelectField, SubmitField
from wtforms.validators import ValidationError, Length from wtforms.validators import ValidationError, Length
from datetime import date
def date_check(form, field): def date_check(form, field):
if field.data < form.pitstop.date: if field.data < form.last_pitstop.date:
raise ValidationError('The new date must after %s' % form.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): def odometer_check(form, field):
if field.data <= form.pitstop.odometer: if field.data <= form.last_pitstop.odometer:
raise ValidationError('The new odometer value must be higher than %i km' % form.pitstop.odometer) raise ValidationError('The new odometer value must be higher than %i km' % form.last_pitstop.odometer)
def litres_check(form, field): def litres_check(form, field):
@ -28,10 +31,10 @@ class CreatePitstopForm(Form):
odometer = IntegerField('Odometer (km)', validators=[odometer_check]) odometer = IntegerField('Odometer (km)', validators=[odometer_check])
litres = DecimalField('Litres (l)', places=2, validators=[litres_check]) litres = DecimalField('Litres (l)', places=2, validators=[litres_check])
submit = SubmitField(label='Do it!') submit = SubmitField(label='Do it!')
pitstop = None last_pitstop = None
def set_pitstop(self, pitstop): def set_pitstop(self, last_pitstop):
self.pitstop = pitstop self.last_pitstop = last_pitstop
class EditVehicleForm(Form): class EditVehicleForm(Form):

View File

@ -31,17 +31,17 @@
{% endfor %} {% endfor %}
</select> </select>
{% elif field.type == 'BooleanField' %} {% elif field.type == 'BooleanField' %}
<input class="form-control" type="checkbox" id="{{ field.id }}" name="{{ field.id }}" value="{{ field.default|none_filter }}" /> <input class="form-control" type="checkbox" id="{{ field.id }}" name="{{ field.id }}" value="{{ field.default|none_filter }}" aria-describedby="{{ field.id }}_help" />
{% elif field.type == 'StringField' %} {% elif field.type == 'StringField' %}
<input class="form-control" type="text" id="{{ field.id }}" name="{{ field.id }}" value="{{ field.default|none_filter }}" /> <input class="form-control" type="text" id="{{ field.id }}" name="{{ field.id }}" value="{{ field.default|none_filter }}" aria-describedby="{{ field.id }}_help" />
{% elif field.type == 'PasswordField' %} {% elif field.type == 'PasswordField' %}
<input class="form-control" type="password" id="{{ field.id }}" name="{{ field.id }}" value="{{ field.default|none_filter }}" /> <input class="form-control" type="password" id="{{ field.id }}" name="{{ field.id }}" value="{{ field.default|none_filter }}" aria-describedby="{{ field.id }}_help" />
{% elif field.type == 'DateField' %} {% elif field.type == 'DateField' %}
<input class="form-control" type="date" id="{{ field.id }}" name="{{ field.id }}" value="{{ field.default|none_filter }}" /> <input class="form-control" type="date" id="{{ field.id }}" name="{{ field.id }}" value="{{ field.default|none_filter }}" aria-describedby="{{ field.id }}_help" />
{% elif field.type == 'IntegerField' %} {% elif field.type == 'IntegerField' %}
<input class="form-control" type="number" id="{{ field.id }}" name="{{ field.id }}" value="{{ field.default|none_filter }}" step="1" /> <input class="form-control" type="number" id="{{ field.id }}" name="{{ field.id }}" value="{{ field.default|none_filter }}" step="1" aria-describedby="{{ field.id }}_help" />
{% elif field.type == 'DecimalField' %} {% 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}}" /> <input class="form-control" type="number" id="{{ field.id }}" name="{{ field.id }}" value="{{ field.default|none_filter }}" step="{{ 1 / 10 ** field.places}}" aria-describedby="{{ field.id }}_help" />
{% else %} {% else %}
{{ field(**kwargs)|safe }} {{ field(**kwargs)|safe }}
{% endif %} {% endif %}

View File

@ -5,7 +5,13 @@
<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) }}
<span id="{{form.date.id}}_help" class="help-block">
{{messages['date']}}
</span>
{{ render_field_with_errors(form.odometer) }} {{ render_field_with_errors(form.odometer) }}
<span id="{{form.odometer.id}}_help" class="help-block">
{{messages['odometer']}}
</span>
{{ render_field_with_errors(form.litres) }} {{ render_field_with_errors(form.litres) }}
{{ render_field_with_errors(form.submit) }} {{ render_field_with_errors(form.submit) }}
</form> </form>

View File

@ -82,7 +82,7 @@
{% endif %} {% endif %}
</div> </div>
<div class="tab-pane " id="v{{vehicle.id}}_c3"> <div class="tab-pane " id="v{{vehicle.id}}_c3">
{% if vehicle.pitstop_count > 0 %} {% if vehicle.pitstop_count > 1 %}
<div id="averageUsageDiv{{vehicle.id}}" style="width:100%; height:500px;"></div> <div id="averageUsageDiv{{vehicle.id}}" style="width:100%; height:500px;"></div>
<script type="text/javascript"> <script type="text/javascript">
{{ chartScript('averageUsageDiv'+vehicle.id|str, vehicle.average_litres, 'l/100 km') }} {{ chartScript('averageUsageDiv'+vehicle.id|str, vehicle.average_litres, 'l/100 km') }}