from flask import url_for, redirect, render_template, flash from flask_security import login_required from flask_security.core import current_user from sqlalchemy.exc import IntegrityError from datetime import date from ..entities import Vehicle, Consumable, Pitstop, RegularCost from ..forms import ( SelectVehicleForm, CreateRegularCostForm, DeleteRegularCostForm, EditRegularCostForm, EndRegularCostForm, ) from ..tools import ( db_log_update, db_log_delete, db_log_add, pitstop_service_key, get_event_line_for_vehicle, update_filling_station_prices, ) from .. import app, db @app.route("/regular_costs/delete/", methods=["GET", "POST"]) @login_required def delete_regular_form(pid): regular_cost = RegularCost.query.filter(RegularCost.id == pid).first() if regular_cost is None: return redirect(url_for("get_pit_stops")) vehicle = Vehicle.query.filter(Vehicle.id == regular_cost.vehicle_id).first() if vehicle not in current_user.vehicles: return redirect(url_for("get_pit_stops")) form = DeleteRegularCostForm() if form.validate_on_submit(): db.session.delete(regular_cost) db.session.commit() db_log_delete(regular_cost) return redirect(url_for("get_pit_stops", _anchor="v" + str(vehicle.id))) return render_template( "deleteRegularCostForm.html", form=form, regular_cost=regular_cost ) @app.route("/regular_costs/vehicle/select", methods=["GET", "POST"]) @login_required def select_vehicle_for_new_regular_cost(): if len(current_user.vehicles) == 1: return redirect( url_for("create_regular_cost_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_regular_cost_for_vehicle", vid=form.vehicle.data) ) return render_template("selectVehicle.html", form=form) @app.route("/regular_costs/vehicle//create", methods=["GET", "POST"]) @login_required def create_regular_cost_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 = CreateRegularCostForm() form.preinit_with_data() if form.validate_on_submit(): regular_cost = RegularCost( vid, form.description.data, form.costs.data, form.days.data, form.start_at.data, form.ends_at.data, ) db.session.add(regular_cost) vehicle.regulars.append(regular_cost) db.session.commit() return redirect(url_for("get_pit_stops", _anchor="v" + str(vehicle.id))) form.process() return render_template( "createRegularCostForm.html", form=form, vehicle=vehicle, messages=[] ) @app.route("/regular_costs/edit/", methods=["GET", "POST"]) @login_required def edit_regular_form(pid): edit_regular = RegularCost.query.get(pid) if edit_regular is None: return redirect(url_for("get_pit_stops")) vehicle = Vehicle.query.filter(Vehicle.id == edit_regular.vehicle_id).first() if vehicle not in current_user.vehicles: return redirect(url_for("get_pit_stops")) form = EditRegularCostForm() form.preinit_with_data() if not form.is_submitted(): form.costs.default = edit_regular.costs form.start_at.default = edit_regular.start_at form.ends_at.default = edit_regular.ends_at form.days.default = edit_regular.days form.description.default = edit_regular.description if form.validate_on_submit(): edit_regular.start_at = form.start_at.data edit_regular.ends_at = form.ends_at.data edit_regular.costs = form.costs.data edit_regular.days = form.days.data edit_regular.description = form.description.data db.session.commit() db_log_update(edit_regular) return redirect(url_for("get_pit_stops", _anchor="v" + str(vehicle.id))) form.preinit_with_data() form.process() return render_template( "editRegularCostForm.html", form=form, vehicle=vehicle, messages=form.get_hint_messages(), ) @app.route("/regular_costs/end/", methods=["GET", "POST"]) @login_required def end_regular_form(pid): edit_regular = RegularCost.query.get(pid) if edit_regular is None: return redirect(url_for("get_pit_stops")) vehicle = Vehicle.query.filter(Vehicle.id == edit_regular.vehicle_id).first() if vehicle not in current_user.vehicles: return redirect(url_for("get_pit_stops")) form = EndRegularCostForm() if form.validate_on_submit(): edit_regular.ends_at = date.today() db.session.commit() return redirect(url_for("get_pit_stops", _anchor="v" + str(vehicle.id))) return render_template( "endRegularCostForm.html", form=form, vehicle=vehicle, )