From 3c05230bd32d5b77c7c677fb899a13c20ce78242 Mon Sep 17 00:00:00 2001 From: Son NK Date: Wed, 1 Jan 2020 20:04:39 +0100 Subject: [PATCH] User can use lifetime coupon --- .../templates/dashboard/pricing.html | 20 ++++++++++ app/dashboard/views/pricing.py | 37 +++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/app/dashboard/templates/dashboard/pricing.html b/app/dashboard/templates/dashboard/pricing.html index b1b0b0db..b7306dd0 100644 --- a/app/dashboard/templates/dashboard/pricing.html +++ b/app/dashboard/templates/dashboard/pricing.html @@ -52,6 +52,26 @@ Yearly
$29.99/year + +
+ +
+ {{ coupon_form.csrf_token }} + + +
Coupon
+
+ If you have a lifetime coupon, please paste it here.
+ For information, we offer free premium account for education (student, professor or technical staff working at + an educational institute).
+ Drop us an email at hi@simplelogin.io with your student ID or certificate to get the coupon. +
+ + + {{ coupon_form.code(class="form-control", placeholder="Coupon") }} + {{ render_field_errors(coupon_form.code) }} + +
diff --git a/app/dashboard/views/pricing.py b/app/dashboard/views/pricing.py index d29891bc..aa74ea34 100644 --- a/app/dashboard/views/pricing.py +++ b/app/dashboard/views/pricing.py @@ -1,13 +1,23 @@ 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 from app.config import ( PADDLE_VENDOR_ID, PADDLE_MONTHLY_PRODUCT_ID, PADDLE_YEARLY_PRODUCT_ID, URL, + ADMIN_EMAIL, ) from app.dashboard.base import dashboard_bp +from app.email_utils import send_email +from app.extensions import db +from app.models import LifetimeCoupon + + +class CouponForm(FlaskForm): + code = StringField("Coupon Code", validators=[validators.DataRequired()]) @dashboard_bp.route("/pricing", methods=["GET", "POST"]) @@ -18,12 +28,39 @@ def pricing(): flash("You are already a premium user", "warning") return redirect(url_for("dashboard.index")) + coupon_form = CouponForm() + + if coupon_form.validate_on_submit(): + code = coupon_form.code.data + + coupon = LifetimeCoupon.get_by(code=code) + + if coupon and coupon.nb_used > 0: + coupon.nb_used -= 1 + current_user.lifetime = True + db.session.commit() + + # notify admin + send_email( + ADMIN_EMAIL, + subject=f"User {current_user.id} used lifetime coupon. Coupon nb_used: {coupon.nb_used}", + plaintext="", + html="", + ) + + flash("You are upgraded to lifetime premium!", "success") + return redirect(url_for("dashboard.index")) + + else: + flash(f"Coupon *{code}* expired or invalid", "warning") + return render_template( "dashboard/pricing.html", PADDLE_VENDOR_ID=PADDLE_VENDOR_ID, PADDLE_MONTHLY_PRODUCT_ID=PADDLE_MONTHLY_PRODUCT_ID, PADDLE_YEARLY_PRODUCT_ID=PADDLE_YEARLY_PRODUCT_ID, success_url=URL + "/dashboard/subscription_success", + coupon_form=coupon_form, )