From 71412bf4878a4bbcea9268f20d0d0f52f630b88e Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Tue, 1 Nov 2016 11:19:26 +0100 Subject: [PATCH] integrate services into rollerverbrauch --- app/__init__.py | 120 ++++++++++++++++++++++++++++++- app/templates/account.html | 6 ++ app/templates/pitstops.html | 138 +++++++++++++++++++++++++----------- 3 files changed, 220 insertions(+), 44 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index c2d0e09..bf23383 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -24,7 +24,10 @@ from .forms import \ CreateConsumableForm, \ EditConsumableForm, \ DeletConsumableForm, \ - SelectConsumableForm + SelectConsumableForm, \ + CreateServiceForm, \ + DeleteServiceForm, \ + EditServiceForm app = Flask(__name__) @@ -38,7 +41,8 @@ from .entities import \ Role, \ Pitstop, \ Vehicle, \ - Consumable + Consumable, \ + Service # required to activate the filters from .filters import * @@ -367,10 +371,120 @@ def edit_pit_stop_form(pid): return render_template('editPitStopForm.html', form=form, vehicle=vehicle, messages=messages) +def pitstop_service_key(x): + return x.odometer + + @app.route('/pitstops', methods=['GET']) @login_required def get_pit_stops(): - return render_template('pitstops.html', user=current_user) + user = { + 'vehicles': [] + } + for vehicle in current_user.vehicles: + data = [] + for pitstop in vehicle.pitstops: + data.append(pitstop) + for service in vehicle.services: + data.append(service) + v = { + 'id': vehicle.id, + 'name': vehicle.name, + 'data': sorted(data, key=pitstop_service_key) + } + user['vehicles'].append(v) + + return render_template('pitstops.html', user=user) + + +@app.route('/service/vehicle//create', methods=['GET', 'POST']) +@login_required +def create_service_for_vehicle(vid): + vehicle = Vehicle.query.get(vid) + if vehicle is None or vehicle not in current_user.vehicles: + return redirect(url_for('get_account_page')) + + form = CreateServiceForm() + last_pitstop = tools.get_latest_pitstop_for_vehicle(vid) + form.set_pitstop(last_pitstop) + form.same_odometer_allowed = True + + form.preinit_with_data() + + if form.validate_on_submit(): + new_service = Service(form.date.data, form.odometer.data, vid, form.costs.data, form.description.data) + db.session.add(new_service) + vehicle.services.append(new_service) + db.session.commit() + print(new_service) + return redirect(url_for('get_account_page')) + + form.process() + return render_template('createServiceForm.html', form=form, vehicle=vehicle, messages=[]) + + +@app.route('/service/delete/', methods=['GET', 'POST']) +@login_required +def delete_service_form(sid): + service = Service.query.filter(Service.id == sid).first() + if service is None: + return redirect(url_for('get_pit_stops')) + vehicle = Vehicle.query.filter(Vehicle.id == service.vehicle_id).first() + if vehicle not in current_user.vehicles: + return redirect(url_for('get_pit_stops')) + + form = DeleteServiceForm() + if form.validate_on_submit(): + db.session.delete(service) + db.session.commit() + tools.db_log_delete(service) + return redirect(url_for('get_pit_stops', _anchor='v' + str(vehicle.id))) + + return render_template('deleteServiceForm.html', form=form, service=service ) + + +@app.route('/service/edit/', methods=['GET', 'POST']) +@login_required +def edit_service_form(sid): + edit_service = Service.query.get(sid) + if edit_service is None: + return redirect(url_for('get_pit_stops')) + + vehicle = Vehicle.query.filter(Vehicle.id == edit_service.vehicle_id).first() + if vehicle not in current_user.vehicles: + return redirect(url_for('get_pit_stops')) + + # last_pitstop_pos = vehicle.pitstops.index(edit_service) - 1 + # if last_pitstop_pos > 0: + # last_pitstop = vehicle.pitstops[last_pitstop_pos] + # else: + # last_pitstop = Pitstop(0, 0, date(1970, 1, 1), 0, 0) + last_pitstop = Pitstop(0, 0, date(1970, 1, 1), 0, 0) + + form = EditServiceForm() + form.set_pitstop(last_pitstop) + + if form.validate_on_submit(): + edit_service.costs = form.costs.data + edit_service.date = form.date.data + edit_service.description = form.description.data + edit_service.odometer = form.odometer.data + db.session.commit() + tools.db_log_update(edit_service) + return redirect(url_for('get_pit_stops', _anchor='v' + str(vehicle.id))) + + form.odometer.default = edit_service.odometer + form.description.default = edit_service.description + form.date.default = edit_service.date + form.costs.default = edit_service.costs + form.process() + 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)) + } + if edit_service.costs is not None and edit_service.costs > 0: + messages['costs'] = 'Costs must be higher than 0.01 €.' + return render_template('editServiceForm.html', form=form, vehicle=vehicle, messages=messages) @app.route('/manual', methods=['GET']) diff --git a/app/templates/account.html b/app/templates/account.html index e408636..01bf40a 100644 --- a/app/templates/account.html +++ b/app/templates/account.html @@ -40,6 +40,12 @@ {{ vehicle.consumables | length }} consumables + + add service + + + add pitstop + edit diff --git a/app/templates/pitstops.html b/app/templates/pitstops.html index 9a4cec5..4a5ba12 100644 --- a/app/templates/pitstops.html +++ b/app/templates/pitstops.html @@ -1,9 +1,95 @@ - {% extends "layout.html" %} +{% extends "layout.html" %} + +{% macro pitstop(field, vindex, loop) -%} +
+
+
+ +
+ + + + + + + + + + + + + + + + + +
Date{{field.date}}
Odometer{{field.odometer}} km
{{ field.consumable.name }}{{field.amount}} {{ field.consumable.unit }}
Costs + {% if field.costs %} + {{field.costs}} € + {% else %} + -- € + {% endif %} +
+ {% if loop.first %} + + edit + + + delete + + {% endif %} +
+
+{%- endmacro %} + +{% macro service(field, vindex, loop) -%} +
+
+
+ +
+ + + + + + + + + + + + + + + + + +
Date{{field.date}}
Odometer{{field.odometer}} km
Description{{field.description}}
Costs + {% if field.costs %} + {{field.costs}} € + {% else %} + -- € + {% endif %} +
+ {% if loop.first %} + + edit + + + delete + + {% endif %} +
+
+{%- endmacro %} + + {% block body %}