[Flask] Creating table only if its not created for user already
Anonymous Coder
anonymouscodar at gmail.com
Tue Mar 7 12:30:39 EST 2017
Thanks for help but all I am trying to do here is create a udemy clone with
some changes I would like my solution to have. Its not a job I am doing it
for myself. I like python and that's why I chose Flask.
For your comment above, official documentation sucks that's why I have no
other option as this is my very first project.
Thanks for help though, I will try it.
Cheers.
On Tue, Mar 7, 2017 at 4:21 PM, Ziirish <ziirish at ziirish.info> wrote:
> It would be better if you showed us some of your tries and/or error
> messages.
> Regarding the latest emails you sent it looks like you want us to do your
> job...
>
> Anyway, the "if" you are looking for is probably something like:
>
>
> package = Package.query.filter_by(
> student_id=student_id, stripe_id = customer.id,
> student_email=request.form['stripeEmail'], is_active=True,
> package_type='yearlyrec', subscription_id=subscription.id
> ).first()
>
> if not package:
> package = Package(...)
> dbase.session.add(package)
> dbase.session.commit()
>
>
>
> * On Tuesday, March 07, 2017 at 04:01 PM +0000, Anonymous Coder <
> anonymouscodar at gmail.com> wrote:
> > I was trying to find a way to process/commit a model table for view only
> if
> > it doesn't exists already. But I couldn't find the way to do it. I tried
> > if/else but it doesn't create model at all if I try it that way.
> Following
> > is view and I need to create sqlalchymy table only if it doesn't exist
> > alrady.
> >
> > @app.route('/yearlychargedrec', methods=['GET', 'POST'])
> > def yearly_charged_rec():
> > if not user_authorized():
> > return redirect('/')
> >
> > current_package = Package.query.all()
> >
> > if request.method == 'POST':
> > data = get_profile_data(session['auth_token'])
> > profile_data = data['StudentProfile']
> > student = get_profile_data(session['
> auth_token'])['StudentProfile']
> >
> > 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",
> > )
> > student_id = profile_data.id
> > student.stripe_customer_id = customer.id
> > student.stripe_subscription_id = subscription.id
> >
> > package = Package(
> > student_id=student_id,
> > stripe_id = customer.id,
> > student_email=request.form['stripeEmail'],
> > is_active=True,
> > package_type='yearlyrec',
> > subscription_id=subscription.id
> > )
> > dbase.session.add(package)
> > dbase.session.commit()
> >
> > except stripe.error.CardError as e:
> > body = e.json_body
> > err = body['error']
> >
> > return render_template('/profile/charge/monthlycharge.html',
> > current_package=current_package)
> >
> > Please advise.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20170307/387926ef/attachment.html>
More information about the Flask
mailing list