[Flask] Deleting Flask tables conditionally

Anonymous Coder anonymouscodar at gmail.com
Sun Mar 5 06:55:31 EST 2017


Hi,
I've created a view and I attached my table creation with subscription. Now
I can create a table with subscription but I can't delete same table if
subscription is cancelled.

Following are my views for creating subscription and adding subscription
data to model table successfully.

@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')

Now following is the view where I need to delete table if user choose to
delete the subscription.

@app.route('/cancelannualsub', methods=['GET', 'POST'])
def cancel_yearly_rec():
    if not user_authorized():
        return redirect('/')

    data = get_profile_data(session['auth_token'])
    profile_data = data['StudentProfile']
    student_id = profile_data.id
    package = Package()
    subscription_id = package.subscription_id

    if request.method == 'POST':
        if student_id in package:
            key = stripe_keys['publishable_key']
            subscription = stripe.Subscription.retrieve(subscription_id)
            subscription.delete()
            package = Package()
            dbase.session.delete(package)
            dbase.session.commit()


    return redirect(url_for('new_template'))

Problem is deletion is not deleting table in fact no impact on anything at
all.

Please advise.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20170305/0a7292ce/attachment.html>


More information about the Flask mailing list