dashboards.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. owned_art = Art.query.filter_by(owner=current_user.id).all()
  65. listed_art = List.query.filter_by(seller=current_user.id).all()
  66. available_art = list()
  67. # to remove art that is already listed
  68. available_art = [art for art in owned_art if art.filehash not in listed_art]
  69. print(listed_art)
  70. print(available_art)
  71. # check POST req
  72. if form.validate_on_submit():
  73. new_art = form.upload.data
  74. art_name = form.art_name.data
  75. art_desc = form.art_desc.data
  76. min_price = form.min_price.data
  77. buyout_price = form.buyout_price.data
  78. close_date = form.close_date.data
  79. # this may be redundant now that we have WTForms ###
  80. if tools.check_fields([new_art, art_name, min_price, buyout_price, close_date]):
  81. if new_art and new_art.filename != '' and cf.allowed_file(new_art.filename):
  82. designated_fn = cf.sanitize(new_art.filename)
  83. new_art.save(f'{cf.UPLOAD_FOLDER}/{designated_fn}')
  84. dispatch.mint(designated_fn, art_name, art_desc, min_price, buyout_price, close_date)
  85. # elif here
  86. return render_template('create_art.html', user = current_user, form = form, av_art = available_art)
  87. # Pop Ups
  88. @dashboards.route('/modal_home')
  89. @login_required
  90. def modal_home():
  91. return render_template('detail_art_for_market.html', user=current_user)
  92. @dashboards.route('/modal_profile')
  93. @login_required
  94. def modal_profile():
  95. return render_template('detail_art_for_profile.html', user=current_user)
  96. @dashboards.route('/modal_search')
  97. @login_required
  98. def modal_search():
  99. return render_template('detail_art_for_search.html', user=current_user)