dashboards.py 10.0 KB

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