adds edit feature for last pitstop
Adds the possibility to edit the last pitstop in a dedicated form.
This commit is contained in:
parent
6f7716abc6
commit
9d41c39ae7
@ -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/<int:pid>', 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():
|
||||
|
@ -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
|
||||
|
||||
|
||||
|
29
app/rollerverbrauch/templates/editPitStopForm.html
Normal file
29
app/rollerverbrauch/templates/editPitStopForm.html
Normal file
@ -0,0 +1,29 @@
|
||||
{% extends "layout.html" %}
|
||||
|
||||
{% block body %}
|
||||
<div class="col-md-2" ></div>
|
||||
<div class="col-md-8">
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-body">
|
||||
<h3>Edit Pitstop for '{{ vehicle.name }}'</h3>
|
||||
<form class='form-horizontal' method="POST">
|
||||
{{ form.hidden_tag() }}
|
||||
{{ render_field_with_errors(form.date) }}
|
||||
<span id="{{form.date.id}}_help" class="help-block">
|
||||
{{messages['date']}}
|
||||
</span>
|
||||
{{ render_field_with_errors(form.odometer) }}
|
||||
<span id="{{form.odometer.id}}_help" class="help-block">
|
||||
{{messages['odometer']}}
|
||||
</span>
|
||||
{{ render_field_with_errors(form.litres) }}
|
||||
{{ render_field_with_errors(form.costs) }}
|
||||
<span id="{{form.costs.id}}_help" class="help-block">
|
||||
{{messages['costs']}}
|
||||
</span>
|
||||
{{ render_field_with_errors(form.submit) }}
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
@ -61,6 +61,9 @@
|
||||
{% if loop.first %}
|
||||
<tr class='pitstop'>
|
||||
<td colspan='4'>
|
||||
<a href="{{ url_for('edit_pit_stop_form', pid=pitstop.id) }}" class="btn btn-primary">
|
||||
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> edit
|
||||
</a>
|
||||
<a href="{{ url_for('delete_pit_stop_form', pid=pitstop.id) }}" class="btn btn-primary btn-warning ">
|
||||
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span> delete
|
||||
</a>
|
||||
|
Loading…
Reference in New Issue
Block a user