Fixed a bug with editing services / pitstops

The event that was edited should not limit the dates and
odometers for the update.
This commit is contained in:
Joachim Lusiardi 2018-08-17 07:15:27 +02:00
parent 1d767d65a6
commit 6497d4677f
5 changed files with 24 additions and 41 deletions

View File

@ -14,16 +14,12 @@ class EditPitstopForm(FlaskForm):
litres = DecimalField('Litres (l)', places=2, validators=[litres_check]) litres = DecimalField('Litres (l)', places=2, validators=[litres_check])
costs = DecimalField('Costs (€, overall)', places=2, validators=[costs_check]) costs = DecimalField('Costs (€, overall)', places=2, validators=[costs_check])
submit = SubmitField(label='Update it!') submit = SubmitField(label='Update it!')
last_pitstop = None
same_odometer_allowed = True same_odometer_allowed = True
pitstops = [] pitstops = []
def set_pitstops(self, pitstops): def set_pitstops(self, pitstops):
self.pitstops = pitstops self.pitstops = pitstops
def set_pitstop(self, last_pitstop):
self.last_pitstop = last_pitstop
def set_consumable(self, consumable): def set_consumable(self, consumable):
self.litres.label = '%s (%s)' % (consumable.name, consumable.unit) self.litres.label = '%s (%s)' % (consumable.name, consumable.unit)
@ -44,6 +40,7 @@ class EditPitstopForm(FlaskForm):
} }
return messages return messages
class CreatePitstopForm(FlaskForm): class CreatePitstopForm(FlaskForm):
date = DateField('Date of Pitstop') date = DateField('Date of Pitstop')
odometer = IntegerField('Odometer (km)', validators=[odometer_date_check]) odometer = IntegerField('Odometer (km)', validators=[odometer_date_check])

View File

@ -43,33 +43,33 @@ class DeleteServiceForm(FlaskForm):
class EditServiceForm(FlaskForm): class EditServiceForm(FlaskForm):
date = DateField('Date of Service', validators=[date_check]) date = DateField('Date of Service')
odometer = IntegerField('Odometer (km)', validators=[odometer_check]) odometer = IntegerField('Odometer (km)', validators=[edit_odometer_date_check])
costs = DecimalField('Costs (€, overall)', places=2, validators=[costs_check]) costs = DecimalField('Costs (€, overall)', places=2, validators=[costs_check])
description = TextAreaField('Description', validators=[Length(1, 4096)]) description = TextAreaField('Description', validators=[Length(1, 4096)])
submit = SubmitField(label='Do it!') submit = SubmitField(label='Do it!')
last_pitstop = None
same_odometer_allowed = True same_odometer_allowed = True
pitstops = []
def set_pitstop(self, last_pitstop): def set_pitstops(self, pitstops):
self.last_pitstop = last_pitstop self.pitstops = pitstops
def preinit_with_data(self): def preinit_with_data(self):
if self.date.data: if self.date.data:
self.date.default = self.date.data self.date.default = self.date.data
else:
self.date.default = date.today()
if self.odometer.data: if self.odometer.data:
self.odometer.default = self.odometer.data self.odometer.default = self.odometer.data
else:
self.odometer.default = self.last_pitstop.odometer
if self.costs.data: if self.costs.data:
self.costs.default = self.costs.data self.costs.default = self.costs.data
else:
self.costs.default = 0
if self.description.data: if self.description.data:
self.description.default = self.description.data self.description.default = self.description.data
def get_hint_messages(self):
messages = {
'litres': 'Litres must be higher than 0.01 L.',
'costs': 'Costs must be higher than 0.01 €.'
}
return messages

View File

@ -127,15 +127,9 @@ def edit_pit_stop_form(pid):
if vehicle not in current_user.vehicles: if vehicle not in current_user.vehicles:
return redirect(url_for('get_pit_stops')) 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), 0, 0)
form = EditPitstopForm() form = EditPitstopForm()
form.set_pitstop(last_pitstop)
data = get_event_line_for_vehicle(vehicle) data = get_event_line_for_vehicle(vehicle)
data = [x for x in data if x != edit_pitstop]
form.set_pitstops(data) form.set_pitstops(data)
if not form.is_submitted(): if not form.is_submitted():
form.odometer.default = edit_pitstop.odometer form.odometer.default = edit_pitstop.odometer

View File

@ -70,16 +70,17 @@ def edit_service_form(sid):
if vehicle not in current_user.vehicles: if vehicle not in current_user.vehicles:
return redirect(url_for('get_pit_stops')) return redirect(url_for('get_pit_stops'))
form = EditServiceForm()
data = get_event_line_for_vehicle(vehicle) data = get_event_line_for_vehicle(vehicle)
data.reverse() data = [x for x in data if x != edit_service]
if len(data) > 0: form = EditServiceForm()
last_pitstop = Service(data[-1].date, data[-1].odometer, vehicle.id, 0, '')
else:
last_pitstop = Service(date(1970, 1, 1), 0, vehicle.id, 0, '')
form.set_pitstop(last_pitstop)
form.same_odometer_allowed = True form.same_odometer_allowed = True
form.set_pitstops(data)
if not form.is_submitted():
form.odometer.default = edit_service.odometer
form.description.default = edit_service.description
form.date.default = edit_service.date
form.costs.default = edit_service.costs
if form.validate_on_submit(): if form.validate_on_submit():
edit_service.costs = form.costs.data edit_service.costs = form.costs.data
edit_service.date = form.date.data edit_service.date = form.date.data
@ -89,18 +90,9 @@ def edit_service_form(sid):
db_log_update(edit_service) db_log_update(edit_service)
return redirect(url_for('get_pit_stops', _anchor='v' + str(vehicle.id))) return redirect(url_for('get_pit_stops', _anchor='v' + str(vehicle.id)))
form.odometer.default = edit_service.odometer form.preinit_with_data()
form.description.default = edit_service.description
form.date.default = edit_service.date
form.costs.default = edit_service.costs
form.process() form.process()
messages = { return render_template('editServiceForm.html', form=form, vehicle=vehicle, messages=form.get_hint_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('/service/vehicle/select', methods=['GET', 'POST']) @app.route('/service/vehicle/select', methods=['GET', 'POST'])

View File

@ -73,10 +73,10 @@
</tr> </tr>
</table> </table>
{% if loop.first %} {% if loop.first %}
{% endif %}
<a id="vehicle_{{vindex}}_edit_pitstop_{{loop.index}}" href="{{ url_for('edit_service_form', sid=field.id) }}" class="btn btn-primary"> <a id="vehicle_{{vindex}}_edit_pitstop_{{loop.index}}" href="{{ url_for('edit_service_form', sid=field.id) }}" class="btn btn-primary">
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> edit <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> edit
</a> </a>
{% endif %}
<a id="vehicle_{{vindex}}_delete_pitstop_{{loop.index}}" href="{{ url_for('delete_service_form', sid=field.id) }}" class="btn btn-primary btn-warning "> <a id="vehicle_{{vindex}}_delete_pitstop_{{loop.index}}" href="{{ url_for('delete_service_form', sid=field.id) }}" class="btn btn-primary btn-warning ">
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span> delete <span class="glyphicon glyphicon-trash" aria-hidden="true"></span> delete
</a> </a>