From 4d388a202cf58a01810d99d087e7e32f97dda028 Mon Sep 17 00:00:00 2001 From: Son Date: Wed, 1 Dec 2021 17:16:01 +0100 Subject: [PATCH] allow user with manual or coinbase subscription to switch to paddle --- app/dashboard/views/pricing.py | 27 ++++++++++++++++++++++++--- app/models.py | 25 ------------------------- templates/dashboard/pricing.html | 19 +++++++++++++++++++ 3 files changed, 43 insertions(+), 28 deletions(-) diff --git a/app/dashboard/views/pricing.py b/app/dashboard/views/pricing.py index da16424a..3427066d 100644 --- a/app/dashboard/views/pricing.py +++ b/app/dashboard/views/pricing.py @@ -1,3 +1,4 @@ +import arrow from coinbase_commerce import Client from flask import render_template, flash, redirect, url_for from flask_login import login_required, current_user @@ -12,16 +13,33 @@ from app.config import ( ) from app.dashboard.base import dashboard_bp from app.log import LOG -from app.models import AppleSubscription +from app.models import ( + AppleSubscription, + Subscription, + ManualSubscription, + CoinbaseSubscription, +) @dashboard_bp.route("/pricing", methods=["GET", "POST"]) @login_required def pricing(): - if not current_user.can_upgrade(): - flash("You are already a premium user", "warning") + sub: Subscription = current_user.get_subscription() + # user who has canceled can re-subscribe + if sub and not sub.cancelled: + flash("You already have an active subscription", "error") return redirect(url_for("dashboard.index")) + now = arrow.now() + manual_sub: ManualSubscription = ManualSubscription.filter( + ManualSubscription.user_id == current_user.id, ManualSubscription.end_at > now + ).first() + + coinbase_sub = CoinbaseSubscription.filter( + CoinbaseSubscription.user_id == current_user.id, + CoinbaseSubscription.end_at > now, + ).first() + apple_sub: AppleSubscription = AppleSubscription.get_by(user_id=current_user.id) if apple_sub and apple_sub.is_valid(): flash("Please make sure to cancel your subscription on Apple first", "warning") @@ -32,6 +50,9 @@ def pricing(): PADDLE_MONTHLY_PRODUCT_ID=PADDLE_MONTHLY_PRODUCT_ID, PADDLE_YEARLY_PRODUCT_ID=PADDLE_YEARLY_PRODUCT_ID, success_url=URL + "/dashboard/subscription_success", + manual_sub=manual_sub, + coinbase_sub=coinbase_sub, + now=now, ) diff --git a/app/models.py b/app/models.py index cf970498..86d5e664 100644 --- a/app/models.py +++ b/app/models.py @@ -562,31 +562,6 @@ class User(Base, ModelMixin, UserMixin, PasswordOracle): return True - def can_upgrade(self) -> bool: - """ - The following users can upgrade: - - have giveaway lifetime licence - - have giveaway manual subscriptions - - have a cancelled Paddle subscription - - have a expired Apple subscription - - have a expired Coinbase subscription - """ - sub: Subscription = self.get_subscription() - # user who has canceled can also re-subscribe - if sub and not sub.cancelled: - return False - - manual_sub: ManualSubscription = ManualSubscription.get_by(user_id=self.id) - # user who has giveaway premium can decide to upgrade - if manual_sub and manual_sub.is_active() and not manual_sub.is_giveaway: - return False - - coinbase_subscription = CoinbaseSubscription.get_by(user_id=self.id) - if coinbase_subscription and coinbase_subscription.is_active(): - return False - - return True - def is_premium(self) -> bool: """ user is premium if they: diff --git a/templates/dashboard/pricing.html b/templates/dashboard/pricing.html index 9bc8d175..4900dab4 100644 --- a/templates/dashboard/pricing.html +++ b/templates/dashboard/pricing.html @@ -70,6 +70,16 @@
+ {% if manual_sub %} +
+ You currently have a subscription until {{ manual_sub.end_at.format("YYYY-MM-DD") }} + ({{ (manual_sub.end_at - now).days }} days left). +
+ Please note that the time left will not be taken into account in a new subscription. +
+
+ {% endif %} +
🔐 Secure payments by @@ -87,6 +97,15 @@
{% endif %} + {% if coinbase_sub %} +
+ You currently have a Coinbase subscription until {{ coinbase_sub.end_at.format("YYYY-MM-DD") }} + ({{ (coinbase_sub.end_at - now).days }} days left). +
+ Please note that the time left will not be taken into account in a new Paddle subscription. +
+ {% endif %} +
Paddle supports bank cards (Mastercard, Visa, American Express, etc) and PayPal.