app-MAIL-temp/app/dashboard/views/billing.py

39 lines
1.4 KiB
Python
Raw Normal View History

2020-03-08 10:28:13 +01:00
from flask import render_template, flash, redirect, url_for, request
2019-11-14 15:05:20 +01:00
from flask_login import login_required, current_user
from app.dashboard.base import dashboard_bp
2020-03-08 10:28:13 +01:00
from app.log import LOG
from app.models import Subscription
from app.extensions import db
from app.paddle_utils import cancel_subscription
2019-11-14 15:05:20 +01:00
@dashboard_bp.route("/billing", methods=["GET", "POST"])
@login_required
def billing():
# sanity check: make sure this page is only for user who has paddle subscription
2020-03-08 10:28:13 +01:00
sub: Subscription = current_user.get_subscription()
2019-11-14 15:05:20 +01:00
if not sub:
flash("You don't have any active subscription", "warning")
return redirect(url_for("dashboard.index"))
2020-03-08 10:28:13 +01:00
if request.method == "POST":
if request.form.get("form-name") == "cancel":
2020-03-09 09:48:33 +01:00
LOG.warning(f"User {current_user} cancels their subscription")
2020-03-08 10:28:13 +01:00
success = cancel_subscription(sub.subscription_id)
if success:
sub.cancelled = True
db.session.commit()
flash("Your subscription has been canceled successfully", "success")
else:
flash(
"Something went wrong, sorry for the inconvenience. Please retry. We are already notified and will be on it asap",
"error",
)
return redirect(url_for("dashboard.billing"))
2019-11-14 15:05:20 +01:00
return render_template("dashboard/billing.html", sub=sub)