dashboards.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from flask import Blueprint, render_template, request, flash, redirect, url_for
  2. from flask_login import login_required, current_user
  3. from werkzeug.security import generate_password_hash, check_password_hash
  4. from .models import User, Art, List
  5. from . import db
  6. from . import dispatch
  7. from app.lib import clean_file as cf
  8. from app.lib import tools
  9. dashboards = Blueprint('dashboards', __name__)
  10. # Main Pages
  11. @dashboards.route('/', methods=['GET', 'POST'])
  12. def market():
  13. listings = List.query.all()
  14. art = Art.query.all()
  15. return_list = list()
  16. for l in listings:
  17. for a in art:
  18. if l.filehash == a.filehash:
  19. 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])
  20. return render_template('market.html', user=current_user, listings = return_list)
  21. @dashboards.route('/profile', methods=['GET', 'POST'])
  22. @login_required
  23. def profile():
  24. # fetch post request
  25. if request.method == "POST":
  26. cpasswd = request.form.get('current_password')
  27. passwd = request.form.get('password')
  28. passwd_con = request.form.get('password_confirm')
  29. # password check before update
  30. if passwd and passwd_con and cpasswd:
  31. if passwd == passwd_con and check_password_hash(current_user.password, cpasswd):
  32. npasswd_dbcall = User.query.filter_by(id=current_user.id).first()
  33. npasswd_dbcall.password = generate_password_hash(passwd_con, method='sha256')
  34. db.session.commit()
  35. flash('Updated password!', category='success')
  36. else:
  37. flash('Password update failed!', category='error')
  38. my_art = Art.query.filter_by(owner=current_user.id).all()
  39. my_creation = Art.query.filter_by(creator=current_user.id).all()
  40. return render_template('profile.html', user = current_user, my_art = my_art, my_creation = my_creation)
  41. @dashboards.route('/create_art', methods=['GET', 'POST'])
  42. @login_required
  43. def create():
  44. # check POST req
  45. if request.method == "POST":
  46. new_art = request.files.get('art_img')
  47. art_name = request.form.get('art_name')
  48. art_desc = request.form.get('description')
  49. min_price = request.form.get('minimum_price')
  50. buyout_price = request.form.get('buyout_price')
  51. close_date = request.form.get('closing_date')
  52. if tools.check_fields([new_art, art_name, min_price, buyout_price, close_date]):
  53. if new_art and new_art.filename != '' and cf.allowed_file(new_art.filename):
  54. designated_fn = cf.sanitize(new_art.filename)
  55. new_art.save(f'{cf.UPLOAD_FOLDER}/{designated_fn}')
  56. dispatch.mint(designated_fn, art_name, art_desc, min_price, buyout_price, close_date)
  57. return render_template('create_art.html', user=current_user)
  58. # Pop Ups
  59. @dashboards.route('/modal_home')
  60. @login_required
  61. def modal_home():
  62. return render_template('detail_art_for_market.html', user=current_user)
  63. @dashboards.route('/modal_profile')
  64. @login_required
  65. def modal_profile():
  66. return render_template('detail_art_for_profile.html', user=current_user)