rollerverbrauch/app/routes/account.py

162 lines
5.3 KiB
Python
Raw Normal View History

2017-11-07 22:33:14 +01:00
from flask import url_for, redirect, render_template, request, jsonify
2016-11-01 18:01:52 +01:00
from flask_security import login_required
from flask_security.core import current_user
from sqlalchemy.exc import IntegrityError
import json
2016-11-01 18:01:52 +01:00
from ..entities import Vehicle, Consumable
2016-11-01 18:01:52 +01:00
from ..forms import EditVehicleForm, DeleteVehicleForm, DeleteAccountForm
from ..tools import db_log_update, db_log_delete, db_log_add
from .. import app, db, user_datastore
2016-11-01 18:01:52 +01:00
2021-06-20 08:43:46 +02:00
@app.route("/account", methods=["GET"])
2016-11-01 18:01:52 +01:00
@login_required
def get_account_page():
2021-06-20 08:43:46 +02:00
return render_template(
"account.html",
map_pos=(current_user.home_lat, current_user.home_long, current_user.home_zoom),
)
2016-11-01 18:01:52 +01:00
2021-06-20 08:43:46 +02:00
@app.route("/account/vehicle/edit/<int:vid>", methods=["GET", "POST"])
2016-11-01 18:01:52 +01:00
@login_required
def edit_vehicle(vid):
vehicle = Vehicle.query.filter(Vehicle.id == vid).first()
# prevent edit of foreign vehicles
if 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 = EditVehicleForm()
form.consumables.choices = [(g.id, g.name) for g in Consumable.query.all()]
if not form.consumables.data:
form.consumables.default = [g.id for g in vehicle.consumables]
if form.name.data is not None:
form.name.default = form.name.data
2021-06-20 08:43:46 +02:00
if form.is_active.data is not None:
form.is_active.default = form.is_active.data
2016-11-01 18:01:52 +01:00
if form.validate_on_submit():
vehicle.name = form.name.data
2021-06-20 08:43:46 +02:00
vehicle.is_active = form.is_active.data
2016-11-01 18:01:52 +01:00
# we cannot delete consumables where there are pitstops for => report error
vehicle.consumables = []
2017-11-07 22:33:14 +01:00
for consumable_id in form.consumables.data:
2016-11-01 18:01:52 +01:00
consumable = Consumable.query.get(consumable_id)
if consumable is not None:
vehicle.consumables.append(consumable)
try:
db.session.commit()
db_log_update(vehicle)
except IntegrityError:
db.session.rollback()
form.name.errors.append('"%s" is not unique.' % (form.name.data))
2021-06-20 08:43:46 +02:00
return render_template("editVehicleForm.html", form=form)
return redirect(url_for("get_account_page"))
2016-11-01 18:01:52 +01:00
form.name.default = vehicle.name
2021-06-20 08:43:46 +02:00
form.is_active.default = vehicle.is_active
2016-11-01 18:01:52 +01:00
form.process()
2021-06-20 08:43:46 +02:00
return render_template("editVehicleForm.html", form=form, vehicle=vehicle)
2016-11-01 18:01:52 +01:00
2021-06-20 08:43:46 +02:00
@app.route("/account/vehicle/delete/<int:vid>", methods=["GET", "POST"])
2016-11-01 18:01:52 +01:00
@login_required
def delete_vehicle(vid):
vehicle = Vehicle.query.filter(Vehicle.id == vid).first()
# prevent deletion of foreign vehicles
if 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
if len(current_user.vehicles) == 1:
2021-06-20 08:43:46 +02:00
return redirect(url_for("get_account_page"))
2016-11-01 18:01:52 +01:00
form = DeleteVehicleForm()
if form.validate_on_submit():
db.session.delete(vehicle)
db.session.commit()
db_log_delete(vehicle)
2021-06-20 08:43:46 +02:00
return redirect(url_for("get_account_page"))
2016-11-01 18:01:52 +01:00
2021-06-20 08:43:46 +02:00
return render_template("deleteVehicleForm.html", form=form, vehicle=vehicle)
2016-11-01 18:01:52 +01:00
2021-06-20 08:43:46 +02:00
@app.route("/account/vehicle/create", methods=["GET", "POST"])
2016-11-01 18:01:52 +01:00
@login_required
def create_vehicle():
form = EditVehicleForm()
form.consumables.choices = [(g.id, g.name) for g in Consumable.query.all()]
if form.name.data is not None:
form.name.default = form.name.data
if form.consumables.data:
form.consumables.default = form.consumables.data
else:
form.consumables.default = []
if form.validate_on_submit():
if len(form.consumables.data) == 0:
2021-06-20 08:43:46 +02:00
form.consumables.errors.append("At least one consumable must be selected.")
return render_template("createVehicleForm.html", form=form)
2016-11-01 18:01:52 +01:00
vehicle_name = form.name.data
new_vehicle = Vehicle(vehicle_name)
for consumable_id in form.consumables.data:
consumable = Consumable.query.get(consumable_id)
if consumable is not None:
new_vehicle.consumables.append(consumable)
db.session.add(new_vehicle)
current_user.vehicles.append(new_vehicle)
try:
db.session.commit()
db_log_add(new_vehicle)
except IntegrityError:
db.session.rollback()
form.name.errors.append('"%s" is not unique.' % (form.name.data))
2021-06-20 08:43:46 +02:00
return render_template("createVehicleForm.html", form=form)
return redirect(url_for("get_account_page"))
2016-11-01 18:01:52 +01:00
2021-06-20 08:43:46 +02:00
return render_template("createVehicleForm.html", form=form)
2016-11-01 18:01:52 +01:00
2021-06-20 08:43:46 +02:00
@app.route("/account/delete", methods=["GET", "POST"])
2016-11-01 18:01:52 +01:00
@login_required
def delete_account():
form = DeleteAccountForm()
if form.validate_on_submit():
user_datastore.delete_user(current_user)
db.session.commit()
2021-06-20 08:43:46 +02:00
return redirect(url_for("index"))
2016-11-01 18:01:52 +01:00
2021-06-20 08:43:46 +02:00
return render_template("deleteAccountForm.html", form=form)
2016-11-01 18:01:52 +01:00
2021-06-20 08:43:46 +02:00
@app.route("/account/home", methods=["GET"])
2017-11-07 22:33:14 +01:00
@login_required
def get_users_home():
return jsonify(
2021-06-20 08:43:46 +02:00
{
"lat": float(current_user.home_lat),
"long": float(current_user.home_long),
"zoom": current_user.home_zoom,
}
)
2017-11-07 22:33:14 +01:00
2021-06-20 08:43:46 +02:00
@app.route("/account/home", methods=["POST"])
2017-11-07 22:33:14 +01:00
@login_required
def set_users_home():
2021-06-20 08:43:46 +02:00
current_user.home_lat = request.json["lat"]
current_user.home_long = request.json["long"]
current_user.home_zoom = request.json["zoom"]
2017-11-07 22:33:14 +01:00
db.session.commit()
return jsonify({})