diff --git a/app/rollerverbrauch/__init__.py b/app/rollerverbrauch/__init__.py index b37ac1c..a815566 100644 --- a/app/rollerverbrauch/__init__.py +++ b/app/rollerverbrauch/__init__.py @@ -29,7 +29,9 @@ from rollerverbrauch.forms import \ EditVehicleForm, \ DeleteVehicleForm, \ SelectVehicleForm, \ - DeleteAccountForm + DeleteAccountForm, \ + DeletePitStopForm, \ + EditPitstopForm from rollerverbrauch.entities import \ User, \ @@ -208,6 +210,68 @@ def create_pit_stop_form(vid): return render_template('newPitStopForm.html', form=form, vehicle=vehicle, messages = messages) +@app.route('/pitstops/delete/', methods=['GET', 'POST']) +@login_required +def delete_pit_stop_form(pid): + pitstop = Pitstop.query.filter(Pitstop.id == pid).first() + if pitstop is None: + return redirect(url_for('get_pit_stops')) + vehicle = Vehicle.query.filter(Vehicle.id == pitstop.vehicle_id).first() + if vehicle not in current_user.vehicles: + return redirect(url_for('get_pit_stops')) + + form = DeletePitStopForm() + if form.validate_on_submit(): + db.session.delete(pitstop) + db.session.commit() + tools.db_log_delete(pitstop) + return redirect(url_for('get_pit_stops', _anchor='v' + str(vehicle.id))) + + return render_template('deletePitstopForm.html', form=form, pitstop=pitstop ) + + +@app.route('/pitstops/edit/', methods=['GET', 'POST']) +@login_required +def edit_pit_stop_form(pid): + edit_pitstop = Pitstop.query.filter(Pitstop.id == pid).first() + if edit_pitstop is None: + return redirect(url_for('get_pit_stops')) + vehicle = Vehicle.query.filter(Vehicle.id == edit_pitstop.vehicle_id).first() + if vehicle not in current_user.vehicles: + return redirect(url_for('get_pit_stops')) + + last_pitstop_pos = vehicle.pitstops.index(edit_pitstop) - 1 + if last_pitstop_pos > 0: + last_pitstop = vehicle.pitstops[last_pitstop_pos] + else: + last_pitstop = Pitstop(0, 0, date(1970, 1, 1)) + + form = EditPitstopForm() + form.set_pitstop(last_pitstop) + + if form.validate_on_submit(): + edit_pitstop.costs = form.costs.data + edit_pitstop.date = form.date.data + edit_pitstop.litres = form.litres.data + edit_pitstop.odometer = form.odometer.data + db.session.commit() + tools.db_log_update(edit_pitstop) + return redirect(url_for('get_pit_stops', _anchor='v' + str(vehicle.id))) + + form.odometer.default = edit_pitstop.odometer + form.litres.default = edit_pitstop.litres + form.date.default = edit_pitstop.date + form.costs.default = edit_pitstop.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_pitstop.costs > 0: + messages['costs'] = 'Costs must be higher than 0.01 €.' + return render_template('editPitStopForm.html', form=form, vehicle=vehicle, messages=messages) + + @app.route('/pitstops', methods=['GET']) @login_required def get_pit_stops(): diff --git a/app/rollerverbrauch/forms.py b/app/rollerverbrauch/forms.py index 560de7b..c6bb7c2 100644 --- a/app/rollerverbrauch/forms.py +++ b/app/rollerverbrauch/forms.py @@ -26,6 +26,11 @@ def costs_check(form, field): raise ValidationError('Costs must be above 0.01 €.') +def edit_costs_check(form, field): + costs_check_required = (form.costs.default is not None and form.costs.default > 0) + if costs_check_required and field.data is not None and field.data <= 0: + raise ValidationError('Costs must be above 0.01 €.') + class SelectVehicleForm(Form): vehicle = SelectField('Vehicle', coerce=int) submit = SubmitField(label='Do it!') @@ -54,3 +59,21 @@ class DeleteVehicleForm(Form): class DeleteAccountForm(Form): submit = SubmitField(label='Really delete my account!') + + +class DeletePitStopForm(Form): + submit = SubmitField(label='Really delete this pitstop!') + + +class EditPitstopForm(Form): + date = DateField('Date of Pitstop', validators=[date_check]) + odometer = IntegerField('Odometer (km)', validators=[odometer_check]) + litres = DecimalField('Litres (l)', places=2, validators=[litres_check]) + costs = DecimalField('Costs (€, overall)', places=2, validators=[edit_costs_check]) + submit = SubmitField(label='Update it!') + last_pitstop = None + + def set_pitstop(self, last_pitstop): + self.last_pitstop = last_pitstop + + diff --git a/app/rollerverbrauch/templates/deletePitstopForm.html b/app/rollerverbrauch/templates/deletePitstopForm.html new file mode 100644 index 0000000..fa14203 --- /dev/null +++ b/app/rollerverbrauch/templates/deletePitstopForm.html @@ -0,0 +1,35 @@ +{% extends 'layout.html' %} + +{% block body %} +
+
+
+
+

Delete pitstop?

+ + + + + + + + + + + + + + + + + +
Date of Pitstop{{ pitstop.date }}
Odometer{{ pitstop.odometer }} km
Litres{{ pitstop.litres }} l
Costs (overall){{ pitstop.costs }} €
+
+ {{ form.hidden_tag() }} + {{ render_field_with_errors(form.submit) }} +
+
+
+
+
+{% endblock %} diff --git a/app/rollerverbrauch/templates/editPitStopForm.html b/app/rollerverbrauch/templates/editPitStopForm.html new file mode 100644 index 0000000..3761c34 --- /dev/null +++ b/app/rollerverbrauch/templates/editPitStopForm.html @@ -0,0 +1,29 @@ +{% extends "layout.html" %} + +{% block body %} +
+
+
+
+

Edit Pitstop for '{{ vehicle.name }}'

+
+ {{ 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.costs) }} + + {{messages['costs']}} + + {{ render_field_with_errors(form.submit) }} +
+
+
+
+{% endblock %} diff --git a/app/rollerverbrauch/templates/pitstops.html b/app/rollerverbrauch/templates/pitstops.html index 157df6e..f45c131 100644 --- a/app/rollerverbrauch/templates/pitstops.html +++ b/app/rollerverbrauch/templates/pitstops.html @@ -58,6 +58,18 @@ {{ (pitstop.costs / pitstop.litres) | round(2) }} €/l + {% if loop.first %} + + + + edit + + + delete + + + + {% endif %} {% else %}