dashboards.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import os
  2. from flask import Blueprint, render_template, request, flash, redirect, url_for
  3. from flask_login import login_required, current_user
  4. from werkzeug.security import generate_password_hash, check_password_hash
  5. from werkzeug.utils import secure_filename
  6. from .models import User
  7. from . import db
  8. from . import alphagen as ag
  9. dashboards = Blueprint('dashboards', __name__)
  10. # Main Pages
  11. @dashboards.route('/', methods=['GET', 'POST'])
  12. def market():
  13. return render_template('market.html', user=current_user)
  14. @dashboards.route('/profile', methods=['GET', 'POST'])
  15. @login_required
  16. def profile():
  17. # fetch post request
  18. if request.method == "POST":
  19. cpasswd = request.form.get('current_password')
  20. passwd = request.form.get('password')
  21. passwd_con = request.form.get('password_confirm')
  22. # password check before update
  23. if passwd and passwd_con and cpasswd:
  24. if passwd == passwd_con and check_password_hash(current_user.password, cpasswd):
  25. npasswd_dbcall = User.query.filter_by(id=current_user.id).first()
  26. npasswd_dbcall.password = generate_password_hash(passwd_con, method='sha256')
  27. db.session.commit()
  28. flash('Updated password!', category='success')
  29. else:
  30. flash('Password update failed!', category='error')
  31. return render_template('profile.html', user=current_user)
  32. @dashboards.route('/create_art', methods=['GET', 'POST'])
  33. #login_required
  34. def create():
  35. UPLOAD_FOLDER = 'app/static/incoming'
  36. ALLOWED_EXT = {'png', 'jpg', 'jpeg'}
  37. # Checks file for allowed extension
  38. def allowed_file(filename):
  39. return '.' in filename and \
  40. filename.rsplit('.', 1)[1].lower() in ALLOWED_EXT
  41. # check POST req
  42. if request.method == "POST":
  43. new_art = request.files.get('art_img')
  44. if new_art and new_art.filename != '' and allowed_file(new_art.filename):
  45. sfn = secure_filename(new_art.filename) # strips any slashes
  46. ssfn, fx = os.path.splitext(sfn) # ensures that internal filenames are not
  47. rsfn = ag.gen_alphanum() # known to users.
  48. ffn = f'{rsfn}{fx}'
  49. new_art.save(f'{UPLOAD_FOLDER}/{ffn}')
  50. return render_template('create_art.html', user=current_user)
  51. # Pop Ups
  52. @dashboards.route('/modal_home')
  53. def modal_home():
  54. return render_template('detail_art_for_home.html', user=current_user)
  55. @dashboards.route('/modal_profile')
  56. def modal_profile():
  57. return render_template('detail_art_for_profile.html', user=current_user)