add and remove front
list shows up in a header bar on the home page
This commit is contained in:
@@ -15,7 +15,6 @@ def new():
|
||||
if request.method == 'POST':
|
||||
name = request.form['name']
|
||||
bio = request.form['bio']
|
||||
#icon = request.form['icon']
|
||||
user_id = g.user[0]
|
||||
db = get_db()
|
||||
error = None
|
||||
@@ -52,12 +51,41 @@ def edit(mid):
|
||||
db.commit()
|
||||
|
||||
if "file" in request.files:
|
||||
# here we are just saving the uploaded file to the icons folder.
|
||||
# we're not going hard on security because we expect there to only be 1 admin
|
||||
# but the filename will always be changed to a random string of numbers and letters known as uuid
|
||||
file = request.files["file"]
|
||||
filename = str(uuid.uuid4()) + "." + file.filename.split(".")[1]
|
||||
file.save(os.path.join(current_app.config["UPLOAD_FOLDER"], filename))
|
||||
file.save(os.path.join(current_app.config["ICON_UPLOAD_FOLDER"], filename))
|
||||
db.execute("INSERT INTO icons (member_id, icon_location) VALUES (?, ?)", (mid, filename),)
|
||||
db.commit()
|
||||
|
||||
# this specific chunk here is checking whether icons in the myriad/static/icons folder have a link in the database
|
||||
# in case the database was rebuilt, or something else happened, it is a waste of storage keeping an unlinked image
|
||||
icons = db.execute("SELECT * FROM icons").fetchall()
|
||||
icon_storage = os.listdir(current_app.config["ICON_UPLOAD_FOLDER"])
|
||||
for icon in icon_storage:
|
||||
in_database = False
|
||||
for i in icons:
|
||||
print(i[2], icon)
|
||||
if i[2] == icon:
|
||||
in_database = True
|
||||
if not in_database:
|
||||
os.remove(os.path.join(current_app.config["ICON_UPLOAD_FOLDER"], icon))
|
||||
|
||||
# and now for the same in reverse - clean the database of references to images that don't exist
|
||||
for i in icons:
|
||||
in_storage = False
|
||||
print(i[2], i[0])
|
||||
if i[2] in icon_storage:
|
||||
in_storage = True
|
||||
if not in_storage:
|
||||
db.execute("DELETE FROM icons WHERE id=(?)", (i[0],),)
|
||||
db.commit()
|
||||
|
||||
# the above cleanup operations should be a button in the manage sidebar but for now they are here.
|
||||
|
||||
|
||||
member = db.execute("SELECT * FROM member WHERE id=(?)",(mid,)).fetchone()
|
||||
icons = db.execute("SELECT * FROM icons WHERE member_id=(?)",(mid,)).fetchall()
|
||||
return render_template("manage/edit.html", member=member, icons=icons)
|
||||
@@ -71,4 +99,22 @@ def set_main_icon(mid, icon_id):
|
||||
db.execute("UPDATE member SET main_icon=(?) WHERE id=(?)",(icon_id, mid))
|
||||
db.commit()
|
||||
|
||||
return redirect(url_for("manage.edit", mid=mid))
|
||||
return redirect(url_for("manage.edit", mid=mid))
|
||||
|
||||
@bp.route("/add_to_front/<mid>")
|
||||
@login_required
|
||||
def add_to_front(mid):
|
||||
db = get_db()
|
||||
db.execute("UPDATE member SET front=(?) WHERE id=(?)",(1, mid))
|
||||
db.commit()
|
||||
|
||||
return redirect(url_for('home.full_list'))
|
||||
|
||||
@bp.route("/remove_front/<mid>")
|
||||
@login_required
|
||||
def remove_front(mid):
|
||||
db = get_db()
|
||||
db.execute("UPDATE member SET front=(?) WHERE id=(?)",(0, mid))
|
||||
db.commit()
|
||||
|
||||
return redirect(url_for('home.full_list'))
|
||||
|
||||
Reference in New Issue
Block a user