pruneforums.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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, UpdatingMonitor
  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. sync_forums = []
  13. for forum in Forum.objects.all():
  14. archive = forum.pruned_archive
  15. deleted = 0
  16. if forum.prune_start:
  17. for thread in forum.thread_set.filter(weight=0).filter(start__lte=timezone.now() - timedelta(days=forum.prune_start)):
  18. if archive:
  19. thread.move_to(archive)
  20. thread.save(force_update=True)
  21. else:
  22. thread.delete()
  23. deleted += 1
  24. if forum.prune_last:
  25. for thread in forum.thread_set.filter(weight=0).filter(last__lte=timezone.now() - timedelta(days=forum.prune_last)):
  26. if archive:
  27. thread.move_to(archive)
  28. thread.save(force_update=True)
  29. else:
  30. thread.delete()
  31. deleted += 1
  32. if deleted:
  33. if forum not in sync_forums:
  34. sync_forums.append(forum)
  35. if archive and archive not in sync_forums:
  36. sync_forums.append(archive)
  37. for forum in sync_forums:
  38. forum.sync()
  39. forum.save(force_update=True)
  40. with UpdatingMonitor() as cm:
  41. monitor['threads'] = Thread.objects.count()
  42. monitor['posts'] = Post.objects.count()
  43. self.stdout.write('Forums were pruned.\n')