dashboards.py 8.7 KB

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