[Flask] Creating table only if its not created for user already

Anonymous Coder anonymouscodar at gmail.com
Tue Mar 7 11:01:13 EST 2017


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/fa3cb392/attachment-0001.html>


More information about the Flask mailing list