2021-07-13 08:54:37 +02:00
|
|
|
import arrow
|
2021-11-26 18:11:03 +01:00
|
|
|
from flask import render_template, flash, redirect, url_for, request
|
2021-07-13 08:54:37 +02:00
|
|
|
from flask_login import login_required, current_user
|
|
|
|
from flask_wtf import FlaskForm
|
|
|
|
from wtforms import StringField, validators
|
|
|
|
|
2021-11-22 16:30:27 +01:00
|
|
|
from app.config import ADMIN_EMAIL, PADDLE_VENDOR_ID, PADDLE_COUPON_ID
|
2021-07-13 08:54:37 +02:00
|
|
|
from app.dashboard.base import dashboard_bp
|
2021-10-12 14:36:47 +02:00
|
|
|
from app.db import Session
|
2021-07-13 08:54:37 +02:00
|
|
|
from app.email_utils import send_email
|
2021-11-12 17:53:56 +01:00
|
|
|
from app.log import LOG
|
2021-08-21 16:04:32 +02:00
|
|
|
from app.models import (
|
|
|
|
ManualSubscription,
|
|
|
|
Coupon,
|
|
|
|
Subscription,
|
|
|
|
AppleSubscription,
|
|
|
|
CoinbaseSubscription,
|
2021-11-12 17:53:56 +01:00
|
|
|
LifetimeCoupon,
|
2021-08-21 16:04:32 +02:00
|
|
|
)
|
2021-07-13 08:54:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
class CouponForm(FlaskForm):
|
|
|
|
code = StringField("Coupon Code", validators=[validators.DataRequired()])
|
|
|
|
|
|
|
|
|
|
|
|
@dashboard_bp.route("/coupon", methods=["GET", "POST"])
|
|
|
|
@login_required
|
|
|
|
def coupon_route():
|
2021-11-12 17:53:56 +01:00
|
|
|
coupon_form = CouponForm()
|
|
|
|
|
|
|
|
if coupon_form.validate_on_submit():
|
|
|
|
code = coupon_form.code.data
|
|
|
|
if LifetimeCoupon.get_by(code=code):
|
|
|
|
LOG.d("redirect %s to lifetime page instead", current_user)
|
|
|
|
flash("Redirect to the lifetime coupon page instead", "success")
|
|
|
|
return redirect(url_for("dashboard.lifetime_licence"))
|
|
|
|
|
2021-08-21 16:04:32 +02:00
|
|
|
# handle case user already has an active subscription via another channel (Paddle, Apple, etc)
|
|
|
|
can_use_coupon = True
|
|
|
|
|
2021-07-13 08:54:37 +02:00
|
|
|
if current_user.lifetime:
|
2021-08-21 16:04:32 +02:00
|
|
|
can_use_coupon = False
|
2021-07-13 08:54:37 +02:00
|
|
|
|
2021-08-21 16:04:32 +02:00
|
|
|
sub: Subscription = current_user.get_subscription()
|
|
|
|
if sub:
|
|
|
|
can_use_coupon = False
|
|
|
|
|
|
|
|
apple_sub: AppleSubscription = AppleSubscription.get_by(user_id=current_user.id)
|
|
|
|
if apple_sub and apple_sub.is_valid():
|
|
|
|
can_use_coupon = False
|
|
|
|
|
|
|
|
coinbase_subscription: CoinbaseSubscription = CoinbaseSubscription.get_by(
|
|
|
|
user_id=current_user.id
|
|
|
|
)
|
|
|
|
if coinbase_subscription and coinbase_subscription.is_active():
|
|
|
|
can_use_coupon = False
|
|
|
|
|
2021-07-13 08:54:37 +02:00
|
|
|
if coupon_form.validate_on_submit():
|
|
|
|
code = coupon_form.code.data
|
|
|
|
|
|
|
|
coupon: Coupon = Coupon.get_by(code=code)
|
|
|
|
if coupon and not coupon.used:
|
2021-11-26 18:11:03 +01:00
|
|
|
if coupon.expires_date and coupon.expires_date < arrow.now():
|
|
|
|
flash(
|
|
|
|
f"The coupon was expired on {coupon.expires_date.humanize()}",
|
|
|
|
"error",
|
|
|
|
)
|
|
|
|
return redirect(request.url)
|
|
|
|
|
2021-07-13 08:54:37 +02:00
|
|
|
coupon.used_by_user_id = current_user.id
|
|
|
|
coupon.used = True
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2021-07-13 08:54:37 +02:00
|
|
|
|
|
|
|
manual_sub: ManualSubscription = ManualSubscription.get_by(
|
|
|
|
user_id=current_user.id
|
|
|
|
)
|
|
|
|
if manual_sub:
|
|
|
|
# renew existing subscription
|
|
|
|
if manual_sub.end_at > arrow.now():
|
|
|
|
manual_sub.end_at = manual_sub.end_at.shift(years=coupon.nb_year)
|
|
|
|
else:
|
|
|
|
manual_sub.end_at = arrow.now().shift(years=coupon.nb_year, days=1)
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2021-07-13 08:54:37 +02:00
|
|
|
flash(
|
|
|
|
f"Your current subscription is extended to {manual_sub.end_at.humanize()}",
|
|
|
|
"success",
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
ManualSubscription.create(
|
|
|
|
user_id=current_user.id,
|
|
|
|
end_at=arrow.now().shift(years=coupon.nb_year, days=1),
|
|
|
|
comment="using coupon code",
|
2021-09-07 15:36:19 +02:00
|
|
|
is_giveaway=coupon.is_giveaway,
|
2021-07-13 08:54:37 +02:00
|
|
|
commit=True,
|
|
|
|
)
|
|
|
|
flash(
|
|
|
|
f"Your account has been upgraded to Premium, thanks for your support!",
|
|
|
|
"success",
|
|
|
|
)
|
|
|
|
|
|
|
|
# notify admin
|
2021-09-07 15:36:19 +02:00
|
|
|
if coupon.is_giveaway:
|
|
|
|
subject = f"User {current_user} applies a (free) coupon"
|
|
|
|
else:
|
2021-11-19 16:40:52 +01:00
|
|
|
subject = f"User {current_user} applies a (paid, {coupon.comment or ''}) coupon"
|
2021-07-13 08:54:37 +02:00
|
|
|
send_email(
|
|
|
|
ADMIN_EMAIL,
|
2021-09-07 15:36:19 +02:00
|
|
|
subject=subject,
|
2021-07-13 08:54:37 +02:00
|
|
|
plaintext="",
|
|
|
|
html="",
|
|
|
|
)
|
|
|
|
|
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
|
|
|
else:
|
|
|
|
flash(f"Code *{code}* expired or invalid", "warning")
|
|
|
|
|
2021-11-22 16:30:27 +01:00
|
|
|
return render_template(
|
|
|
|
"dashboard/coupon.html",
|
|
|
|
coupon_form=coupon_form,
|
|
|
|
PADDLE_VENDOR_ID=PADDLE_VENDOR_ID,
|
|
|
|
PADDLE_COUPON_ID=PADDLE_COUPON_ID,
|
|
|
|
can_use_coupon=can_use_coupon,
|
2021-11-26 18:20:39 +01:00
|
|
|
# a coupon is only valid until this date
|
|
|
|
# this is to avoid using the coupon to renew an account forever
|
|
|
|
max_coupon_date=arrow.now().shift(years=1, days=-1),
|
2021-11-22 16:30:27 +01:00
|
|
|
)
|