Merge branch 'issue_3_last_stop_editable' into 'development'

Issue 3 last stop editable



See merge request !15
This commit is contained in:
Joachim Lusiardi 2016-06-24 07:17:00 +02:00
commit 56757f29a1
5 changed files with 164 additions and 1 deletions

View File

@ -29,7 +29,9 @@ from rollerverbrauch.forms import \
EditVehicleForm, \ EditVehicleForm, \
DeleteVehicleForm, \ DeleteVehicleForm, \
SelectVehicleForm, \ SelectVehicleForm, \
DeleteAccountForm DeleteAccountForm, \
DeletePitStopForm, \
EditPitstopForm
from rollerverbrauch.entities import \ from rollerverbrauch.entities import \
User, \ User, \
@ -208,6 +210,68 @@ def create_pit_stop_form(vid):
return render_template('newPitStopForm.html', form=form, vehicle=vehicle, messages = messages) return render_template('newPitStopForm.html', form=form, vehicle=vehicle, messages = messages)
@app.route('/pitstops/delete/<int:pid>', 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/<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']) @app.route('/pitstops', methods=['GET'])
@login_required @login_required
def get_pit_stops(): def get_pit_stops():

View File

@ -26,6 +26,11 @@ def costs_check(form, field):
raise ValidationError('Costs must be above 0.01 €.') 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): class SelectVehicleForm(Form):
vehicle = SelectField('Vehicle', coerce=int) vehicle = SelectField('Vehicle', coerce=int)
submit = SubmitField(label='Do it!') submit = SubmitField(label='Do it!')
@ -54,3 +59,21 @@ class DeleteVehicleForm(Form):
class DeleteAccountForm(Form): class DeleteAccountForm(Form):
submit = SubmitField(label='Really delete my account!') 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

View File

@ -0,0 +1,35 @@
{% 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>Delete pitstop?</h3>
<table style='width: 100%'>
<tr>
<th style='text-align:right'>Date of Pitstop</th>
<td style='text-align: left'>{{ pitstop.date }}</td>
</tr>
<tr>
<th style='text-align:right'>Odometer</th>
<td style='text-align: left'>{{ pitstop.odometer }} km</td>
</tr>
<tr>
<th style='text-align:right'>Litres</th>
<td style='text-align: left'>{{ pitstop.litres }} l</td>
</tr>
<tr>
<th style='text-align:right'>Costs (overall)</th>
<td style='text-align: left'>{{ pitstop.costs }} €</td>
</tr>
</table>
<form class='form-horizontal' method='POST'>
{{ form.hidden_tag() }}
{{ render_field_with_errors(form.submit) }}
</form>
</div>
</div>
</div>
<div class='col-md-2' ></div>
{% endblock %}

View 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 %}

View File

@ -58,6 +58,18 @@
{{ (pitstop.costs / pitstop.litres) | round(2) }} €/l {{ (pitstop.costs / pitstop.litres) | round(2) }} €/l
</td> </td>
</tr> </tr>
{% 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>
</td>
</tr>
{% endif %}
{% else %} {% else %}
<tr class='pitstop'> <tr class='pitstop'>
<td> <td>