First step for WTForms

This commit is contained in:
Joachim Lusiardi 2016-04-17 12:50:31 +02:00
parent 808e0d80d3
commit 8c8803874e
4 changed files with 50 additions and 96 deletions

View File

@ -10,6 +10,10 @@ import hashlib
import time
from functools import wraps
from flask_wtf import Form
from wtforms import DateField, IntegerField, DecimalField
from wtforms.validators import DataRequired
app = Flask(__name__)
DATABASE = '/data/rollerverbrauch.db'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'+DATABASE
@ -106,71 +110,32 @@ def teardown_request(exception):
def index():
return redirect(url_for('get_pit_stops'))
class CreatePitstopForm(Form):
date = DateField('Date of Pitstop')
odometer = IntegerField('Odometer (km)')
litres = DecimalField('Litres (l)', places=1)
@app.route('/pitstops', methods=['POST'])
@app.route('/pitstops/createForm', methods=['GET', 'POST'])
@requires_auth
def create_pit_stop():
def create_pit_stop_form():
form = CreatePitstopForm()
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)
error_msg = {}
date_of_pitstop = request.form['date']
try:
date_of_pitstop = datetime.strptime(date_of_pitstop, '%Y-%m-%d').strftime('%Y-%m-%d')
except ValueError:
error_msg['date'] = 'invalid date, only YYYY-MM-DD is allowed'
date_of_pitstop = request.form['date']
odometer = request.form['odometer']
try:
odometer = int(odometer)
except ValueError:
error_msg['odometer'] = 'Illegal Value, only Integers allowed'
odometer = None
if odometer is not None and odometer <= last_pitstop.odometer:
error_msg['odometer'] = 'Illegal Value, new Value must be bigger as given value'
odometer = request.form['odometer']
if odometer is None:
odometer = request.form['odometer']
litres = request.form['litres']
try:
litres = float(litres)
except ValueError:
error_msg['litres'] = 'Illegal Value, only floating point allowed'
litres = None
if litres is not None and litres <= 0:
error_msg['litres'] = 'Litres must not be 0'
litres = request.form['litres']
if litres is None:
litres = request.form['litres']
# error checking here
if len(error_msg) > 0:
data = {'last': {'date': date_of_pitstop, 'odometer': odometer, 'litres': litres}, 'error': error_msg}
return render_template('newPitStopForm.html', data=data)
new_stop = Pitstop(odometer, litres, datetime.strptime(date_of_pitstop, '%Y-%m-%d'))
sqldb.session.add(new_stop)
sqldb.session.commit()
return redirect(url_for('get_pit_stops'))
@app.route('/pitstops/createForm', methods=['GET'])
@requires_auth
def create_pit_stop_form():
last_stop = Pitstop.query.order_by(Pitstop.date.desc()).first()
if last_stop is None:
last_stop = Pitstop(0, 0, date(1970, 1, 1))
values = dict()
values['odometer'] = last_stop.odometer
values['litres'] = last_stop.litres
values['date'] = time.strftime("%Y-%m-%d")
g.data['last'] = values
g.data['error'] = None
return render_template('newPitStopForm.html', data=g.data)
# dynamically set values
form.odometer.default = last_pitstop.odometer
form.litres.default = last_pitstop.litres
form.date.default = datetime.now()
form.process()
return render_template('newPitStopForm.html', form=form)
@app.route('/pitstops', methods=['GET'])
@ -197,7 +162,7 @@ def get_statistics():
average_distance = 0
average_litres_fuelled = 0
average_litres_used = 0
if count > 0:
sum_litres = 0
for pitstop in pitstops:

View File

@ -1,2 +1,3 @@
Flask
Flask-SQLAlchemy
Flask-WTF

View File

@ -69,4 +69,3 @@
</body>
</html>

View File

@ -1,43 +1,32 @@
{% extends "layout.html" %}
{% block body %}
<form class='form-horizontal' method="POST">
{{ form.hidden_tag() }}
<form class='form-horizontal' id='createPitStop' action="{{ url_for('create_pit_stop') }}" method='post'>
<!-- Text input-->
<div class="form-group {% if data.error['date'] %}has-error{% endif %}">
<label class="col-sm-2 control-label" for="date">Date of Pitstop</label>
<div class="col-sm-10">
<input class="form-control" id="date" name="date" placeholder="" required="" type="date" value='{{ data.last.date }}' />
<p class='error'>{{ data.error['date'] }}</p>
</div>
</div>
<!-- Text input-->
<div class="form-group {% if data.error['odometer'] %}has-error{% endif %}">
<label class="col-sm-2 control-label" for="odometer">Odometer (km)</label>
<div class="col-sm-10">
<input class="form-control" id="odometer" name="odometer" placeholder="" type="number" value='{{ data.last.odometer }}' />
<p class='error'>{{ data.error['odometer'] }}</p>
</div>
</div>
<!-- Text input-->
<div class="form-group {% if data.error['litres'] %}has-error{% endif %}">
<label class="col-sm-2 control-label" for="litres">Litres (l)</label>
<div class="col-sm-10">
<input class="form-control" id="litres" name="litres" placeholder="" type="number" step='0.1' value='{{ data.last.litres }}' />
<p class='error'>{{ data.error['litres'] }}</p>
</div>
</div>
<!-- Button (Double) -->
<div class="form-group">
<div class="controls">
<button id="buttonLogId" name="buttonLogId" class="btn btn-success" onclick="document.getElementById('create_pit_stop').submit();">Log Pitstop</button>
<button id="buttonAbortId" name="buttonAbortId" class="btn btn-warning" onclick="window.location.href='{{ url_for('get_pit_stops') }}'" type="button" >Abort</button>
<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>
<input type="submit" value="Go">
</form>
{% endblock %}
{% endblock %}