From af5d4ae6b3814dcc0aa76e1e1da4ccc0b5325a72 Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Wed, 29 Jun 2016 22:50:01 +0200 Subject: [PATCH] cann add pitstops for selected vehicles / consumables --- app/rollerverbrauch/__init__.py | 54 ++++++++++++++----- app/rollerverbrauch/forms.py | 5 ++ app/rollerverbrauch/templates/pitstops.html | 14 ++++- .../templates/selectConsumableForVehicle.html | 19 +++++++ app/rollerverbrauch/templates/statistics.html | 10 ++-- 5 files changed, 82 insertions(+), 20 deletions(-) create mode 100644 app/rollerverbrauch/templates/selectConsumableForVehicle.html diff --git a/app/rollerverbrauch/__init__.py b/app/rollerverbrauch/__init__.py index 57fd72a..c79ce53 100644 --- a/app/rollerverbrauch/__init__.py +++ b/app/rollerverbrauch/__init__.py @@ -35,7 +35,8 @@ from rollerverbrauch.forms import \ EditPitstopForm, \ CreateConsumableForm, \ EditConsumableForm, \ - DeletConsumableForm + DeletConsumableForm, \ + SelectConsumableForm from rollerverbrauch.entities import \ User, \ @@ -210,29 +211,53 @@ def create_vehicle(): return render_template('createVehicleForm.html', form=form) -@app.route('/pitstops/select_vehicle', methods=['GET', 'POST']) +@app.route('/pitstops/vehicle/select', methods=['GET', 'POST']) @login_required def select_vehicle_for_new_pitstop(): + if len(current_user.vehicles) == 1: + return redirect(url_for('select_consumable_for_new_pitstop', vid=current_user.vehicles[0].id)) + form = SelectVehicleForm() form.vehicle.choices = [(g.id, g.name) for g in current_user.vehicles] if form.validate_on_submit(): - vehicle = Vehicle.query.filter(Vehicle.id == form.vehicle.data).first() - if vehicle not in current_user.vehicles: - return render_template('selectVehicle.html', form=form) - - return redirect(url_for('create_pit_stop_form', vid=form.vehicle.data)) + return redirect(url_for('select_consumable_for_new_pitstop', vid=form.vehicle.data)) return render_template('selectVehicle.html', form=form) -@app.route('/pitstops/create/', methods=['GET', 'POST']) +@app.route('/pitstops/vehicle//consumable/select', methods=['GET', 'POST']) @login_required -def create_pit_stop_form(vid): - vehicle = Vehicle.query.filter(Vehicle.id == vid).first() - if vehicle not in current_user.vehicles: +def select_consumable_for_new_pitstop(vid): + vehicle = Vehicle.query.get(vid) + if vehicle is None or vehicle not in current_user.vehicles: return redirect(url_for('select_vehicle_for_new_pitstop')) + if len(vehicle.consumables) == 1: + return redirect(url_for('create_pit_stop_form', vid=vid, cid=vehicle.consumables[0].id)) + + form = SelectConsumableForm() + form.consumable.choices = [(g.id, g.name) for g in vehicle.consumables] + + if form.validate_on_submit(): + return redirect(url_for('create_pit_stop_form', vid=vid, cid=form.consumable.data)) + + return render_template('selectConsumableForVehicle.html', vehicle=vehicle, form=form) + + +@app.route('/pitstops/vehicle//consumable//create', methods=['GET', 'POST']) +@login_required +def create_pit_stop_form(vid, cid): + vehicle = Vehicle.query.get(vid) + if vehicle is None or vehicle not in current_user.vehicles: + return redirect(url_for('select_vehicle_for_new_pitstop')) + + consumable = Consumable.query.get(cid) + if consumable not in vehicle.consumables: + return redirect(url_for('select_consumable_for_new_pitstop', vid=vid)) + + # the last pitstop is required to be able to check the monotonicy of date and odometer + if len(vehicle.pitstops) > 0: last_pitstop = vehicle.pitstops[-1] else: @@ -240,9 +265,10 @@ def create_pit_stop_form(vid): form = CreatePitstopForm() form.set_pitstop(last_pitstop) + form.litres.label = '%s (%s)' % (consumable.name, consumable.unit) if form.validate_on_submit(): - new_stop = Pitstop(form.odometer.data, form.litres.data, form.date.data, form.costs.data) + new_stop = Pitstop(form.odometer.data, form.litres.data, form.date.data, form.costs.data, cid) db.session.add(new_stop) vehicle.pitstops.append(new_stop) db.session.commit() @@ -250,9 +276,9 @@ def create_pit_stop_form(vid): return redirect(url_for('get_pit_stops', _anchor= 'v' + str(vehicle.id))) form.odometer.default = last_pitstop.odometer - form.litres.default = last_pitstop.litres +# form.litres.default = last_pitstop.litres form.date.default = date.today() - form.costs.default = last_pitstop.costs +# form.costs.default = last_pitstop.costs form.process() messages = { 'date': 'Date must be between %s and %s (including).' % (str(last_pitstop.date), str(date.today())), diff --git a/app/rollerverbrauch/forms.py b/app/rollerverbrauch/forms.py index ffc33ac..4bab1b5 100644 --- a/app/rollerverbrauch/forms.py +++ b/app/rollerverbrauch/forms.py @@ -37,6 +37,11 @@ class SelectVehicleForm(Form): submit = SubmitField(label='Do it!') +class SelectConsumableForm(Form): + consumable = SelectField('Consumable', coerce=int) + submit = SubmitField(label='Do it!') + + class CreatePitstopForm(Form): date = DateField('Date of Pitstop', validators=[date_check]) odometer = IntegerField('Odometer (km)', validators=[odometer_check]) diff --git a/app/rollerverbrauch/templates/pitstops.html b/app/rollerverbrauch/templates/pitstops.html index f45c131..7a3e773 100644 --- a/app/rollerverbrauch/templates/pitstops.html +++ b/app/rollerverbrauch/templates/pitstops.html @@ -89,13 +89,25 @@ {{ (pitstop.costs / pitstop.litres) | round(2) }} €/l + {% if loop.first %} + + + + edit + + + delete + + + + {% endif %} {% endif %} {% endfor %} {% else %} {% endif %} diff --git a/app/rollerverbrauch/templates/selectConsumableForVehicle.html b/app/rollerverbrauch/templates/selectConsumableForVehicle.html new file mode 100644 index 0000000..9676e2a --- /dev/null +++ b/app/rollerverbrauch/templates/selectConsumableForVehicle.html @@ -0,0 +1,19 @@ +{% extends "layout.html" %} + +{% block body %} +
+
+
+
+

Select Consumable for '{{ vehicle.name }}'

+
+ {{ form.hidden_tag() }} + {{ render_field_with_errors(form.consumable) }} + {{ render_field_with_errors(form.submit) }} +
+
+
+
+
+{% endblock %} + diff --git a/app/rollerverbrauch/templates/statistics.html b/app/rollerverbrauch/templates/statistics.html index c66622b..85cb81c 100644 --- a/app/rollerverbrauch/templates/statistics.html +++ b/app/rollerverbrauch/templates/statistics.html @@ -87,7 +87,7 @@ {% else %} {% endif %} @@ -99,7 +99,7 @@ {% else %} {% endif %} @@ -111,7 +111,7 @@ {% else %} {% endif %} @@ -123,7 +123,7 @@ {% else %} {% endif %} @@ -135,7 +135,7 @@ {% else %} {% endif %}