mirror of
https://github.com/jlengrand/bugsink.git
synced 2026-03-10 08:01:17 +00:00
18
phonehome/migrations/0002_installation_email_quota_usage.py
Normal file
18
phonehome/migrations/0002_installation_email_quota_usage.py
Normal file
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 4.2.21 on 2025-07-28 12:04
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("phonehome", "0001_b_squashed_initial"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="installation",
|
||||
name="email_quota_usage",
|
||||
field=models.TextField(default='{"per_month": {}}'),
|
||||
),
|
||||
]
|
||||
@@ -1,6 +1,10 @@
|
||||
import json
|
||||
import uuid
|
||||
|
||||
from django.db import models
|
||||
from bugsink.transaction import immediate_atomic
|
||||
|
||||
from bugsink.app_settings import get_settings
|
||||
|
||||
|
||||
class Installation(models.Model):
|
||||
@@ -12,6 +16,29 @@ class Installation(models.Model):
|
||||
|
||||
silence_email_system_warning = models.BooleanField(default=False)
|
||||
|
||||
email_quota_usage = models.TextField(null=False, default='{"per_month": {}}')
|
||||
|
||||
@classmethod
|
||||
@immediate_atomic(only_if_needed=True) # minimalize write-lock-hogging (while being callable within atomic blocks)
|
||||
def check_and_inc_email_quota(cls, date):
|
||||
obj = cls.objects.first()
|
||||
|
||||
email_quota_usage = json.loads(obj.email_quota_usage)
|
||||
|
||||
key = date.strftime('%Y-%m')
|
||||
if key not in email_quota_usage["per_month"]:
|
||||
email_quota_usage['per_month'] = {key: 0} # full overwrite: no need to keep old info around.
|
||||
|
||||
if (get_settings().MAX_EMAILS_PER_MONTH is not None
|
||||
and email_quota_usage['per_month'][key] >= get_settings().MAX_EMAILS_PER_MONTH):
|
||||
return False
|
||||
|
||||
email_quota_usage['per_month'][key] += 1
|
||||
|
||||
obj.email_quota_usage = json.dumps(email_quota_usage)
|
||||
obj.save()
|
||||
return True
|
||||
|
||||
|
||||
class OutboundMessage(models.Model):
|
||||
attempted_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
Reference in New Issue
Block a user