Browse Source

Update MetQuotaEmail context

David Burke 5 months ago
parent
commit
ef097097d4

+ 13 - 3
apps/organizations_ext/email.py

@@ -15,14 +15,24 @@ class MetQuotaEmail(DetailEmail):
         return self.object.email
 
     def get_context_data(self, **kwargs):
+        from djstripe.models import Product
+
         context = super().get_context_data(**kwargs)
         base_url = settings.GLITCHTIP_URL.geturl()
-        event_limit = settings.BILLING_FREE_TIER_EVENTS
         organization = self.object
         subscription_link = f"{base_url}/{organization.slug}/settings/subscription"
         context["organization_name"] = organization.name
-        context["event_limit"] = event_limit
-        context["subscription_link"] = subscription_link
+        product = Product.objects.filter(
+            plan__subscriptions__customer__subscriber=organization,
+            plan__subscriptions__status="active",
+        ).first()
+        context.update(
+            {
+                "product": product,
+                "event_limit": product.metadata.get("events") if product else None,
+                "subscription_link": subscription_link,
+            }
+        )
         return context
 
 

+ 4 - 3
apps/organizations_ext/tasks.py

@@ -61,14 +61,15 @@ def _check_and_update_throttle(org: Organization):
         org_throttle = 10
 
     if org.event_throttle_rate != org_throttle:
-        if org_throttle > org.event_throttle_rate:
-            send_throttle_email.delay(org.id, org.event_throttle_rate, org_throttle)
+        old_throttle = org.event_throttle_rate
         org.event_throttle_rate = org_throttle
         org.save(update_fields=["event_throttle_rate"])
+        if org_throttle > old_throttle:
+            send_throttle_email.delay(org.id)
 
 
 @shared_task
-def send_throttle_email(organization_id: int, old_throttle: int, new_throttle: int):
+def send_throttle_email(organization_id: int):
     MetQuotaEmail(pk=organization_id).send_email()
 
 

+ 6 - 0
apps/organizations_ext/tests/test_throttling.py

@@ -1,5 +1,6 @@
 from datetime import timedelta
 
+from django.core import mail
 from django.test import TestCase, override_settings
 from django.utils import timezone
 from freezegun import freeze_time
@@ -65,6 +66,7 @@ class OrganizationThrottleCheckTestCase(TestCase):
         check_organization_throttle(self.organization.id)
         self.organization.refresh_from_db()
         self.assertEqual(self.organization.event_throttle_rate, 10)
+        self.assertEqual(len(mail.outbox), 1)
 
         baker.make(
             "projects.IssueEventProjectHourlyStatistic",
@@ -74,6 +76,7 @@ class OrganizationThrottleCheckTestCase(TestCase):
         check_organization_throttle(self.organization.id)
         self.organization.refresh_from_db()
         self.assertEqual(self.organization.event_throttle_rate, 100)
+        self.assertEqual(len(mail.outbox), 2)
 
     def test_check_all_organizations_throttle(self):
         org = self.organization
@@ -90,12 +93,14 @@ class OrganizationThrottleCheckTestCase(TestCase):
         check_all_organizations_throttle()
         org.refresh_from_db()
         self.assertEqual(org.event_throttle_rate, 0)
+        self.assertEqual(len(mail.outbox), 0)
 
         # 11 events (of 10), small throttle
         self._make_events(5)
         check_all_organizations_throttle()
         org.refresh_from_db()
         self.assertEqual(org.event_throttle_rate, 10)
+        self.assertEqual(len(mail.outbox), 1)
 
         # New time period, should reset throttle
         now = self.subscription.current_period_start + timedelta(hours=1)
@@ -105,6 +110,7 @@ class OrganizationThrottleCheckTestCase(TestCase):
         check_all_organizations_throttle()
         org.refresh_from_db()
         self.assertEqual(org.event_throttle_rate, 0)
+        self.assertEqual(len(mail.outbox), 1)
 
         # Throttle again
         with freeze_time(now):

+ 0 - 2
glitchtip/settings.py

@@ -745,8 +745,6 @@ PLAUSIBLE_DOMAIN = env.str("PLAUSIBLE_DOMAIN", default=None)
 # Support plans available. Email info@burkesoftware.com for more info.
 I_PAID_FOR_GLITCHTIP = env.bool("I_PAID_FOR_GLITCHTIP", False)
 
-# Max events per month for free tier
-BILLING_FREE_TIER_EVENTS = env.int("BILLING_FREE_TIER_EVENTS", 1000)
 DJSTRIPE_SUBSCRIBER_MODEL = "organizations_ext.Organization"
 DJSTRIPE_SUBSCRIBER_MODEL_REQUEST_CALLBACK = organization_request_callback
 DJSTRIPE_USE_NATIVE_JSONFIELD = True