Add subscription property to Profile model
and update has_premium method to use subscription property
This commit is contained in:
Родитель
1f393bcfe7
Коммит
70014eeef4
|
@ -28,6 +28,7 @@ from allauth.socialaccount.models import SocialAccount
|
|||
from rest_framework.authtoken.models import Token
|
||||
|
||||
from api.exceptions import ErrorContextType, RelayAPIException
|
||||
from privaterelay.models import Subscription, update_or_create_subscription
|
||||
from privaterelay.plans import get_premium_countries
|
||||
from privaterelay.utils import (
|
||||
AcceptLanguageError,
|
||||
|
@ -324,13 +325,13 @@ class Profile(models.Model):
|
|||
# this to mark the user as a premium user as well
|
||||
if not self.fxa:
|
||||
return False
|
||||
|
||||
if self.subscription:
|
||||
return True
|
||||
|
||||
for premium_domain in PREMIUM_DOMAINS:
|
||||
if self.user.email.endswith(f"@{premium_domain}"):
|
||||
return True
|
||||
user_subscriptions = self.fxa.extra_data.get("subscriptions", [])
|
||||
for sub in settings.SUBSCRIPTIONS_WITH_UNLIMITED:
|
||||
if sub in user_subscriptions:
|
||||
return True
|
||||
return False
|
||||
|
||||
@property
|
||||
|
@ -573,6 +574,14 @@ class Profile(models.Model):
|
|||
return "free"
|
||||
return f"{plan}_{self.plan_term}"
|
||||
|
||||
@property
|
||||
def subscription(self) -> str | None:
|
||||
try:
|
||||
subscription = Subscription.objects.get(user=self.user)
|
||||
except Subscription.DoesNotExist:
|
||||
subscription = update_or_create_subscription(self.fxa)
|
||||
return subscription.names
|
||||
|
||||
|
||||
@receiver(models.signals.post_save, sender=Profile)
|
||||
def copy_auth_token(sender, instance=None, created=False, **kwargs):
|
||||
|
|
|
@ -668,6 +668,35 @@ class ProfileLastBounceDateTest(ProfileBounceTestCase):
|
|||
assert self.profile.last_bounce_date == self.profile.last_hard_bounce
|
||||
|
||||
|
||||
class ProfileSubscriptionTest(ProfileTestCase):
|
||||
"""Test Profile.subscription"""
|
||||
|
||||
@patch("privaterelay.signals.update_or_create_subscription")
|
||||
def test_subscription_object_dne_creates_subscription(
|
||||
self, mocked_update: Mock
|
||||
) -> None:
|
||||
self.get_or_create_social_account()
|
||||
assert self.profile.subscription == ""
|
||||
assert self.profile.has_premium is False
|
||||
mocked_update.assert_called_once_with(self.profile.fxa)
|
||||
|
||||
def test_no_subscriptions_returns_empty_string(self) -> None:
|
||||
self.get_or_create_social_account()
|
||||
assert self.profile.subscription == ""
|
||||
assert self.profile.has_premium is False
|
||||
|
||||
@patch("privaterelay.signals.update_or_create_subscription")
|
||||
def test_premium_user_no_subscription_object_dne_creates_subscription(
|
||||
self, mocked_update: Mock
|
||||
) -> None:
|
||||
self.upgrade_to_premium()
|
||||
assert self.profile.fxa is not None
|
||||
user_subscriptions = self.profile.fxa.extra_data.get("subscriptions", [])
|
||||
expected_subscriptions = ",".join(user_subscriptions)
|
||||
assert self.profile.subscription == f"{expected_subscriptions},"
|
||||
assert self.profile.has_premium is True
|
||||
|
||||
|
||||
class ProfileHasPremiumTest(ProfileTestCase):
|
||||
"""Tests for Profile.has_premium"""
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче