dashboards.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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, Bids
  5. from . import db
  6. from . import dispatch
  7. from app.lib import clean_file as cf, tools
  8. from app.lib import collector
  9. from .forms import UPForm, PicForm, CAForm, BidForm
  10. dashboards = Blueprint('dashboards', __name__)
  11. # Main Pages
  12. @dashboards.route('/', methods=['GET', 'POST'])
  13. def market():
  14. return_list = collector.join_art_list_table()
  15. if request.method == "POST":
  16. focus_item = request.form.get('focus_but')
  17. u_dbcall = User.query.filter_by(id=current_user.id).first()
  18. u_dbcall.focus = focus_item
  19. db.session.commit()
  20. return redirect(url_for('dashboards.detail'))
  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. user_bid_hist = collector.user_bid_hist(current_user.id)
  28. if request.method == "POST":
  29. focus_item = request.form.get('focus_but')
  30. if focus_item and collector.check_art_listing(focus_item):
  31. u_dbcall = User.query.filter_by(id=current_user.id).first()
  32. u_dbcall.focus = focus_item
  33. db.session.commit()
  34. return redirect(url_for('dashboards.detail'))
  35. elif focus_item and not collector.check_art_listing(focus_item):
  36. pass
  37. if form2.validate_on_submit():
  38. print('passsing')
  39. f = form2.upload.data
  40. if cf.allowed_file(f.filename):
  41. designated_fn = cf.sanitize(f.filename)
  42. f.save(f'{cf.PROFILEPIC_FOLDER}/{designated_fn}')
  43. dispatch.save_pp(designated_fn)
  44. flash('Updated Profile Picture!', category='success')
  45. if form.validate_on_submit():
  46. cpasswd = form.cpasswd.data
  47. passwd = form.passwd_1.data
  48. passwd_con = form.passwd_2.data
  49. # password check before update
  50. if passwd and passwd_con and cpasswd:
  51. if passwd == passwd_con and check_password_hash(current_user.password, cpasswd):
  52. npasswd_dbcall = User.query.filter_by(id=current_user.id).first()
  53. npasswd_dbcall.password = generate_password_hash(passwd_con, method='sha256')
  54. db.session.commit()
  55. flash('Updated password!', category='success')
  56. else:
  57. flash('Password update failed!', category='error')
  58. my_art = Art.query.filter_by(owner=current_user.id).all()
  59. my_creation = Art.query.filter_by(creator=current_user.id).all()
  60. return render_template(
  61. 'profile.html',
  62. user = current_user,
  63. my_art = my_art,
  64. my_creation = my_creation,
  65. form = form,
  66. form2 = form2,
  67. ubh = user_bid_hist
  68. )
  69. @dashboards.route('/create_art', methods=['GET', 'POST'])
  70. @login_required
  71. def create():
  72. form = CAForm()
  73. available_art = collector.check_listing()
  74. # check POST req
  75. if form.validate_on_submit():
  76. print('this passes')
  77. new_art = form.upload.data
  78. art_name = form.art_name.data
  79. art_desc = form.art_desc.data
  80. min_price = form.min_price.data
  81. buyout_price = form.buyout_price.data
  82. close_date = form.close_date.data
  83. # For minting Art
  84. if tools.check_fields([new_art, art_name, min_price, buyout_price, close_date]):
  85. if new_art and new_art.filename != '' and cf.allowed_file(new_art.filename):
  86. designated_fn = cf.sanitize(new_art.filename)
  87. new_art.save(f'{cf.UPLOAD_FOLDER}/{designated_fn}')
  88. dispatch.mint(designated_fn, art_name, art_desc, min_price, buyout_price, close_date)
  89. flash('Minted & Listed!', category='success')
  90. # For re-Listing Art
  91. if not new_art:
  92. new_art = request.form.get('web_group') # fetch filehash
  93. if new_art:
  94. # get Art obj from filehash
  95. art_obj = collector.get_art_obj(new_art)
  96. # dispatch re-list function
  97. dispatch.list_item(
  98. art_obj.dname,
  99. art_name,
  100. art_desc,
  101. min_price,
  102. buyout_price,
  103. close_date,
  104. art_obj.filehash
  105. )
  106. flash('Listed!', category='success')
  107. return render_template('create_art.html', user = current_user, form = form, av_art = available_art)
  108. @dashboards.route('/search', methods=['GET', 'POST'])
  109. @login_required
  110. def search():
  111. return render_template('search.html', user = current_user)
  112. @dashboards.route('/detail', methods=['GET', 'POST'])
  113. @login_required
  114. def detail():
  115. focus = None
  116. # Collects details of the listing based on the
  117. # focus pointer of the user
  118. # focus is the return of join_art_list_table in collector
  119. return_list = collector.join_art_list_table()
  120. for item in return_list:
  121. if item[11] == current_user.focus: # comparing hash
  122. focus = item
  123. break
  124. owner_obj = collector.find_user_obj(focus[2])
  125. item_bid_hist = collector.item_bid_hist(current_user.focus)
  126. # New Bid
  127. form = BidForm()
  128. if form.validate_on_submit():
  129. user_bid = form.price.data
  130. # checking if bid is at buyout price or more
  131. if user_bid and user_bid >= focus[8]:
  132. dispatch.enter_bid(user_bid, focus)
  133. dispatch.tx_exchange(current_user.focus, focus[6], user_bid)
  134. flash('You Bought this piece out! WOAH!', category='success')
  135. return redirect(url_for('dashboards.profile'))
  136. # checking if bid is higher than minimum bidding price
  137. elif user_bid and user_bid > focus[7]:
  138. dispatch.enter_bid(user_bid, focus)
  139. flash('Bid set! Good luck!', category='success')
  140. else:
  141. flash('Your Bid Price is too low!', category='error')
  142. return redirect(url_for('dashboards.detail'))
  143. return render_template(
  144. 'detail_art.html',
  145. user = current_user,
  146. detail = focus,
  147. own_uname = owner_obj.username,
  148. form = form,
  149. ibh = item_bid_hist
  150. )