From 02c74e6a5a20118c733bcb4708412849e5134dd7 Mon Sep 17 00:00:00 2001 From: Son NK <> Date: Sun, 13 Dec 2020 19:08:06 +0100 Subject: [PATCH] take into account Coinbase in can_upgrade(), is_paid(), _lifetime_or_active_subscription() --- app/models.py | 48 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/app/models.py b/app/models.py index 0b6ec86a..0e349324 100644 --- a/app/models.py +++ b/app/models.py @@ -341,7 +341,13 @@ class User(db.Model, ModelMixin, UserMixin): return True manual_sub: ManualSubscription = ManualSubscription.get_by(user_id=self.id) - if manual_sub and manual_sub.end_at > arrow.now(): + if manual_sub and manual_sub.is_active(): + return True + + coinbase_subscription: CoinbaseSubscription = CoinbaseSubscription.get_by( + user_id=self.id + ) + if coinbase_subscription and coinbase_subscription.is_active(): return True return False @@ -357,11 +363,13 @@ class User(db.Model, ModelMixin, UserMixin): return True manual_sub: ManualSubscription = ManualSubscription.get_by(user_id=self.id) - if ( - manual_sub - and not manual_sub.is_giveaway - and manual_sub.end_at > arrow.now() - ): + if manual_sub and not manual_sub.is_giveaway and manual_sub.is_active(): + return True + + coinbase_subscription: CoinbaseSubscription = CoinbaseSubscription.get_by( + user_id=self.id + ) + if coinbase_subscription and coinbase_subscription.is_active(): return True return False @@ -387,8 +395,15 @@ class User(db.Model, ModelMixin, UserMixin): return True - def can_upgrade(self): - """User who has lifetime licence or giveaway manual subscriptions can decide to upgrade to a paid plan""" + 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: @@ -400,11 +415,11 @@ class User(db.Model, ModelMixin, UserMixin): 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.end_at > arrow.now() - and not manual_sub.is_giveaway - ): + 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 @@ -477,7 +492,8 @@ class User(db.Model, ModelMixin, UserMixin): return "".join([n[0].upper() for n in names if n]) def get_subscription(self) -> Optional["Subscription"]: - """return *active* subscription + """return *active* Paddle subscription + Return None if the subscription is already expired TODO: support user unsubscribe and re-subscribe """ sub = Subscription.get_by(user_id=self.id) @@ -1434,6 +1450,10 @@ class ManualSubscription(db.Model, ModelMixin): user = db.relationship(User) + def is_active(self): + return self.end_at > arrow.now() + + class CoinbaseSubscription(db.Model, ModelMixin): """ For subscriptions using Coinbase Commerce