dashboards.py 6.7 KB

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