pruneattachments.py 862 B

12345678910111213141516171819202122
  1. from datetime import timedelta
  2. from django.core.management.base import BaseCommand
  3. from django.utils import timezone
  4. from misago.models import Attachment
  5. class Command(BaseCommand):
  6. """
  7. Prune Attachments
  8. This command removes attachments that were uploaded but not attached to any posts.
  9. """
  10. help = 'Prune orphaned attachments'
  11. def handle(self, *args, **options):
  12. date_cutoff = timezone.now() - timedelta(days=1)
  13. deleted_count = 0
  14. for attachment in Attachment.objects.filter(date__lt=date_cutoff).filter(post__isnull=True).iterator():
  15. attachment.delete()
  16. deleted_count += 1
  17. if deleted_count == 1:
  18. self.stdout.write('One orphaned attachment has been deleted.\n')
  19. else:
  20. self.stdout.write('%s orphaned attachments have been deleted.\n' % deleted_count)