rollerverbrauch/app/routes/service.py

137 lines
4.2 KiB
Python
Raw Normal View History

2016-11-01 18:01:52 +01:00
from flask import url_for, redirect, render_template
from flask_security import login_required, current_user
from datetime import date
2016-11-06 13:22:54 +01:00
from ..entities import Vehicle, Service
2021-06-20 08:43:46 +02:00
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,
)
2016-11-01 18:01:52 +01:00
from .. import app, db
2021-06-20 08:43:46 +02:00
@app.route("/service/vehicle/<int:vid>/create", methods=["GET", "POST"])
2016-11-01 18:01:52 +01:00
@login_required
def create_service_for_vehicle(vid):
vehicle = Vehicle.query.get(vid)
if vehicle is None or vehicle not in current_user.vehicles:
2021-06-20 08:43:46 +02:00
return redirect(url_for("get_account_page"))
2016-11-01 18:01:52 +01:00
form = CreateServiceForm()
2016-11-06 13:22:54 +01:00
data = get_event_line_for_vehicle(vehicle)
if len(data) > 0:
form.set_pitstops(data)
2016-11-06 13:22:54 +01:00
form.same_odometer_allowed = type(data[-1]) != Service
else:
form.set_pitstops([])
2016-11-06 13:22:54 +01:00
form.same_odometer_allowed = True
2016-11-01 18:01:52 +01:00
form.preinit_with_data()
if form.validate_on_submit():
2021-06-20 08:43:46 +02:00
new_service = Service(
form.date.data,
form.odometer.data,
vid,
form.costs.data,
form.description.data,
)
2016-11-01 18:01:52 +01:00
db.session.add(new_service)
vehicle.services.append(new_service)
db.session.commit()
2021-06-20 08:43:46 +02:00
return redirect(url_for("get_pit_stops", _anchor="v" + str(vehicle.id)))
2016-11-01 18:01:52 +01:00
form.process()
2021-06-20 08:43:46 +02:00
return render_template(
"createServiceForm.html", form=form, vehicle=vehicle, messages=[]
)
2016-11-01 18:01:52 +01:00
2021-06-20 08:43:46 +02:00
@app.route("/service/delete/<int:sid>", methods=["GET", "POST"])
2016-11-01 18:01:52 +01:00
@login_required
def delete_service_form(sid):
service = Service.query.filter(Service.id == sid).first()
if service is None:
2021-06-20 08:43:46 +02:00
return redirect(url_for("get_pit_stops"))
2016-11-06 13:22:54 +01:00
2016-11-01 18:01:52 +01:00
vehicle = Vehicle.query.filter(Vehicle.id == service.vehicle_id).first()
if vehicle not in current_user.vehicles:
2021-06-20 08:43:46 +02:00
return redirect(url_for("get_pit_stops"))
2016-11-01 18:01:52 +01:00
form = DeleteServiceForm()
if form.validate_on_submit():
db.session.delete(service)
db.session.commit()
db_log_delete(service)
2021-06-20 08:43:46 +02:00
return redirect(url_for("get_pit_stops", _anchor="v" + str(vehicle.id)))
2016-11-01 18:01:52 +01:00
2021-06-20 08:43:46 +02:00
return render_template("deleteServiceForm.html", form=form, service=service)
2016-11-01 18:01:52 +01:00
2021-06-20 08:43:46 +02:00
@app.route("/service/edit/<int:sid>", methods=["GET", "POST"])
2016-11-01 18:01:52 +01:00
@login_required
def edit_service_form(sid):
edit_service = Service.query.get(sid)
if edit_service is None:
2021-06-20 08:43:46 +02:00
return redirect(url_for("get_pit_stops"))
2016-11-01 18:01:52 +01:00
vehicle = Vehicle.query.filter(Vehicle.id == edit_service.vehicle_id).first()
if vehicle not in current_user.vehicles:
2021-06-20 08:43:46 +02:00
return redirect(url_for("get_pit_stops"))
2016-11-01 18:01:52 +01:00
2016-11-06 13:22:54 +01:00
data = get_event_line_for_vehicle(vehicle)
data = [x for x in data if x != edit_service]
form = EditServiceForm()
2016-11-06 13:22:54 +01:00
form.same_odometer_allowed = True
form.set_pitstops(data)
2016-11-01 18:01:52 +01:00
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
2016-11-01 18:01:52 +01:00
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)
2021-06-20 08:43:46 +02:00
return redirect(url_for("get_pit_stops", _anchor="v" + str(vehicle.id)))
2016-11-01 18:01:52 +01:00
form.preinit_with_data()
2016-11-01 18:01:52 +01:00
form.process()
2021-06-20 08:43:46 +02:00
return render_template(
"editServiceForm.html",
form=form,
vehicle=vehicle,
messages=form.get_hint_messages(),
)
2016-11-06 13:22:54 +01:00
2021-06-20 08:43:46 +02:00
@app.route("/service/vehicle/select", methods=["GET", "POST"])
2016-11-06 13:22:54 +01:00
@login_required
def select_vehicle_for_new_service():
2021-06-20 08:43:46 +02:00
active_vehicles = get_users_active_vehicle(current_user)
if len(active_vehicles) == 1:
return redirect(
url_for("create_service_for_vehicle", vid=active_vehicles[0].id)
)
2016-11-06 13:22:54 +01:00
form = SelectVehicleForm()
2021-06-20 08:43:46 +02:00
form.vehicle.choices = [(g.id, g.name) for g in active_vehicles]
2016-11-06 13:22:54 +01:00
if form.validate_on_submit():
2021-06-20 08:43:46 +02:00
return redirect(url_for("create_service_for_vehicle", vid=form.vehicle.data))
2016-11-06 13:22:54 +01:00
2021-06-20 08:43:46 +02:00
return render_template("selectVehicle.html", form=form)