You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

22 lines
756 B

  1. from django.db.models import F
  2. from django.contrib.auth.models import User
  3. from django.core.management.base import BaseCommand
  4. from hc.accounts.models import Profile
  5. from hc.api.models import Ping
  6. class Command(BaseCommand):
  7. help = 'Prune pings based on limits in user profiles'
  8. def handle(self, *args, **options):
  9. # Create any missing user profiles
  10. for user in User.objects.filter(profile=None):
  11. Profile.objects.get_or_create(user_id=user.id)
  12. q = Ping.objects
  13. q = q.annotate(limit=F("owner__user__profile__ping_log_limit"))
  14. q = q.filter(n__lt=F("owner__n_pings") - F("limit"))
  15. q = q.filter(n__gt=0)
  16. n_pruned, _ = q.delete()
  17. return "Done! Pruned %d pings" % n_pruned