def update(): try: current_plan = current_user.subscription.plan active_plan = Subscription.get_plan_by_id(current_plan) new_plan = Subscription.get_new_plan(request.form.keys()) plan = Subscription.get_plan_by_id(new_plan) # Guard against an invalid, missing or identical plan. is_same_plan = new_plan == active_plan['id'] if ((new_plan is not None and plan is None) or is_same_plan) and \ request.method == 'POST': return redirect(url_for('billing.update')) form = UpdateSubscriptionForm( coupon_code=current_user.subscription.coupon) if form.validate_on_submit(): subscription = Subscription() updated = subscription.update( user=current_user, coupon=request.form.get('coupon_code'), plan=plan.get('id')) if updated: from app.blueprints.billing.billing_functions import change_limits change_limits(current_user, plan, active_plan) from app.blueprints.user.tasks import send_plan_change_email send_plan_change_email.delay(current_user.email, plan) flash( 'Your plan has been updated. Changes will take effect immediately.', 'success') return redirect(url_for('user.dashboard')) return render_template('billing/pricing.html', form=form, plans=settings.STRIPE_PLANS, active_plan=active_plan) except Exception as e: print_traceback(e) flash( 'There was an error. We weren\'t able to change your plan at this time.', 'error') return redirect(url_for('user.dashboard'))
def update(): current_plan = current_user.subscription.plan active_plan = Subscription.get_plan_by_id(current_plan) new_plan = Subscription.get_new_plan(request.form.keys()) plan = Subscription.get_plan_by_id(new_plan) # Guard against an invalid, missing or identical plan. is_same_plan = new_plan == active_plan['id'] if ((new_plan is not None and plan is None) or is_same_plan) and \ request.method == 'POST': return redirect(url_for('billing.update')) form = UpdateSubscriptionForm(coupon_code=current_user.subscription.coupon) if form.validate_on_submit(): subscription = Subscription() updated = subscription.update(user=current_user, coupon=request.form.get('coupon_code'), plan=plan.get('id')) if updated: if new_plan is not None: # Set the user's current mailbox to the oldest mailbox from app.blueprints.parse.models.mailbox import Mailbox mailbox = Mailbox.query.filter( Mailbox.user_email == current_user.email).order_by( Mailbox.created_on.asc()).first() if mailbox: current_user.mailbox_id = mailbox.mailbox_id # Set the mailbox and email limits accordingly mailbox_limit = 10 if new_plan == 'hobby' else 40 if new_plan == 'startup'\ else 60 if new_plan == 'professional' else 100 if new_plan == 'enterprise'\ else 100 if new_plan == 'developer' else 0 email_limit = 400 if new_plan == 'hobby' else 2000 if new_plan == 'startup'\ else 5000 if new_plan == 'professional' else 15000 if new_plan == 'enterprise'\ else 20000 if new_plan == 'developer' else 0 # Adjust the mailboxes by deleting # the most recent mailboxes and emails from app.blueprints.user.tasks import adjust_mailboxes if plan['amount'] < active_plan['amount']: current_user.email_count, current_user.mailbox_count = adjust_mailboxes( current_user.email, current_user.mailbox_id, current_user.email_count, mailbox_limit, email_limit) current_user.mailbox_limit, current_user.email_limit = mailbox_limit, email_limit current_user.save() flash( 'Your plan has been updated. Changes will take effect immediately.', 'success') return redirect(url_for('user.settings')) return render_template('billing/pricing.html', form=form, plans=settings.STRIPE_PLANS, active_plan=active_plan)