91 lines
4.0 KiB
Python
91 lines
4.0 KiB
Python
from flask import Blueprint, render_template, redirect, url_for, request
|
|
from flask_login import login_required, current_user
|
|
from models import db
|
|
from models import User, ListOfItems, Item, Shared
|
|
from log import Log
|
|
|
|
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():
|
|
return render_template("lists_show.html", user=current_user, ilists=current_user.all_my_lists())
|
|
|
|
@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"])
|
|
newname = request.form["name"]
|
|
print(ilist_id)
|
|
print(newname)
|
|
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:
|
|
print("found double")
|
|
return render_template("lists_edit.html", user=current_user, ilists=current_user.my_owned_lists(with_inactive=True), newlist=newlist, error_id=ilist_id)
|
|
|
|
# if id already exists, overwrite the existing list, else create a new one
|
|
ilist = ListOfItems.query.get(ilist_id)
|
|
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:
|
|
print("not to be deleted but updated")
|
|
# 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():
|
|
print(f"deleting {s.user_id} - {s.listofitem_id}")
|
|
db.session.delete(s)
|
|
# and then rebuild them
|
|
for u in not_current_user(current_user.id):
|
|
print(f"tryin user {u.id}")
|
|
if f"shares_{u.id}" in request.form:
|
|
print(f"found sharebox: {u.id}")
|
|
shared1 = Shared(user_id=u.id, listofitem_id=ilist_id)
|
|
db.session.add(shared1)
|
|
|
|
else:
|
|
# must be a new list
|
|
newlist = ListOfItems(name=request.form["name"], owner_user_id=current_user.id)
|
|
# check if name exists
|
|
duplicate = ListOfItems.query.filter_by(owner_user_id=current_user.id, name=request.form["name"]).first()
|
|
if duplicate:
|
|
# there is a duplicate name
|
|
return render_template("lists_edit.html", user=current_user, ilists=current_user.my_owned_lists(with_inactive=True), newlist=newlist, error_id=9999)
|
|
|
|
newlist.is_active = True
|
|
newlist.owner_user_id = current_user.id
|
|
db.session.add(newlist)
|
|
db.session.commit()
|
|
return home()
|
|
|
|
|
|
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)
|