2026-05-14 14:30:23 +02:00
|
|
|
from flask import Blueprint, render_template, redirect, url_for, request, make_response, abort
|
2026-01-01 21:07:34 +01:00
|
|
|
from flask_login import login_required, current_user
|
|
|
|
|
from models import db
|
|
|
|
|
from models import User, ListOfItems, Item, Shared
|
|
|
|
|
from log import Log
|
2026-01-18 08:22:41 +01:00
|
|
|
from auth import create_cookie
|
2026-01-01 21:07:34 +01:00
|
|
|
|
|
|
|
|
lists_bp = Blueprint("lists", __name__)
|
|
|
|
|
|
|
|
|
|
def not_current_user(current_user_id):
|
|
|
|
|
result = []
|
|
|
|
|
users = User.query.all()
|
|
|
|
|
for user in users:
|
|
|
|
|
if user.id != current_user_id and not user.is_admin:
|
|
|
|
|
result.append(user)
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
@lists_bp.route("/")
|
|
|
|
|
@login_required
|
|
|
|
|
def home():
|
2026-01-18 08:22:41 +01:00
|
|
|
resp = make_response(render_template("lists_show.html", user=current_user, ilists=current_user.all_my_lists(),
|
|
|
|
|
button_top_url1 = url_for("auth.change_pwd"),
|
|
|
|
|
button_top_txt1 = "add",))
|
|
|
|
|
resp.set_cookie('username', create_cookie(current_user.id))
|
|
|
|
|
return resp
|
2026-01-01 21:07:34 +01:00
|
|
|
|
|
|
|
|
@lists_bp.route("/lists_edit", methods=["GET", "POST"])
|
|
|
|
|
@login_required
|
|
|
|
|
def edit():
|
|
|
|
|
newlist = ListOfItems(name="")
|
|
|
|
|
|
|
|
|
|
if request.method == "POST":
|
|
|
|
|
if "iid" in request.form:
|
|
|
|
|
ilist_id = int(request.form["iid"])
|
2026-05-14 14:30:23 +02:00
|
|
|
ilist = db.get_or_404(ListOfItems, ilist_id)
|
|
|
|
|
if not current_user.owns_list(ilist_id):
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
|
newname = request.form["name"].strip()
|
2026-01-01 21:07:34 +01:00
|
|
|
double = False
|
|
|
|
|
for d in ListOfItems.query.filter_by(owner_user_id=current_user.id, name=newname).all():
|
|
|
|
|
if not d.id == ilist_id:
|
|
|
|
|
# there is a duplicate name
|
|
|
|
|
double = True
|
|
|
|
|
if double:
|
2026-05-14 14:30:23 +02:00
|
|
|
return render_template("lists_edit.html", user=current_user,
|
|
|
|
|
ilists=current_user.my_owned_lists(with_inactive=True),
|
|
|
|
|
users=not_current_user(current_user.id),
|
|
|
|
|
newlist=newlist, error_id=ilist_id)
|
2026-01-01 21:07:34 +01:00
|
|
|
|
|
|
|
|
# if id already exists, overwrite the existing list, else create a new one
|
|
|
|
|
if ilist:
|
|
|
|
|
# exists: delete OR overwrite shared data, activate data and update
|
|
|
|
|
if "to_be_deleted" in request.form:
|
|
|
|
|
# remove all items connected tothis list
|
|
|
|
|
Log.info(f'User {current_user.id} deleted list {ilist.id} and all its items')
|
|
|
|
|
for item in Item.query.filter_by(listofitems_id=ilist.id).all():
|
|
|
|
|
db.session.delete(item)
|
|
|
|
|
db.session.delete(ilist)
|
|
|
|
|
else:
|
|
|
|
|
# update name
|
|
|
|
|
ilist.name = newname
|
|
|
|
|
# update active
|
|
|
|
|
ilist.is_active = True if "is_active" in request.form else False
|
|
|
|
|
# sharing. there may be 0 or more chacboces named "sharing_<n>" where <N> is the user.id
|
|
|
|
|
# first, remoe all the existing shares
|
|
|
|
|
for s in Shared.query.filter_by(listofitem_id=ilist_id).all():
|
|
|
|
|
db.session.delete(s)
|
|
|
|
|
# and then rebuild them
|
|
|
|
|
for u in not_current_user(current_user.id):
|
|
|
|
|
if f"shares_{u.id}" in request.form:
|
|
|
|
|
shared1 = Shared(user_id=u.id, listofitem_id=ilist_id)
|
|
|
|
|
db.session.add(shared1)
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
# must be a new list
|
2026-05-14 14:30:23 +02:00
|
|
|
newlist = ListOfItems(name=request.form["name"].strip(), owner_user_id=current_user.id)
|
2026-01-01 21:07:34 +01:00
|
|
|
# check if name exists
|
2026-05-14 14:30:23 +02:00
|
|
|
duplicate = ListOfItems.query.filter_by(owner_user_id=current_user.id, name=newlist.name).first()
|
2026-01-01 21:07:34 +01:00
|
|
|
if duplicate:
|
|
|
|
|
# there is a duplicate name
|
2026-05-14 14:30:23 +02:00
|
|
|
return render_template("lists_edit.html", user=current_user,
|
|
|
|
|
ilists=current_user.my_owned_lists(with_inactive=True),
|
|
|
|
|
users=not_current_user(current_user.id),
|
|
|
|
|
newlist=newlist, error_id=9999)
|
2026-01-01 21:07:34 +01:00
|
|
|
|
|
|
|
|
newlist.is_active = True
|
|
|
|
|
newlist.owner_user_id = current_user.id
|
|
|
|
|
db.session.add(newlist)
|
|
|
|
|
db.session.commit()
|
|
|
|
|
return home()
|
|
|
|
|
|
|
|
|
|
|
2026-01-18 08:22:41 +01:00
|
|
|
resp = make_response(render_template("lists_edit.html", user=current_user,
|
2026-01-01 21:07:34 +01:00
|
|
|
ilists=current_user.my_owned_lists(with_inactive=True),
|
|
|
|
|
users=not_current_user(current_user.id),
|
2026-01-18 08:22:41 +01:00
|
|
|
newlist=newlist))
|
|
|
|
|
resp.set_cookie('username', create_cookie(current_user.id))
|
|
|
|
|
return resp
|