diff --git a/boards/config/default_settings.ini b/boards/config/default_settings.ini --- a/boards/config/default_settings.ini +++ b/boards/config/default_settings.ini @@ -19,8 +19,7 @@ PostingDelay = 30 [Messages] # Thread bumplimit MaxPostsPerThread = 10 -# Old posts will be archived or deleted if this value is reached -MaxThreadCount = 5 +ThreadArchiveDays = 300 AnonymousMode = false [View] diff --git a/boards/management/commands/archive_threads.py b/boards/management/commands/archive_threads.py new file mode 100644 --- /dev/null +++ b/boards/management/commands/archive_threads.py @@ -0,0 +1,16 @@ +from django.core.management import BaseCommand +from django.db import transaction +from django.db.models import Count + +from boards.models import Thread + + +__author__ = 'neko259' + + +class Command(BaseCommand): + help = 'Archive old threads' + + @transaction.atomic + def handle(self, *args, **options): + Thread.objects.process_old_threads() diff --git a/boards/models/post/manager.py b/boards/models/post/manager.py --- a/boards/models/post/manager.py +++ b/boards/models/post/manager.py @@ -56,7 +56,6 @@ class PostManager(models.Manager): bump_time=posting_time, last_edit_time=posting_time, monochrome=monochrome) list(map(thread.tags.add, tags)) - boards.models.thread.Thread.objects.process_oldest_threads() new_thread = True pre_text = Parser().preparse(text) diff --git a/boards/models/thread.py b/boards/models/thread.py --- a/boards/models/thread.py +++ b/boards/models/thread.py @@ -1,11 +1,13 @@ import logging from adjacent import Client -from boards.models.attachment import FILE_TYPES_IMAGE +from datetime import timedelta + from django.db.models import Count, Sum, QuerySet, Q from django.utils import timezone from django.db import models, transaction +from boards.models.attachment import FILE_TYPES_IMAGE from boards.models import STATUS_BUMPLIMIT, STATUS_ACTIVE, STATUS_ARCHIVE from boards import settings @@ -36,27 +38,23 @@ STATUS_CHOICES = ( class ThreadManager(models.Manager): - def process_oldest_threads(self): + def process_old_threads(self): """ Preserves maximum thread count. If there are too many threads, archive or delete the old ones. """ - - threads = Thread.objects.exclude(status=STATUS_ARCHIVE).order_by('-bump_time') - thread_count = threads.count() + old_time_delta = settings.get_int('Messages', 'ThreadArchiveDays') + old_time = timezone.now() - timedelta(days=old_time_delta) + old_ops = Post.objects.filter(opening=True, pub_time__lte=old_time).exclude(thread__status=STATUS_ARCHIVE) - max_thread_count = settings.get_int('Messages', 'MaxThreadCount') - if thread_count > max_thread_count: - num_threads_to_delete = thread_count - max_thread_count - old_threads = threads[thread_count - num_threads_to_delete:] + for op in old_ops: + thread = op.get_thread() + if settings.get_bool('Storage', 'ArchiveThreads'): + self._archive_thread(thread) + else: + thread.delete() + logger.info('Processed old thread {}'.format(thread)) - for thread in old_threads: - if settings.get_bool('Storage', 'ArchiveThreads'): - self._archive_thread(thread) - else: - thread.delete() - - logger.info('Processed %d old threads' % num_threads_to_delete) def _archive_thread(self, thread): thread.status = STATUS_ARCHIVE