dashboards.py 10 KB

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