Show More
@@ -0,0 +1,16 b'' | |||||
|
1 | from django.core.management import BaseCommand | |||
|
2 | from django.db import transaction | |||
|
3 | from django.db.models import Count | |||
|
4 | ||||
|
5 | from boards.models import Thread | |||
|
6 | ||||
|
7 | ||||
|
8 | __author__ = 'neko259' | |||
|
9 | ||||
|
10 | ||||
|
11 | class Command(BaseCommand): | |||
|
12 | help = 'Archive old threads' | |||
|
13 | ||||
|
14 | @transaction.atomic | |||
|
15 | def handle(self, *args, **options): | |||
|
16 | Thread.objects.process_old_threads() |
@@ -19,8 +19,7 b' PostingDelay = 30' | |||||
19 | [Messages] |
|
19 | [Messages] | |
20 | # Thread bumplimit |
|
20 | # Thread bumplimit | |
21 | MaxPostsPerThread = 10 |
|
21 | MaxPostsPerThread = 10 | |
22 | # Old posts will be archived or deleted if this value is reached |
|
22 | ThreadArchiveDays = 300 | |
23 | MaxThreadCount = 5 |
|
|||
24 | AnonymousMode = false |
|
23 | AnonymousMode = false | |
25 |
|
24 | |||
26 | [View] |
|
25 | [View] |
@@ -56,7 +56,6 b' class PostManager(models.Manager):' | |||||
56 | bump_time=posting_time, last_edit_time=posting_time, |
|
56 | bump_time=posting_time, last_edit_time=posting_time, | |
57 | monochrome=monochrome) |
|
57 | monochrome=monochrome) | |
58 | list(map(thread.tags.add, tags)) |
|
58 | list(map(thread.tags.add, tags)) | |
59 | boards.models.thread.Thread.objects.process_oldest_threads() |
|
|||
60 | new_thread = True |
|
59 | new_thread = True | |
61 |
|
60 | |||
62 | pre_text = Parser().preparse(text) |
|
61 | pre_text = Parser().preparse(text) |
@@ -1,11 +1,13 b'' | |||||
1 | import logging |
|
1 | import logging | |
2 | from adjacent import Client |
|
2 | from adjacent import Client | |
3 | from boards.models.attachment import FILE_TYPES_IMAGE |
|
3 | from datetime import timedelta | |
|
4 | ||||
4 |
|
5 | |||
5 | from django.db.models import Count, Sum, QuerySet, Q |
|
6 | from django.db.models import Count, Sum, QuerySet, Q | |
6 | from django.utils import timezone |
|
7 | from django.utils import timezone | |
7 | from django.db import models, transaction |
|
8 | from django.db import models, transaction | |
8 |
|
9 | |||
|
10 | from boards.models.attachment import FILE_TYPES_IMAGE | |||
9 | from boards.models import STATUS_BUMPLIMIT, STATUS_ACTIVE, STATUS_ARCHIVE |
|
11 | from boards.models import STATUS_BUMPLIMIT, STATUS_ACTIVE, STATUS_ARCHIVE | |
10 |
|
12 | |||
11 | from boards import settings |
|
13 | from boards import settings | |
@@ -36,27 +38,23 b' STATUS_CHOICES = (' | |||||
36 |
|
38 | |||
37 |
|
39 | |||
38 | class ThreadManager(models.Manager): |
|
40 | class ThreadManager(models.Manager): | |
39 |
def process_old |
|
41 | def process_old_threads(self): | |
40 | """ |
|
42 | """ | |
41 | Preserves maximum thread count. If there are too many threads, |
|
43 | Preserves maximum thread count. If there are too many threads, | |
42 | archive or delete the old ones. |
|
44 | archive or delete the old ones. | |
43 | """ |
|
45 | """ | |
44 |
|
46 | old_time_delta = settings.get_int('Messages', 'ThreadArchiveDays') | ||
45 | threads = Thread.objects.exclude(status=STATUS_ARCHIVE).order_by('-bump_time') |
|
47 | old_time = timezone.now() - timedelta(days=old_time_delta) | |
46 | thread_count = threads.count() |
|
48 | old_ops = Post.objects.filter(opening=True, pub_time__lte=old_time).exclude(thread__status=STATUS_ARCHIVE) | |
47 |
|
49 | |||
48 | max_thread_count = settings.get_int('Messages', 'MaxThreadCount') |
|
50 | for op in old_ops: | |
49 |
|
|
51 | thread = op.get_thread() | |
50 | num_threads_to_delete = thread_count - max_thread_count |
|
52 | if settings.get_bool('Storage', 'ArchiveThreads'): | |
51 | old_threads = threads[thread_count - num_threads_to_delete:] |
|
53 | self._archive_thread(thread) | |
|
54 | else: | |||
|
55 | thread.delete() | |||
|
56 | logger.info('Processed old thread {}'.format(thread)) | |||
52 |
|
57 | |||
53 | for thread in old_threads: |
|
|||
54 | if settings.get_bool('Storage', 'ArchiveThreads'): |
|
|||
55 | self._archive_thread(thread) |
|
|||
56 | else: |
|
|||
57 | thread.delete() |
|
|||
58 |
|
||||
59 | logger.info('Processed %d old threads' % num_threads_to_delete) |
|
|||
60 |
|
58 | |||
61 | def _archive_thread(self, thread): |
|
59 | def _archive_thread(self, thread): | |
62 | thread.status = STATUS_ARCHIVE |
|
60 | thread.status = STATUS_ARCHIVE |
General Comments 0
You need to be logged in to leave comments.
Login now