121 lines
4.4 KiB
Python
121 lines
4.4 KiB
Python
from flask import url_for, redirect, render_template
|
|
from flask_security import login_required, current_user
|
|
from datetime import date
|
|
|
|
from ..entities import Vehicle, Service
|
|
from ..forms import CreateServiceForm, DeleteServiceForm, EditServiceForm, SelectVehicleForm
|
|
from ..tools import db_log_update, db_log_delete, get_event_line_for_vehicle, get_latest_pitstop_for_vehicle
|
|
from .. import app, db
|
|
|
|
|
|
@app.route('/service/vehicle/<int:vid>/create', methods=['GET', 'POST'])
|
|
@login_required
|
|
def create_service_for_vehicle(vid):
|
|
vehicle = Vehicle.query.get(vid)
|
|
if vehicle is None or vehicle not in current_user.vehicles:
|
|
return redirect(url_for('get_account_page'))
|
|
|
|
form = CreateServiceForm()
|
|
|
|
data = get_event_line_for_vehicle(vehicle)
|
|
if len(data) > 0:
|
|
form.set_pitstops(data)
|
|
form.same_odometer_allowed = type(data[-1]) != Service
|
|
else:
|
|
form.set_pitstops([])
|
|
form.same_odometer_allowed = True
|
|
|
|
form.preinit_with_data()
|
|
|
|
if form.validate_on_submit():
|
|
new_service = Service(form.date.data, form.odometer.data, vid, form.costs.data, form.description.data)
|
|
db.session.add(new_service)
|
|
vehicle.services.append(new_service)
|
|
db.session.commit()
|
|
return redirect(url_for('get_pit_stops', _anchor='v' + str(vehicle.id)))
|
|
|
|
form.process()
|
|
return render_template('createServiceForm.html', form=form, vehicle=vehicle, messages=[])
|
|
|
|
|
|
@app.route('/service/delete/<int:sid>', methods=['GET', 'POST'])
|
|
@login_required
|
|
def delete_service_form(sid):
|
|
service = Service.query.filter(Service.id == sid).first()
|
|
if service is None:
|
|
return redirect(url_for('get_pit_stops'))
|
|
|
|
vehicle = Vehicle.query.filter(Vehicle.id == service.vehicle_id).first()
|
|
if vehicle not in current_user.vehicles:
|
|
return redirect(url_for('get_pit_stops'))
|
|
|
|
form = DeleteServiceForm()
|
|
if form.validate_on_submit():
|
|
db.session.delete(service)
|
|
db.session.commit()
|
|
db_log_delete(service)
|
|
return redirect(url_for('get_pit_stops', _anchor='v' + str(vehicle.id)))
|
|
|
|
return render_template('deleteServiceForm.html', form=form, service=service )
|
|
|
|
|
|
@app.route('/service/edit/<int:sid>', methods=['GET', 'POST'])
|
|
@login_required
|
|
def edit_service_form(sid):
|
|
edit_service = Service.query.get(sid)
|
|
if edit_service is None:
|
|
return redirect(url_for('get_pit_stops'))
|
|
|
|
vehicle = Vehicle.query.filter(Vehicle.id == edit_service.vehicle_id).first()
|
|
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)
|
|
form.same_odometer_allowed = True
|
|
|
|
if form.validate_on_submit():
|
|
edit_service.costs = form.costs.data
|
|
edit_service.date = form.date.data
|
|
edit_service.description = form.description.data
|
|
edit_service.odometer = form.odometer.data
|
|
db.session.commit()
|
|
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.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)
|
|
|
|
|
|
@app.route('/service/vehicle/select', methods=['GET', 'POST'])
|
|
@login_required
|
|
def select_vehicle_for_new_service():
|
|
if len(current_user.vehicles) == 1:
|
|
return redirect(url_for('create_service_for_vehicle', vid=current_user.vehicles[0].id))
|
|
|
|
form = SelectVehicleForm()
|
|
form.vehicle.choices = [(g.id, g.name) for g in current_user.vehicles]
|
|
|
|
if form.validate_on_submit():
|
|
return redirect(url_for('create_service_for_vehicle', vid=form.vehicle.data))
|
|
|
|
return render_template('selectVehicle.html', form=form)
|
|
|
|
|