Browse Source

"prunenotifications" management command

pull/109/head
Pēteris Caune 8 years ago
parent
commit
0b6d484bd5
2 changed files with 29 additions and 0 deletions
  1. +7
    -0
      README.md
  2. +22
    -0
      hc/api/management/commands/prunenotifications.py

+ 7
- 0
README.md 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.


+ 22
- 0
hc/api/management/commands/prunenotifications.py 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

Loading…
Cancel
Save