Skip to content

Commit 2c640a4

Browse files
committed
Fixes
1 parent ca24fcd commit 2c640a4

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

readthedocs/organizations/querysets.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,11 @@ def subscription_trial_plan_ended(self):
5151
stripe_subscription__items__price__id=settings.RTD_ORG_DEFAULT_STRIPE_SUBSCRIPTION_PRICE,
5252
)
5353

54-
def disable_soon(self, days, exact=False):
54+
def subscription_ended(self, days, exact=False):
5555
"""
56-
Filter organizations that will eventually be marked as disabled.
56+
Filter organizations which their subscription has ended.
5757
58-
This will return organizations that the paid/trial subscription has
59-
ended ``days`` ago.
60-
61-
Organizations to be disabled are which their subscription has been canceled,
58+
This will return organizations which their subscription has been canceled,
6259
or hasn't been paid for ``days``.
6360
6461
:param days: Days after the subscription has ended
@@ -102,16 +99,27 @@ def disable_soon(self, days, exact=False):
10299
)
103100
)
104101

105-
orgs = (
106-
subscription_ended
102+
return subscription_ended.distinct()
103+
104+
def disable_soon(self, days, exact=False):
105+
"""
106+
Filter organizations that will eventually be marked as disabled.
107+
108+
These are organizations which their subscription has ended,
109+
excluding organizations that can't be disabled, or are already disabled.
110+
111+
:param days: Days after the subscription has ended
112+
:param exact: Make the ``days`` date to match exactly that day after the
113+
subscription has ended (useful to send emails only once)
114+
"""
115+
return (
116+
self.subscription_ended(days=days, exact=exact)
107117
# Exclude organizations that can't be disabled.
108118
.exclude(never_disable=True)
109119
# Exclude organizations that are already disabled
110120
.exclude(disabled=True)
111121
)
112122

113-
return orgs.distinct()
114-
115123
def clean_artifacts(self):
116124
"""
117125
Filter organizations which their artifacts can be cleaned up.
@@ -120,13 +128,11 @@ def clean_artifacts(self):
120128
are disabled and their artifacts weren't cleaned already. We should be
121129
safe to cleanup all their artifacts at this point.
122130
"""
123-
end_date = timezone.now().date() - timedelta(days=3 * DISABLE_AFTER_DAYS)
124-
orgs = self.filter(
131+
return self.subscription_ended(days=3 * DISABLE_AFTER_DAYS, exact=True).filter(
125132
disabled=True,
126-
subscription__end_date__lt=end_date,
127133
artifacts_cleaned=False,
128134
)
129-
return orgs.distinct()
135+
130136

131137
def single_owner(self, user):
132138
"""Returns organizations where `user` is the only owner."""

readthedocs/organizations/signals.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Organization signals."""
22

3+
from djstripe.enums import SubscriptionStatus
34
import structlog
45
from allauth.account.signals import user_signed_up
56
from django.db.models import Count
@@ -10,7 +11,7 @@
1011
from readthedocs.builds.models import Build
1112
from readthedocs.builds.signals import build_complete
1213
from readthedocs.organizations.models import Organization, Team, TeamMember
13-
from readthedocs.payments.utils import delete_customer
14+
from readthedocs.payments.utils import cancel_subscription
1415
from readthedocs.projects.models import Project
1516

1617
from .tasks import (
@@ -52,11 +53,12 @@ def remove_organization_completely(sender, instance, using, **kwargs):
5253
stripe_customer = organization.stripe_customer
5354
if stripe_customer:
5455
log.info(
55-
"Removing Stripe customer",
56+
"Canceling subscriptions",
5657
organization_slug=organization.slug,
5758
stripe_customer_id=stripe_customer.id,
5859
)
59-
delete_customer(stripe_customer.id)
60+
for subscription in stripe_customer.subscriptions.exclude(status=SubscriptionStatus.canceled):
61+
cancel_subscription(subscription.id)
6062

6163
log.info("Removing organization completely", organization_slug=organization.slug)
6264

0 commit comments

Comments
 (0)