pruneforums.py 1.2 KB

1234567891011121314151617181920212223242526272829
  1. from datetime import timedelta
  2. from django.core.management.base import BaseCommand
  3. from django.utils import timezone
  4. from misago.models import Forum, Thread, Post
  5. from misago.monitor import Monitor
  6. class Command(BaseCommand):
  7. """
  8. This command is intended to work as CRON job fired every few days to run forums pruning policies
  9. """
  10. help = 'Updates Popular Threads ranking'
  11. def handle(self, *args, **options):
  12. for forum in Forum.objects.all():
  13. deleted = 0
  14. if forum.prune_start:
  15. for thread in forum.thread_set.filter(weight=0).filter(start__lte=timezone.now() - timedelta(days=forum.prune_start)):
  16. thread.delete()
  17. deleted += 1
  18. if forum.prune_last:
  19. for thread in forum.thread_set.filter(weight=0).filter(last__lte=timezone.now() - timedelta(days=forum.prune_last)):
  20. thread.delete()
  21. deleted += 1
  22. if deleted:
  23. forum.sync()
  24. forum.save(force_update=True)
  25. monitor = Monitor()
  26. monitor['threads'] = Post.objects.count()
  27. monitor['posts'] = Post.objects.count()
  28. self.stdout.write('Forums were pruned.\n')