take into account Coinbase in can_upgrade(), is_paid(), _lifetime_or_active_subscription()

This commit is contained in:
Son NK 2020-12-13 19:08:06 +01:00
parent 9329cf04ad
commit 02c74e6a5a

View file

@ -341,7 +341,13 @@ class User(db.Model, ModelMixin, UserMixin):
return True return True
manual_sub: ManualSubscription = ManualSubscription.get_by(user_id=self.id) 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 True
return False return False
@ -357,11 +363,13 @@ class User(db.Model, ModelMixin, UserMixin):
return True return True
manual_sub: ManualSubscription = ManualSubscription.get_by(user_id=self.id) manual_sub: ManualSubscription = ManualSubscription.get_by(user_id=self.id)
if ( if manual_sub and not manual_sub.is_giveaway and manual_sub.is_active():
manual_sub return True
and not manual_sub.is_giveaway
and manual_sub.end_at > arrow.now() coinbase_subscription: CoinbaseSubscription = CoinbaseSubscription.get_by(
): user_id=self.id
)
if coinbase_subscription and coinbase_subscription.is_active():
return True return True
return False return False
@ -387,8 +395,15 @@ class User(db.Model, ModelMixin, UserMixin):
return True return True
def can_upgrade(self): def can_upgrade(self) -> bool:
"""User who has lifetime licence or giveaway manual subscriptions can decide to upgrade to a paid plan""" """
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() sub: Subscription = self.get_subscription()
# user who has canceled can also re-subscribe # user who has canceled can also re-subscribe
if sub and not sub.cancelled: 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) manual_sub: ManualSubscription = ManualSubscription.get_by(user_id=self.id)
# user who has giveaway premium can decide to upgrade # user who has giveaway premium can decide to upgrade
if ( if manual_sub and manual_sub.is_active() and not manual_sub.is_giveaway:
manual_sub return False
and manual_sub.end_at > arrow.now()
and not manual_sub.is_giveaway coinbase_subscription = CoinbaseSubscription.get_by(user_id=self.id)
): if coinbase_subscription and coinbase_subscription.is_active():
return False return False
return True return True
@ -477,7 +492,8 @@ class User(db.Model, ModelMixin, UserMixin):
return "".join([n[0].upper() for n in names if n]) return "".join([n[0].upper() for n in names if n])
def get_subscription(self) -> Optional["Subscription"]: 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 TODO: support user unsubscribe and re-subscribe
""" """
sub = Subscription.get_by(user_id=self.id) sub = Subscription.get_by(user_id=self.id)
@ -1434,6 +1450,10 @@ class ManualSubscription(db.Model, ModelMixin):
user = db.relationship(User) user = db.relationship(User)
def is_active(self):
return self.end_at > arrow.now()
class CoinbaseSubscription(db.Model, ModelMixin): class CoinbaseSubscription(db.Model, ModelMixin):
""" """
For subscriptions using Coinbase Commerce For subscriptions using Coinbase Commerce