regular_costs #5

Merged
jlusiardi merged 19 commits from regular_costs into master 2021-06-18 20:20:13 +02:00
5 changed files with 24 additions and 41 deletions
Showing only changes of commit 6497d4677f - Show all commits

View File

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

View File

@ -43,33 +43,33 @@ class DeleteServiceForm(FlaskForm):
class EditServiceForm(FlaskForm):
date = DateField('Date of Service', validators=[date_check])
odometer = IntegerField('Odometer (km)', validators=[odometer_check])
date = DateField('Date of Service')
odometer = IntegerField('Odometer (km)', validators=[edit_odometer_date_check])
costs = DecimalField('Costs (€, overall)', places=2, validators=[costs_check])
description = TextAreaField('Description', validators=[Length(1, 4096)])
submit = SubmitField(label='Do it!')
last_pitstop = None
same_odometer_allowed = True
pitstops = []
def set_pitstop(self, last_pitstop):
self.last_pitstop = last_pitstop
def set_pitstops(self, pitstops):
self.pitstops = pitstops
def preinit_with_data(self):
if self.date.data:
self.date.default = self.date.data
else:
self.date.default = date.today()
if self.odometer.data:
self.odometer.default = self.odometer.data
else:
self.odometer.default = self.last_pitstop.odometer
if self.costs.data:
self.costs.default = self.costs.data
else:
self.costs.default = 0
if 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:
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.set_pitstop(last_pitstop)
data = get_event_line_for_vehicle(vehicle)
data = [x for x in data if x != edit_pitstop]
form.set_pitstops(data)
if not form.is_submitted():
form.odometer.default = edit_pitstop.odometer

View File

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

View File

@ -73,10 +73,10 @@
</tr>
</table>
{% 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">
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> edit
</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 ">
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span> delete
</a>