dashboards.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. from .forms import UPForm, PicForm, CAForm
  10. dashboards = Blueprint('dashboards', __name__)
  11. # Main Pages
  12. @dashboards.route('/', methods=['GET', 'POST'])
  13. def market():
  14. listings = List.query.all()
  15. art = Art.query.all()
  16. return_list = list()
  17. for l in listings:
  18. for a in art:
  19. if l.filehash == a.filehash:
  20. 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])
  21. return render_template('market.html', user=current_user, listings = return_list)
  22. @dashboards.route('/profile', methods=['GET', 'POST'])
  23. @login_required
  24. def profile():
  25. form = UPForm()
  26. form2 = PicForm()
  27. if form2.validate_on_submit():
  28. print('passsing')
  29. f = form2.upload.data
  30. if cf.allowed_file(f.filename):
  31. designated_fn = cf.sanitize(f.filename)
  32. f.save(f'{cf.PROFILEPIC_FOLDER}/{designated_fn}')
  33. dispatch.save_pp(designated_fn)
  34. flash('Updated Profile Picture!', category='success')
  35. else:
  36. print('you dick')
  37. if form.validate_on_submit():
  38. cpasswd = form.cpasswd.data
  39. passwd = form.passwd_1.data
  40. passwd_con = form.passwd_2.data
  41. # password check before update
  42. if passwd and passwd_con and cpasswd:
  43. if passwd == passwd_con and check_password_hash(current_user.password, cpasswd):
  44. npasswd_dbcall = User.query.filter_by(id=current_user.id).first()
  45. npasswd_dbcall.password = generate_password_hash(passwd_con, method='sha256')
  46. db.session.commit()
  47. flash('Updated password!', category='success')
  48. else:
  49. flash('Password update failed!', category='error')
  50. my_art = Art.query.filter_by(owner=current_user.id).all()
  51. my_creation = Art.query.filter_by(creator=current_user.id).all()
  52. return render_template(
  53. 'profile.html',
  54. user = current_user,
  55. my_art = my_art,
  56. my_creation = my_creation,
  57. form = form,
  58. form2 = form2
  59. )
  60. @dashboards.route('/create_art', methods=['GET', 'POST'])
  61. @login_required
  62. def create():
  63. form = CAForm()
  64. # check POST req
  65. if form.validate_on_submit():
  66. new_art = form.upload.data
  67. art_name = form.art_name.data
  68. art_desc = form.art_desc.data
  69. min_price = form.min_price.data
  70. buyout_price = form.buyout_price.data
  71. close_date = form.close_date.data
  72. # this may be redundant now that we have WTForms ###
  73. if tools.check_fields([new_art, art_name, min_price, buyout_price, close_date]):
  74. if new_art and new_art.filename != '' and cf.allowed_file(new_art.filename):
  75. designated_fn = cf.sanitize(new_art.filename)
  76. new_art.save(f'{cf.UPLOAD_FOLDER}/{designated_fn}')
  77. dispatch.mint(designated_fn, art_name, art_desc, min_price, buyout_price, close_date)
  78. return render_template('create_art.html', user = current_user, form = form)
  79. @dashboards.route('/search')
  80. @login_required
  81. def search():
  82. return render_template('search.html', user=current_user)
  83. @dashboards.route('/detail')
  84. @login_required
  85. def detail():
  86. return render_template('detail_art.html', user=current_user)