pruneforums.py 1.1 KB

12345678910111213141516171819202122232425
  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
  5. class Command(BaseCommand):
  6. """
  7. This command is intended to work as CRON job fired every few days to run forums pruning policies
  8. """
  9. help = 'Updates Popular Threads ranking'
  10. def handle(self, *args, **options):
  11. for forum in Forum.objects.all():
  12. deleted = 0
  13. if forum.prune_start:
  14. for thread in forum.thread_set.filter(weight=0).filter(start__lte=timezone.now() - timedelta(days=forum.prune_start)):
  15. thread.delete()
  16. deleted += 1
  17. if forum.prune_last:
  18. for thread in forum.thread_set.filter(weight=0).filter(start__lte=timezone.now() - timedelta(days=forum.prune_last)):
  19. thread.delete()
  20. deleted += 1
  21. if deleted:
  22. forum.sync()
  23. forum.save(force_update=True)
  24. self.stdout.write('Forums were pruned.\n')