pruneforums.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from datetime import timedelta
  2. from django.core.management.base import BaseCommand
  3. from django.utils import timezone
  4. from misago.forums.models import Forum
  5. class Command(BaseCommand):
  6. """
  7. This command is intended to work as CRON job fired
  8. every few days to run forums pruning policies
  9. """
  10. help = 'Prunes forums'
  11. def handle(self, *args, **options):
  12. now = timezone.now()
  13. synchronize_forums = []
  14. for forum in Forum.objects.iterator():
  15. archive = forum.archive_pruned_in
  16. pruned_threads = 0
  17. threads_qs = forum.thread_set.filter(weight=0)
  18. if forum.prune_started_after:
  19. cutoff = now - timedelta(days=forum.prune_started_after)
  20. prune_qs = threads_qs.filter(started_on__lte=cutoff)
  21. for thread in prune_qs.iterator():
  22. if archive:
  23. thread.move(archive)
  24. thread.save()
  25. else:
  26. thread.delete()
  27. pruned_threads += 1
  28. if forum.prune_replied_after:
  29. cutoff = now - timedelta(days=forum.prune_replied_after)
  30. prune_qs = threads_qs.filter(last_post_on__lte=cutoff)
  31. for thread in prune_qs.iterator():
  32. if archive:
  33. thread.move(archive)
  34. thread.save()
  35. else:
  36. thread.delete()
  37. pruned_threads += 1
  38. if pruned_threads:
  39. if forum not in synchronize_forums:
  40. synchronize_forums.append(forum)
  41. if archive and archive not in synchronize_forums:
  42. synchronize_forums.append(archive)
  43. for forum in synchronize_forums:
  44. forum.synchronize()
  45. forum.save()
  46. self.stdout.write('Forums were pruned.\n')