diff --git a/app/rollerverbrauch/__init__.py b/app/rollerverbrauch/__init__.py index 54670c0..a815566 100644 --- a/app/rollerverbrauch/__init__.py +++ b/app/rollerverbrauch/__init__.py @@ -30,7 +30,8 @@ from rollerverbrauch.forms import \ DeleteVehicleForm, \ SelectVehicleForm, \ DeleteAccountForm, \ - DeletePitStopForm + DeletePitStopForm, \ + EditPitstopForm from rollerverbrauch.entities import \ User, \ @@ -229,6 +230,48 @@ def delete_pit_stop_form(pid): 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 520144e..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!') @@ -58,3 +63,17 @@ class DeleteAccountForm(Form): 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/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 6d637df..f45c131 100644 --- a/app/rollerverbrauch/templates/pitstops.html +++ b/app/rollerverbrauch/templates/pitstops.html @@ -61,6 +61,9 @@ {% if loop.first %} + + edit + delete