flask app for plurals to publicly share member lists

manage.py 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. from flask import (
  2. Blueprint, flash, g, redirect, render_template, request, session, url_for, current_app
  3. )
  4. from werkzeug.utils import secure_filename
  5. import os, uuid
  6. from myriad.auth import login_required
  7. from myriad.db import get_db
  8. bp = Blueprint('manage', __name__, url_prefix='/manage')
  9. @bp.route('/new', methods=('GET', 'POST'))
  10. @login_required
  11. def new():
  12. if request.method == 'POST':
  13. name = request.form['name']
  14. bio = request.form['bio']
  15. user_id = g.user[0]
  16. db = get_db()
  17. error = None
  18. if not name:
  19. error = 'Name is required.'
  20. if error is None:
  21. db.execute(
  22. "INSERT INTO member (user_id, member_name, bio) VALUES (?, ?, ?)",
  23. (user_id, name, bio),
  24. )
  25. db.commit()
  26. return redirect(url_for('home.full_list'))
  27. return render_template('manage/new.html', error=error)
  28. return render_template('manage/new.html')
  29. @bp.route("/edit/<mid>", methods=('GET', 'POST'))
  30. @login_required
  31. def edit(mid):
  32. db = get_db()
  33. member = db.execute("SELECT * FROM member WHERE id=(?)",(mid,)).fetchone()
  34. icons = db.execute("SELECT * FROM icons WHERE member_id=(?)",(mid,)).fetchall()
  35. if request.method == "POST":
  36. if "name" in request.form:
  37. name = request.form['name']
  38. bio = request.form['bio']
  39. subtitle = request.form['subtitle']
  40. db.execute("UPDATE member SET member_name=(?), bio=(?), subtitle=(?) WHERE id=(?)",(name, bio, subtitle, mid))
  41. db.commit()
  42. if "file" in request.files:
  43. # here we are just saving the uploaded file to the icons folder.
  44. # we're not going hard on security because we expect there to only be 1 admin
  45. # but the filename will always be changed to a random string of numbers and letters known as uuid
  46. file = request.files["file"]
  47. filename = str(uuid.uuid4()) + "." + file.filename.split(".")[1]
  48. file.save(os.path.join(current_app.config["ICON_UPLOAD_FOLDER"], filename))
  49. db.execute("INSERT INTO icons (member_id, icon_location) VALUES (?, ?)", (mid, filename),)
  50. db.commit()
  51. # this specific chunk here is checking whether icons in the myriad/static/icons folder have a link in the database
  52. # in case the database was rebuilt, or something else happened, it is a waste of storage keeping an unlinked image
  53. icons = db.execute("SELECT * FROM icons").fetchall()
  54. icon_storage = os.listdir(current_app.config["ICON_UPLOAD_FOLDER"])
  55. for icon in icon_storage:
  56. in_database = False
  57. for i in icons:
  58. print(i[2], icon)
  59. if i[2] == icon:
  60. in_database = True
  61. if not in_database:
  62. os.remove(os.path.join(current_app.config["ICON_UPLOAD_FOLDER"], icon))
  63. # and now for the same in reverse - clean the database of references to images that don't exist
  64. for i in icons:
  65. in_storage = False
  66. print(i[2], i[0])
  67. if i[2] in icon_storage:
  68. in_storage = True
  69. if not in_storage:
  70. db.execute("DELETE FROM icons WHERE id=(?)", (i[0],),)
  71. db.commit()
  72. # the above cleanup operations should be a button in the manage sidebar but for now they are here.
  73. if "c9" in request.form:
  74. c9 = request.form["c9"]
  75. c10 = request.form["c10"]
  76. c11 = request.form["c11"]
  77. c12 = request.form["c12"]
  78. c13 = request.form["c13"]
  79. c14 = request.form["c14"]
  80. c15 = request.form["c15"]
  81. c16 = request.form["c16"]
  82. #c17 = request.form["c17"]
  83. #c18 = request.form["c18"]
  84. #c19 = request.form["c19"]
  85. #c20 = request.form["c20"]
  86. c21 = request.form["c21"]
  87. c22 = request.form["c22"]
  88. db.execute("UPDATE member SET card_border=(?), card_bg=(?), heading_bg=(?), heading_border=(?), heading_name=(?), heading_subtitle=(?), card_text=(?), icon_border=(?), a1=(?), a2=(?) WHERE id=(?)",(c9, c10, c11, c12, c13, c14, c15, c16, c21, c22, mid))
  89. db.commit()
  90. member = db.execute("SELECT * FROM member WHERE id=(?)",(mid,)).fetchone()
  91. icons = db.execute("SELECT * FROM icons WHERE member_id=(?)",(mid,)).fetchall()
  92. return render_template("manage/edit.html", member=member, icons=icons)
  93. return render_template("manage/edit.html", member=member, icons=icons)
  94. @bp.route("/set_main_icon/<mid>/<icon_id>")
  95. @login_required
  96. def set_main_icon(mid, icon_id):
  97. db = get_db()
  98. db.execute("UPDATE member SET main_icon=(?) WHERE id=(?)",(icon_id, mid))
  99. db.commit()
  100. return redirect(url_for("manage.edit", mid=mid))
  101. @bp.route("/add_to_front/<mid>")
  102. @login_required
  103. def add_to_front(mid):
  104. db = get_db()
  105. db.execute("UPDATE member SET front=(?) WHERE id=(?)",(1, mid))
  106. db.commit()
  107. return redirect(url_for('home.full_list'))
  108. @bp.route("/remove_front/<mid>")
  109. @login_required
  110. def remove_front(mid):
  111. db = get_db()
  112. db.execute("UPDATE member SET front=(?) WHERE id=(?)",(0, mid))
  113. db.commit()
  114. return redirect(url_for('home.full_list'))
  115. @bp.route("/add_to_home/<mid>")
  116. @login_required
  117. def add_to_home(mid):
  118. db = get_db()
  119. db.execute("UPDATE member SET homepage=(?) WHERE id=(?)",(1, mid))
  120. db.commit()
  121. return redirect(url_for('home.full_list'))
  122. @bp.route("/remove_home/<mid>")
  123. @login_required
  124. def remove_home(mid):
  125. db = get_db()
  126. db.execute("UPDATE member SET homepage=(?) WHERE id=(?)",(0, mid))
  127. db.commit()
  128. return redirect(url_for('home.full_list'))