|
|
@@ -1,14 +1,15 @@
|
|
|
from flask import Blueprint, render_template, request, flash, redirect, url_for
|
|
|
from flask_login import login_required, current_user
|
|
|
from werkzeug.security import generate_password_hash, check_password_hash
|
|
|
-from .models import User, Art, List, Bids
|
|
|
+from .models import User, Art, List, Bids, Wallet, Stripe
|
|
|
from . import db
|
|
|
|
|
|
from . import dispatch
|
|
|
from app.lib import clean_file as cf, tools
|
|
|
from app.lib import collector
|
|
|
+from app.lib import stripe
|
|
|
|
|
|
-from .forms import UPForm, PicForm, CAForm, BidForm
|
|
|
+from .forms import UPForm, PicForm, CAForm, BidForm, WalletForm
|
|
|
|
|
|
dashboards = Blueprint('dashboards', __name__)
|
|
|
|
|
|
@@ -32,8 +33,14 @@ def market():
|
|
|
def profile():
|
|
|
form = UPForm()
|
|
|
form2 = PicForm()
|
|
|
+ form3 = WalletForm()
|
|
|
user_bid_hist = collector.user_bid_hist(current_user.id)
|
|
|
|
|
|
+ # Initializes wallet and fetches amount
|
|
|
+ dispatch.init_wallet(current_user.id)
|
|
|
+ wallet = Wallet.query.filter_by(user_id = current_user.id).first()
|
|
|
+ wallet_amount = float(wallet.amount/100)
|
|
|
+
|
|
|
# This takes a post request button press
|
|
|
# when user clicks on a photo
|
|
|
if request.method == "POST":
|
|
|
@@ -48,9 +55,25 @@ def profile():
|
|
|
flash('Auction page not available to items not for sale!', category='error')
|
|
|
|
|
|
|
|
|
+ # Wallet Top Up Form
|
|
|
+ if form3.validate_on_submit():
|
|
|
+ amount = form3.amount.data
|
|
|
+ raw_amount = int(amount*100) # converting to cents int for Stripe
|
|
|
+
|
|
|
+ if amount: # Send to stripe checkout
|
|
|
+ # render a checkout page for Stripe
|
|
|
+ # card handling
|
|
|
+ return render_template(
|
|
|
+ 'checkout.html',
|
|
|
+ key = stripe.stripe_keys['publishable_key'],
|
|
|
+ user = current_user,
|
|
|
+ amount = amount,
|
|
|
+ ramount = raw_amount
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
# Profile Picture Update Form
|
|
|
if form2.validate_on_submit():
|
|
|
- print('passsing')
|
|
|
f = form2.upload.data
|
|
|
if cf.allowed_file(f.filename):
|
|
|
designated_fn = cf.sanitize(f.filename)
|
|
|
@@ -85,7 +108,9 @@ def profile():
|
|
|
my_creation = my_creation,
|
|
|
form = form,
|
|
|
form2 = form2,
|
|
|
- ubh = user_bid_hist
|
|
|
+ form3 = form3,
|
|
|
+ ubh = user_bid_hist,
|
|
|
+ wallet_amount = wallet_amount
|
|
|
)
|
|
|
|
|
|
@dashboards.route('/create_art', methods=['GET', 'POST'])
|
|
|
@@ -97,7 +122,6 @@ def create():
|
|
|
|
|
|
# check POST req
|
|
|
if form.validate_on_submit():
|
|
|
- print('this passes')
|
|
|
new_art = form.upload.data
|
|
|
art_name = form.art_name.data
|
|
|
art_desc = form.art_desc.data
|
|
|
@@ -159,6 +183,14 @@ def detail():
|
|
|
# New Bid
|
|
|
if form.validate_on_submit():
|
|
|
user_bid = form.price.data
|
|
|
+
|
|
|
+ # first check if user has wallet balance
|
|
|
+ dbc_uwallet = Wallet.query.filter_by(user_id = current_user.id).first()
|
|
|
+ uwealth = float(dbc_uwallet.amount / 100)
|
|
|
+ if not uwealth >= float(user_bid):
|
|
|
+ flash('You don\'t have enough cash in your wallet!', category='error')
|
|
|
+ return redirect(url_for('dashboards.profile'))
|
|
|
+
|
|
|
# checking if bid is at buyout price or more
|
|
|
if user_bid and user_bid >= focus[8]:
|
|
|
dispatch.enter_bid(user_bid, focus)
|
|
|
@@ -183,3 +215,30 @@ def detail():
|
|
|
form = form,
|
|
|
ibh = item_bid_hist
|
|
|
)
|
|
|
+
|
|
|
+@dashboards.route('/charge', methods=['POST'])
|
|
|
+@login_required
|
|
|
+def charge():
|
|
|
+ # Stripe charge POST request method
|
|
|
+
|
|
|
+ # Amount in cents
|
|
|
+ dbc_stripe = Stripe.query.filter_by(user_id = current_user.id).order_by(Stripe.id.desc()).first()
|
|
|
+ raw_amount = dbc_stripe.raw_amount
|
|
|
+ amount = float(raw_amount/100)
|
|
|
+
|
|
|
+ customer = stripe.stripe.Customer.create(
|
|
|
+ email = 'customer@example.com',
|
|
|
+ source = request.form['stripeToken']
|
|
|
+ )
|
|
|
+
|
|
|
+ charge = stripe.stripe.Charge.create(
|
|
|
+ customer = customer.id,
|
|
|
+ amount = raw_amount,
|
|
|
+ currency = 'usd',
|
|
|
+ description = 'Flask Charge'
|
|
|
+ )
|
|
|
+
|
|
|
+ # db dispatch for wallet top up
|
|
|
+ dispatch.top_up(current_user.id, raw_amount)
|
|
|
+
|
|
|
+ return render_template('charge.html', amount = amount, user = current_user)
|