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 .models import User, Art, List from . import db from . import dispatch from app.lib import clean_file as cf from app.lib import tools from .forms import UPForm, PicForm, CAForm dashboards = Blueprint('dashboards', __name__) # Main Pages @dashboards.route('/', methods=['GET', 'POST']) def market(): listings = List.query.all() art = Art.query.all() return_list = list() for l in listings: for a in art: if l.filehash == a.filehash: return_list.append([a.name, a.description, a.owner, a.creator, a.dname, l.seller, l.min_price, l.out_price, l.timeout, l.list_date]) return render_template('market.html', user=current_user, listings = return_list) @dashboards.route('/profile', methods=['GET', 'POST']) @login_required def profile(): form = UPForm() form2 = PicForm() if form2.validate_on_submit(): print('passsing') f = form2.upload.data if cf.allowed_file(f.filename): designated_fn = cf.sanitize(f.filename) f.save(f'{cf.PROFILEPIC_FOLDER}/{designated_fn}') dispatch.save_pp(designated_fn) flash('Updated Profile Picture!', category='success') else: print('you dick') if form.validate_on_submit(): cpasswd = form.cpasswd.data passwd = form.passwd_1.data passwd_con = form.passwd_2.data # 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') my_art = Art.query.filter_by(owner=current_user.id).all() my_creation = Art.query.filter_by(creator=current_user.id).all() return render_template( 'profile.html', user = current_user, my_art = my_art, my_creation = my_creation, form = form, form2 = form2 ) @dashboards.route('/create_art', methods=['GET', 'POST']) @login_required def create(): form = CAForm() owned_art = Art.query.filter_by(owner=current_user.id).all() listed_art = List.query.filter_by(seller=current_user.id).all() available_art = list() # to remove art that is already listed available_art = [art for art in owned_art if art.filehash not in listed_art] print(listed_art) print(available_art) # check POST req if form.validate_on_submit(): new_art = form.upload.data art_name = form.art_name.data art_desc = form.art_desc.data min_price = form.min_price.data buyout_price = form.buyout_price.data close_date = form.close_date.data # this may be redundant now that we have WTForms ### if tools.check_fields([new_art, art_name, min_price, buyout_price, close_date]): if new_art and new_art.filename != '' and cf.allowed_file(new_art.filename): designated_fn = cf.sanitize(new_art.filename) new_art.save(f'{cf.UPLOAD_FOLDER}/{designated_fn}') dispatch.mint(designated_fn, art_name, art_desc, min_price, buyout_price, close_date) # elif here return render_template('create_art.html', user = current_user, form = form, av_art = available_art) # Pop Ups @dashboards.route('/modal_home') @login_required def modal_home(): return render_template('detail_art_for_market.html', user=current_user) @dashboards.route('/modal_profile') @login_required def modal_profile(): return render_template('detail_art_for_profile.html', user=current_user) @dashboards.route('/modal_search') @login_required def modal_search(): return render_template('detail_art_for_search.html', user=current_user)