dashboards.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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, Wallet, Stripe, TX, Hashchain
  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 app.lib import stripe
  10. from .forms import UPForm, PicForm, CAForm, BidForm, WalletForm, SearchForm
  11. dashboards = Blueprint('dashboards', __name__)
  12. # Main Pages
  13. @dashboards.route('/', methods=['GET', 'POST'])
  14. def market():
  15. return_list = collector.join_art_list_table()
  16. seform = SearchForm()
  17. if request.method == "POST":
  18. focus_item = request.form.get('focus_but')
  19. if focus_item and current_user.is_authenticated:
  20. u_dbcall = User.query.filter_by(id=current_user.id).first()
  21. u_dbcall.focus = focus_item
  22. db.session.commit()
  23. return redirect(url_for('dashboards.detail'))
  24. return render_template('market.html', user=current_user, listings = return_list, seform = seform)
  25. @dashboards.route('/profile', methods=['GET', 'POST'])
  26. @login_required
  27. def profile():
  28. form = UPForm()
  29. form2 = PicForm()
  30. form3 = WalletForm()
  31. user_bid_hist = collector.user_bid_hist(current_user.id)
  32. seform = SearchForm()
  33. # Initializes wallet and fetches amount
  34. dispatch.init_wallet(current_user.id)
  35. wallet = Wallet.query.filter_by(user_id = current_user.id).first()
  36. wallet_amount = float(wallet.amount/100)
  37. # This takes a post request button press
  38. # when user clicks on a photo
  39. if request.method == "POST":
  40. focus_item = request.form.get('focus_but')
  41. if focus_item and collector.check_art_listing(focus_item):
  42. u_dbcall = User.query.filter_by(id=current_user.id).first()
  43. u_dbcall.focus = focus_item
  44. db.session.commit()
  45. return redirect(url_for('dashboards.detail'))
  46. elif focus_item and not collector.check_art_listing(focus_item):
  47. # else if there's a click but no listing, don't do anything
  48. flash('Auction page not available to items not for sale!', category='error')
  49. # Wallet Top Up Form
  50. if form3.validate_on_submit():
  51. amount = form3.amount.data
  52. raw_amount = int(amount*100) # converting to cents int for Stripe
  53. # save to db
  54. dispatch.save_tx(current_user.id, raw_amount)
  55. if amount: # Send to stripe checkout
  56. # render a checkout page for Stripe
  57. # card handling
  58. return render_template(
  59. 'checkout.html',
  60. key = stripe.stripe_keys['publishable_key'],
  61. user = current_user,
  62. amount = amount,
  63. ramount = raw_amount
  64. )
  65. # Profile Picture Update Form
  66. if form2.validate_on_submit():
  67. f = form2.upload.data
  68. if cf.allowed_file(f.filename):
  69. designated_fn = cf.sanitize(f.filename)
  70. f.save(f'{cf.PROFILEPIC_FOLDER}/{designated_fn}')
  71. dispatch.save_pp(designated_fn)
  72. flash('Updated Profile Picture!', category='success')
  73. # Password Update Form
  74. if form.validate_on_submit():
  75. cpasswd = form.cpasswd.data
  76. passwd = form.passwd_1.data
  77. passwd_con = form.passwd_2.data
  78. # Basic password checks before adding to db
  79. if passwd and passwd_con and cpasswd:
  80. if passwd == passwd_con and check_password_hash(current_user.password, cpasswd):
  81. npasswd_dbcall = User.query.filter_by(id=current_user.id).first()
  82. npasswd_dbcall.password = generate_password_hash(passwd_con, method='sha256')
  83. db.session.commit()
  84. flash('Updated password!', category='success')
  85. else:
  86. flash('Password update failed!', category='error')
  87. my_art = Art.query.filter_by(owner=current_user.id).all()
  88. my_creation = Art.query.filter_by(creator=current_user.id).all()
  89. return render_template(
  90. 'profile.html',
  91. user = current_user,
  92. my_art = my_art,
  93. my_creation = my_creation,
  94. form = form,
  95. form2 = form2,
  96. form3 = form3,
  97. ubh = user_bid_hist,
  98. wallet_amount = wallet_amount,
  99. seform = seform
  100. )
  101. @dashboards.route('/create_art', methods=['GET', 'POST'])
  102. @login_required
  103. def create():
  104. form = CAForm()
  105. available_art = collector.check_listing()
  106. seform = SearchForm()
  107. # check POST req
  108. if form.validate_on_submit():
  109. new_art = form.upload.data
  110. art_name = form.art_name.data
  111. art_desc = form.art_desc.data
  112. min_price = form.min_price.data
  113. buyout_price = form.buyout_price.data
  114. close_date = form.close_date.data
  115. # For minting Art
  116. if tools.check_fields([new_art, art_name, min_price, buyout_price, close_date]):
  117. if new_art and new_art.filename != '' and cf.allowed_file(new_art.filename):
  118. designated_fn = cf.sanitize(new_art.filename)
  119. new_art.save(f'{cf.UPLOAD_FOLDER}/{designated_fn}')
  120. dispatch.mint(designated_fn, art_name, art_desc, min_price, buyout_price, close_date)
  121. # For re-Listing Art
  122. if not new_art:
  123. new_art = request.form.get('web_group') # fetch filehash
  124. if new_art:
  125. # get Art obj from filehash
  126. art_obj = collector.get_art_obj(new_art)
  127. # dispatch re-list function
  128. dispatch.list_item(
  129. art_obj.dname,
  130. art_name,
  131. art_desc,
  132. min_price,
  133. buyout_price,
  134. close_date,
  135. art_obj.filehash
  136. )
  137. flash('Listed!', category='success')
  138. return render_template('create_art.html', user = current_user, form = form, av_art = available_art, seform = seform)
  139. @dashboards.route('/search', methods=['GET', 'POST'])
  140. @login_required
  141. def search():
  142. ##:
  143. seform = SearchForm()
  144. ##BUG: this may need to be on every view function for it to work...
  145. if seform.validate_on_submit():
  146. searchterm = seform.searchterm.data
  147. if searchterm:
  148. search_result = collector.search_art_objn(searchterm)
  149. if search_result:
  150. return render_template('search.html', user = current_user, seform = seform, sr = search_result)
  151. search_result = collector.search_art_objc(searchterm)
  152. if search_result:
  153. return render_template('search.html', user = current_user, seform = seform, sr = search_result)
  154. return render_template('search.html', user = current_user, seform = seform)
  155. @dashboards.route('/detail', methods=['GET', 'POST'])
  156. @login_required
  157. def detail():
  158. focus = None
  159. form = BidForm()
  160. seform = SearchForm()
  161. # Collects details of the listing based on the
  162. # focus pointer of the user
  163. # focus is the return of join_art_list_table in collector
  164. return_list = collector.join_art_list_table()
  165. for item in return_list:
  166. if item[11] == current_user.focus: # comparing hash
  167. focus = item
  168. break
  169. owner_obj = collector.find_user_obj(focus[2])
  170. item_bid_hist = collector.item_bid_hist(current_user.focus)
  171. # New Bid
  172. if form.validate_on_submit():
  173. user_bid = form.price.data
  174. # first check if user has wallet balance
  175. dbc_uwallet = Wallet.query.filter_by(user_id = current_user.id).first()
  176. uwealth = float(dbc_uwallet.amount / 100)
  177. if not uwealth >= float(user_bid):
  178. flash('You don\'t have enough cash in your wallet!', category='error')
  179. return redirect(url_for('dashboards.profile'))
  180. # checking if bid is at buyout price or more
  181. if user_bid and user_bid >= focus[8]:
  182. dispatch.enter_bid(user_bid, focus)
  183. dispatch.tx_exchange(current_user.focus, focus[6], user_bid)
  184. dispatch.clean_bid_table(current_user.focus)
  185. flash('You Bought this piece out! Congratulations!', category='success')
  186. return redirect(url_for('dashboards.profile'))
  187. # checking if bid is higher than minimum bidding price
  188. elif user_bid and user_bid > focus[7]:
  189. dispatch.enter_bid(user_bid, focus)
  190. flash('Bid set! Good luck!', category='success')
  191. else:
  192. flash('Your Bid Price is too low!', category='error')
  193. return redirect(url_for('dashboards.detail'))
  194. return render_template(
  195. 'detail_art.html',
  196. user = current_user,
  197. detail = focus,
  198. own_uname = owner_obj.username,
  199. form = form,
  200. ibh = item_bid_hist,
  201. seform = seform
  202. )
  203. @dashboards.route('/charge', methods=['POST'])
  204. @login_required
  205. def charge():
  206. seform = SearchForm()
  207. # Stripe charge POST request method
  208. # Amount in cents
  209. dbc_stripe = Stripe.query.filter_by(user_id = current_user.id).order_by(Stripe.id.desc()).first()
  210. raw_amount = dbc_stripe.raw_amount
  211. customer = stripe.stripe.Customer.create(
  212. email = 'customer@example.com',
  213. source = request.form['stripeToken']
  214. )
  215. charge = stripe.stripe.Charge.create(
  216. customer = customer.id,
  217. amount = raw_amount,
  218. currency = 'usd',
  219. description = 'Flask Charge'
  220. )
  221. # db dispatch for wallet top up
  222. dispatch.top_up(current_user.id, raw_amount)
  223. return render_template('charge.html', user = current_user, seform = seform)
  224. @dashboards.route('/hashchain', methods=['GET'])
  225. def hashchain():
  226. hashchain = Hashchain.query.all()
  227. txlist = TX.query.all()
  228. seform = SearchForm()
  229. return render_template('hash.html', user = current_user, hashchain = hashchain, txlist = txlist, seform = seform)