Browse Source

Prune forums

Ralfp 12 years ago
parent
commit
f21f3d3ea8
2 changed files with 26 additions and 0 deletions
  1. 1 0
      cron.txt
  2. 25 0
      misago/management/commands/pruneforums.py

+ 1 - 0
cron.txt

@@ -6,3 +6,4 @@
 20 3 * * * python $HOME/misago/manage.py cleartokens
 25 3 * * * python $HOME/misago/manage.py updatethreadranking
 0 0 * * 0 python $HOME/misago/manage.py forcepdssync
+0 3 */2 * * python $HOME/misago/manage.py pruneforums

+ 25 - 0
misago/management/commands/pruneforums.py

@@ -0,0 +1,25 @@
+from datetime import timedelta
+from django.core.management.base import BaseCommand
+from django.utils import timezone
+from misago.models import Forum, Thread
+
+class Command(BaseCommand):
+    """
+    This command is intended to work as CRON job fired every few days to run forums pruning policies
+    """
+    help = 'Updates Popular Threads ranking'
+    def handle(self, *args, **options):
+        for forum in Forum.objects.all():
+            deleted = 0
+            if forum.prune_start:
+                for thread in forum.thread_set.filter(start__lte=timezone.now() - timedelta(days=forum.prune_start)):
+                    thread.delete()
+                    deleted += 1
+            if forum.prune_last:
+                for thread in forum.thread_set.filter(start__lte=timezone.now() - timedelta(days=forum.prune_last)):
+                    thread.delete()
+                    deleted += 1
+            if deleted:
+                forum.sync()
+                forum.save(force_update=True)
+        self.stdout.write('Forums were pruned.\n')