regular_costs #5

Merged
jlusiardi merged 19 commits from regular_costs into master 2021-06-18 20:20:13 +02:00
4 changed files with 52 additions and 18 deletions
Showing only changes of commit 1d767d65a6 - Show all commits

View File

@ -5,7 +5,7 @@ from datetime import date
def odometer_date_check(form, field): def odometer_date_check(form, field):
""" """
Checks that the entered date and odometer of the pit stop is conformant to the existing pit stops. That means, if a Checks that the entered date and odometer of the pit stop is conformant to the existing pit stops. That means, if a
pitstops date is between to other pitstops, the odometer should be as well. pitstops date is between two other pit stops, the odometer should be as well.
:param form: :param form:
:param field: :param field:
:return: :return:
@ -29,6 +29,33 @@ def odometer_date_check(form, field):
% (pitstops[index].odometer,pitstops[index+1].odometer)) % (pitstops[index].odometer,pitstops[index+1].odometer))
def edit_odometer_date_check(form, field):
"""
This makes exactly the same checks as 'odometer_date_check' but the odometers may be the same (to change only amount
and price).
:param form:
:param field:
:return:
"""
odometer = form.odometer.data
date = form.date.data
pitstops = form.pitstops
if len(pitstops) > 0:
if date < pitstops[0].date and odometer > pitstops[0].odometer:
raise ValidationError('The new odometer value must be less than %i km' % pitstops[0].odometer)
if date >= pitstops[-1].date and odometer < pitstops[-1].odometer:
raise ValidationError('The new odometer value must be greater than %i km' % pitstops[-1].odometer)
if len(pitstops) > 1:
for index in range(0, len(pitstops)-1):
if pitstops[index].date <= date < pitstops[index + 1].date:
if odometer < pitstops[index].odometer or odometer > pitstops[index+1].odometer:
raise ValidationError('The new odometer value must be greater than %i km and less than %i km'
% (pitstops[index].odometer,pitstops[index+1].odometer))
def date_check(form, field): def date_check(form, field):
""" """

View File

@ -9,13 +9,17 @@ class DeletePitStopForm(FlaskForm):
class EditPitstopForm(FlaskForm): class EditPitstopForm(FlaskForm):
date = DateField('Date of Pitstop', validators=[date_check]) date = DateField('Date of Pitstop')
odometer = IntegerField('Odometer (km)', validators=[odometer_check]) odometer = IntegerField('Odometer (km)', validators=[edit_odometer_date_check])
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=[edit_costs_check]) costs = DecimalField('Costs (€, overall)', places=2, validators=[costs_check])
submit = SubmitField(label='Update it!') submit = SubmitField(label='Update it!')
last_pitstop = None last_pitstop = None
same_odometer_allowed = True same_odometer_allowed = True
pitstops = []
def set_pitstops(self, pitstops):
self.pitstops = pitstops
def set_pitstop(self, last_pitstop): def set_pitstop(self, last_pitstop):
self.last_pitstop = last_pitstop self.last_pitstop = last_pitstop
@ -33,6 +37,12 @@ class EditPitstopForm(FlaskForm):
if self.costs.data: if self.costs.data:
self.costs.default = self.costs.data self.costs.default = self.costs.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
class CreatePitstopForm(FlaskForm): class CreatePitstopForm(FlaskForm):
date = DateField('Date of Pitstop') date = DateField('Date of Pitstop')

View File

@ -135,7 +135,13 @@ def edit_pit_stop_form(pid):
form = EditPitstopForm() form = EditPitstopForm()
form.set_pitstop(last_pitstop) form.set_pitstop(last_pitstop)
data = get_event_line_for_vehicle(vehicle)
form.set_pitstops(data)
if not form.is_submitted():
form.odometer.default = edit_pitstop.odometer
form.litres.default = edit_pitstop.amount
form.date.default = edit_pitstop.date
form.costs.default = edit_pitstop.costs
if form.validate_on_submit(): if form.validate_on_submit():
edit_pitstop.costs = form.costs.data edit_pitstop.costs = form.costs.data
edit_pitstop.date = form.date.data edit_pitstop.date = form.date.data
@ -145,18 +151,9 @@ def edit_pit_stop_form(pid):
db_log_update(edit_pitstop) db_log_update(edit_pitstop)
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_pitstop.odometer form.preinit_with_data()
form.litres.default = edit_pitstop.amount
form.date.default = edit_pitstop.date
form.costs.default = edit_pitstop.costs
form.process() form.process()
messages = { return render_template('editPitStopForm.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_pitstop.costs is not None and 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'])

View File

@ -31,10 +31,10 @@
</tr> </tr>
</table> </table>
{% if loop.first %} {% if loop.first %}
{% endif %}
<a id="vehicle_{{vindex}}_edit_pitstop_{{loop.index}}" href="{{ url_for('edit_pit_stop_form', pid=field.id) }}" class="btn btn-primary"> <a id="vehicle_{{vindex}}_edit_pitstop_{{loop.index}}" href="{{ url_for('edit_pit_stop_form', pid=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_pit_stop_form', pid=field.id) }}" class="btn btn-primary btn-warning "> <a id="vehicle_{{vindex}}_delete_pitstop_{{loop.index}}" href="{{ url_for('delete_pit_stop_form', pid=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>