import os from flask import Blueprint, render_template, request, flash, redirect, url_for from flask_login import login_required, current_user from werkzeug.security import generate_password_hash, check_password_hash from werkzeug.utils import secure_filename from .models import User from . import db from . import alphagen as ag dashboards = Blueprint('dashboards', __name__) # Main Pages @dashboards.route('/', methods=['GET', 'POST']) def market(): return render_template('market.html', user=current_user) @dashboards.route('/profile', methods=['GET', 'POST']) @login_required def profile(): # fetch post request if request.method == "POST": cpasswd = request.form.get('current_password') passwd = request.form.get('password') passwd_con = request.form.get('password_confirm') # password check before update if passwd and passwd_con and cpasswd: if passwd == passwd_con and check_password_hash(current_user.password, cpasswd): npasswd_dbcall = User.query.filter_by(id=current_user.id).first() npasswd_dbcall.password = generate_password_hash(passwd_con, method='sha256') db.session.commit() flash('Updated password!', category='success') else: flash('Password update failed!', category='error') return render_template('profile.html', user=current_user) @dashboards.route('/create_art', methods=['GET', 'POST']) #login_required def create(): UPLOAD_FOLDER = 'app/static/incoming' ALLOWED_EXT = {'png', 'jpg', 'jpeg'} # Checks file for allowed extension def allowed_file(filename): return '.' in filename and \ filename.rsplit('.', 1)[1].lower() in ALLOWED_EXT # check POST req if request.method == "POST": new_art = request.files.get('art_img') if new_art and new_art.filename != '' and allowed_file(new_art.filename): sfn = secure_filename(new_art.filename) # strips any slashes ssfn, fx = os.path.splitext(sfn) # ensures that internal filenames are not rsfn = ag.gen_alphanum() # known to users. ffn = f'{rsfn}{fx}' new_art.save(f'{UPLOAD_FOLDER}/{ffn}') return render_template('create_art.html', user=current_user) # Pop Ups @dashboards.route('/modal_home') def modal_home(): return render_template('detail_art_for_home.html', user=current_user) @dashboards.route('/modal_profile') def modal_profile(): return render_template('detail_art_for_profile.html', user=current_user)