"prunenotifications" management command

This commit is contained in:
Pēteris Caune 2016-12-15 18:54:03 +02:00
parent c6d5e64286
commit 0b6d484bd5
2 changed files with 29 additions and 0 deletions

View File

@ -190,6 +190,13 @@ There are separate Django management commands for each task:
$ ./manage.py pruneemails $ ./manage.py pruneemails
```` ````
* Remove old records of sent notifications. For each check, remove
notifications that are older than the oldest stored ping for same check.
````
$ ./manage.py prunenotifications
````
* Remove user accounts that match either of these conditions: * Remove user accounts that match either of these conditions:
* Account was created more than a month ago, and user has never logged in. * Account was created more than a month ago, and user has never logged in.
These can happen when user enters invalid email address when signing up. These can happen when user enters invalid email address when signing up.

View File

@ -0,0 +1,22 @@
from django.core.management.base import BaseCommand
from django.db.models import Min
from hc.api.models import Notification, Check
class Command(BaseCommand):
help = 'Prune stored notifications'
def handle(self, *args, **options):
total = 0
q = Check.objects.filter(n_pings__gt=0)
q = q.annotate(min_ping_date=Min("ping__created"))
for check in q:
qq = Notification.objects.filter(owner_id=check.id,
created__lt=check.min_ping_date)
num_deleted, _ = qq.delete()
total += num_deleted
return "Done! Pruned %d notifications." % total