2020-01-03 22:40:44 +01:00
|
|
|
from flask import render_template, flash, redirect, url_for
|
|
|
|
from flask_login import login_required, current_user
|
|
|
|
from flask_wtf import FlaskForm
|
|
|
|
from wtforms import StringField, validators
|
|
|
|
|
2020-04-25 11:30:09 +02:00
|
|
|
from app.config import ADMIN_EMAIL
|
2020-01-03 22:40:44 +01:00
|
|
|
from app.dashboard.base import dashboard_bp
|
2021-10-12 14:36:47 +02:00
|
|
|
from app.db import Session
|
2020-01-03 22:40:44 +01:00
|
|
|
from app.email_utils import send_email
|
|
|
|
from app.models import LifetimeCoupon
|
|
|
|
|
|
|
|
|
|
|
|
class CouponForm(FlaskForm):
|
|
|
|
code = StringField("Coupon Code", validators=[validators.DataRequired()])
|
|
|
|
|
|
|
|
|
|
|
|
@dashboard_bp.route("/lifetime_licence", methods=["GET", "POST"])
|
|
|
|
@login_required
|
|
|
|
def lifetime_licence():
|
2020-04-11 10:47:32 +02:00
|
|
|
if current_user.lifetime:
|
|
|
|
flash("You already have a lifetime licence", "warning")
|
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
|
|
|
# user needs to cancel active subscription first
|
|
|
|
# to avoid being charged
|
|
|
|
sub = current_user.get_subscription()
|
|
|
|
if sub and not sub.cancelled:
|
|
|
|
flash("Please cancel your current subscription first", "warning")
|
2020-01-03 22:40:44 +01:00
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
|
|
|
coupon_form = CouponForm()
|
|
|
|
|
|
|
|
if coupon_form.validate_on_submit():
|
|
|
|
code = coupon_form.code.data
|
|
|
|
|
2020-10-21 19:31:25 +02:00
|
|
|
coupon: LifetimeCoupon = LifetimeCoupon.get_by(code=code)
|
2020-01-03 22:40:44 +01:00
|
|
|
if coupon and coupon.nb_used > 0:
|
|
|
|
coupon.nb_used -= 1
|
|
|
|
current_user.lifetime = True
|
2021-10-04 17:14:34 +02:00
|
|
|
current_user.lifetime_coupon_id = coupon.id
|
2020-10-21 19:31:25 +02:00
|
|
|
if coupon.paid:
|
|
|
|
current_user.paid_lifetime = True
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-01-03 22:40:44 +01:00
|
|
|
|
|
|
|
# notify admin
|
|
|
|
send_email(
|
|
|
|
ADMIN_EMAIL,
|
2020-06-03 21:37:44 +02:00
|
|
|
subject=f"User {current_user} used lifetime coupon. Coupon nb_used: {coupon.nb_used}",
|
2020-01-03 22:40:44 +01:00
|
|
|
plaintext="",
|
|
|
|
html="",
|
|
|
|
)
|
|
|
|
|
|
|
|
flash("You are upgraded to lifetime premium!", "success")
|
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
|
|
|
else:
|
|
|
|
flash(f"Code *{code}* expired or invalid", "warning")
|
|
|
|
|
|
|
|
return render_template("dashboard/lifetime_licence.html", coupon_form=coupon_form)
|