From 026fe4adddbbe4445a2a94d4ebfe66e107ceb8b2 Mon Sep 17 00:00:00 2001 From: Son NK Date: Wed, 1 Jan 2020 19:46:35 +0100 Subject: [PATCH] get_subscription should only return *active* subscription. --- app/dashboard/views/billing.py | 8 ++++---- app/models.py | 18 ++++++++++++------ templates/header.html | 2 +- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/app/dashboard/views/billing.py b/app/dashboard/views/billing.py index 65ce62cd..0648be24 100644 --- a/app/dashboard/views/billing.py +++ b/app/dashboard/views/billing.py @@ -8,10 +8,10 @@ from app.dashboard.base import dashboard_bp @login_required def billing(): # sanity check: make sure this page is only for user who has paddle subscription - if not current_user.is_premium(): - flash("This page is for paid customer only", "warning") - return redirect(url_for("dashboard.index")) - sub = current_user.get_subscription() + if not sub: + flash("You don't have any active subscription", "warning") + return redirect(url_for("dashboard.index")) + return render_template("dashboard/billing.html", sub=sub) diff --git a/app/models.py b/app/models.py index 9ff2f931..ca864a48 100644 --- a/app/models.py +++ b/app/models.py @@ -145,11 +145,6 @@ class User(db.Model, ModelMixin, UserMixin): """user is premium if they have a active subscription""" sub: Subscription = self.get_subscription() if sub: - if sub.cancelled: - # user is premium until the next billing_date + 1 - return sub.next_bill_date >= arrow.now().shift(days=-1).date() - - # subscription active, ie not cancelled return True return False @@ -217,8 +212,19 @@ class User(db.Model, ModelMixin, UserMixin): return "Free Plan" def get_subscription(self): + """return *active* subscription + TODO: support user unsubscribe and re-subscribe + """ sub = Subscription.get_by(user_id=self.id) - return sub + if sub and sub.cancelled: + # sub is active until the next billing_date + 1 + if sub.next_bill_date >= arrow.now().shift(days=-1).date(): + return sub + else: # past subscription, user is considered not having a subscription + return None + else: + return sub + def verified_custom_domains(self): return CustomDomain.query.filter_by(user_id=self.id, verified=True).all() diff --git a/templates/header.html b/templates/header.html index a7c0fd7d..48731ff8 100644 --- a/templates/header.html +++ b/templates/header.html @@ -42,7 +42,7 @@