[Flask] DB Updates based on events
Anonymous Coder
anonymouscodar at gmail.com
Fri Feb 24 21:27:40 EST 2017
Hi,
I've created a view for stripe payments. Now upon success I need to update
my local database table. I am new to Flask so I have no clue how to do it
plus there are multiple ways I've tried and couldn't make it work. It is
like sub model attached to parent model with ForeignKey, What I need is to
update Package field in database upon I commit to child table
automatically. Following is my view and Model:
View:
@app.route('/yearlyrecurring')
def subscriptions_yearly_recurring():
if not user_authorized():
return redirect('/')
key = stripe_keys['publishable_key']
charge_all = stripe.Charge.list(limit=895)
charge_dic = {}
charge_list = []
for charge_data in charge_all:
charge_dic['Amount'] = "$" + str(float(charge_data.amount) / 100) +
" " + charge_data.currency.upper()
charge_dic['Description'] = charge_data.description
charge_dic['Name'] = charge_data.receipt_email
charge_dic['Date'] =
str(datetime.datetime.fromtimestamp(charge_data.created))
charge_list.append(charge_dic)
charge_dic = {}
return render_template('/profile/packages/annualrecurring.html',
**locals())
@app.route('/yearlychargedrec', methods=['POST'])
def yearly_charged_rec():
if not user_authorized():
return redirect('/')
# customer
stripe_token = request.form['stripeToken']
email = request.form['stripeEmail']
customer = stripe.Customer.create(
email=email,
source=request.form['stripeToken']
)
try:
subscription = stripe.Subscription.create(
customer=customer.id,
plan="yearlyrec",
)
except stripe.error.CardError as e:
# The card has been declined
body = e.json_body
err = body['error']
return render_template('/profile/charge/monthlycharge.html')
My Model:
#Parent model
Student(db.Model):
......
package = relationship('Package', backref=backref('student"))
......
Packages(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
student_id = db.Column(db.Integer, ForeignKey('student_profile.id'))
stripe_id = db.Column(db.String(45))
student_email = db.Column(db.String(20))
subscription_date = db.Column(db.DateTime, default=today)
expiry_date = db.Column(db.DateTime, default=deadline)
is_active = db.Column(db.Boolean, default=True)
planname = relationship('Plan', backref=backref('package'))
package_price = db.Column(db.Integer)
coupon = db.Column(db.String(12))
def __init__(self, id,
student_id,
stripe_id,
student_email,
subscription_date,
expiry_date,
is_active,
planname,
package_price,
coupon):
self.id = id
self.stripe_id = stripe_id
self.student_id = student_id
self.student_email = student_email
self.subscription_date = subscription_date
self.expiry_date = expiry_date
self.is_active = is_active
self.planname = planname
self.package_price = package_price
self.coupon = coupon
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20170225/0e59387a/attachment.html>
More information about the Flask
mailing list