diff --git a/boards/admin.py b/boards/admin.py --- a/boards/admin.py +++ b/boards/admin.py @@ -56,9 +56,9 @@ class ThreadAdmin(admin.ModelAdmin): def op(self, obj: Thread): return obj.get_opening_post_id() - list_display = ('id', 'op', 'title', 'reply_count', 'archived', 'ip', + list_display = ('id', 'op', 'title', 'reply_count', 'status', 'ip', 'display_tags') - list_filter = ('bump_time', 'archived', 'bumpable') + list_filter = ('bump_time', 'status') search_fields = ('id', 'title') filter_horizontal = ('tags',) diff --git a/boards/forms.py b/boards/forms.py --- a/boards/forms.py +++ b/boards/forms.py @@ -238,7 +238,7 @@ class PostForm(NeboardForm): for thread_id in threads_id_list: try: thread = Post.objects.get(id=int(thread_id)) - if not thread.is_opening() or thread.get_thread().archived: + if not thread.is_opening() or thread.get_thread().is_archived(): raise ObjectDoesNotExist() threads.append(thread) except (ObjectDoesNotExist, ValueError): diff --git a/boards/models/image.py b/boards/models/image.py --- a/boards/models/image.py +++ b/boards/models/image.py @@ -27,8 +27,8 @@ class PostImageManager(models.Manager): return post_image - def get_random_images(self, count, include_archived=False, tags=None): - images = self.filter(post_images__thread__archived=include_archived) + def get_random_images(self, count, tags=None): + images = self if tags is not None: images = images.filter(post_images__threads__tags__in=tags) return images.order_by('?')[:count] diff --git a/boards/models/post/__init__.py b/boards/models/post/__init__.py --- a/boards/models/post/__init__.py +++ b/boards/models/post/__init__.py @@ -178,7 +178,7 @@ class Post(models.Model, Viewable): thread = self.get_thread() css_classes = [CSS_CLS_POST] - if thread.archived: + if thread.is_archived(): css_classes.append(CSS_CLS_ARCHIVE_POST) elif not thread.can_bump(): css_classes.append(CSS_CLS_DEAD_POST) @@ -283,7 +283,7 @@ class Post(models.Model, Viewable): for thread in self.get_threads().all(): thread.last_edit_time = self.last_edit_time - thread.save(update_fields=['last_edit_time', 'bumpable']) + thread.save(update_fields=['last_edit_time', 'status']) super().save(force_insert, force_update, using, update_fields) @@ -335,7 +335,7 @@ class Post(models.Model, Viewable): thread.update_bump_status() thread.last_edit_time = self.last_edit_time - thread.save(update_fields=['last_edit_time', 'bumpable']) + thread.save(update_fields=['last_edit_time', 'status']) self.threads.add(opening_post.get_thread()) def get_tripcode(self): diff --git a/boards/models/tag.py b/boards/models/tag.py --- a/boards/models/tag.py +++ b/boards/models/tag.py @@ -5,6 +5,7 @@ from django.db.models import Count from django.core.urlresolvers import reverse from boards.models.base import Viewable +from boards.models.thread import STATUS_ACTIVE, STATUS_BUMPLIMIT, STATUS_ARCHIVE from boards.utils import cached_result import boards @@ -61,22 +62,20 @@ class Tag(models.Model, Viewable): return self.get_thread_count() == 0 - def get_thread_count(self, archived=None, bumpable=None) -> int: + def get_thread_count(self, status=None) -> int: threads = self.get_threads() - if archived is not None: - threads = threads.filter(archived=archived) - if bumpable is not None: - threads = threads.filter(bumpable=bumpable) + if status is not None: + threads = threads.filter(status=status) return threads.count() def get_active_thread_count(self) -> int: - return self.get_thread_count(archived=False, bumpable=True) + return self.get_thread_count(status=STATUS_ACTIVE) def get_bumplimit_thread_count(self) -> int: - return self.get_thread_count(archived=False, bumpable=False) + return self.get_thread_count(status=STATUS_BUMPLIMIT) def get_archived_thread_count(self) -> int: - return self.get_thread_count(archived=True) + return self.get_thread_count(status=STATUS_ARCHIVE) def get_absolute_url(self): return reverse('tag', kwargs={'tag_name': self.name}) @@ -106,11 +105,11 @@ class Tag(models.Model, Viewable): def get_description(self): return self.description - def get_random_image_post(self, archived=False): + def get_random_image_post(self, status=False): posts = boards.models.Post.objects.annotate(images_count=Count( 'images')).filter(images_count__gt=0, threads__tags__in=[self]) - if archived is not None: - posts = posts.filter(thread__archived=archived) + if status is not None: + posts = posts.filter(thread__status=status) return posts.order_by('?').first() def get_first_letter(self): diff --git a/boards/models/thread.py b/boards/models/thread.py --- a/boards/models/thread.py +++ b/boards/models/thread.py @@ -5,6 +5,10 @@ from django.db.models import Count, Sum, from django.utils import timezone from django.db import models +STATUS_ACTIVE = 'active' +STATUS_BUMPLIMIT = 'bumplimit' +STATUS_ARCHIVE = 'archived' + from boards import settings import boards from boards.utils import cached_result, datetime_to_epoch @@ -33,7 +37,7 @@ class ThreadManager(models.Manager): archive or delete the old ones. """ - threads = Thread.objects.filter(archived=False).order_by('-bump_time') + threads = Thread.objects.exclude(status=STATUS_ARCHIVE).order_by('-bump_time') thread_count = threads.count() max_thread_count = settings.get_int('Messages', 'MaxThreadCount') @@ -50,11 +54,10 @@ class ThreadManager(models.Manager): logger.info('Processed %d old threads' % num_threads_to_delete) def _archive_thread(self, thread): - thread.archived = True - thread.bumpable = False + thread.status = STATUS_ARCHIVE thread.last_edit_time = timezone.now() thread.update_posts_time() - thread.save(update_fields=['archived', 'last_edit_time', 'bumpable']) + thread.save(update_fields=['last_edit_time', 'status']) def get_new_posts(self, datas): query = None @@ -90,9 +93,8 @@ class Thread(models.Model): tags = models.ManyToManyField('Tag', related_name='thread_tags') bump_time = models.DateTimeField(db_index=True) last_edit_time = models.DateTimeField() - archived = models.BooleanField(default=False) - bumpable = models.BooleanField(default=True) max_posts = models.IntegerField(default=get_thread_max_posts) + status = models.CharField(max_length=50, default=STATUS_ACTIVE) def get_tags(self) -> QuerySet: """ @@ -118,7 +120,7 @@ class Thread(models.Model): def update_bump_status(self, exclude_posts=None): if self.has_post_limit() and self.get_reply_count() >= self.max_posts: - self.bumpable = False + self.status = STATUS_BUMPLIMIT self.update_posts_time(exclude_posts=exclude_posts) def _get_cache_key(self): @@ -138,7 +140,7 @@ class Thread(models.Model): Checks if the thread can be bumped by replying to it. """ - return self.bumpable and not self.is_archived() + return self.get_status() == STATUS_ACTIVE def get_last_replies(self) -> QuerySet: """ @@ -255,4 +257,7 @@ class Thread(models.Model): return self.get_replies().filter(id__gt=post_id) def is_archived(self): - return self.archived + return self.get_status() == STATUS_ARCHIVE + + def get_status(self): + return self.status diff --git a/boards/rss.py b/boards/rss.py --- a/boards/rss.py +++ b/boards/rss.py @@ -3,6 +3,7 @@ from django.core.urlresolvers import rev from django.shortcuts import get_object_or_404 from boards.models import Post, Tag, Thread from boards import settings +from boards.models.thread import STATUS_ARCHIVE __author__ = 'nekorin' @@ -18,7 +19,7 @@ class AllThreadsFeed(Feed): description_template = 'boards/rss/post.html' def items(self): - return Thread.objects.filter(archived=False).order_by('-id')[:MAX_ITEMS] + return Thread.objects.exclude(status=STATUS_ARCHIVE).order_by('-id')[:MAX_ITEMS] def item_title(self, item): return item.get_opening_post().title @@ -36,7 +37,7 @@ class TagThreadsFeed(Feed): description_template = 'boards/rss/post.html' def items(self, obj): - return obj.get_threads().filter(archived=False).order_by('-id')[:MAX_ITEMS] + return obj.get_threads().exclude(status=STATUS_ARCHIVE).order_by('-id')[:MAX_ITEMS] def get_object(self, request, tag_name): return get_object_or_404(Tag, name=tag_name) diff --git a/boards/templates/boards/post.html b/boards/templates/boards/post.html --- a/boards/templates/boards/post.html +++ b/boards/templates/boards/post.html @@ -21,14 +21,14 @@ and this is an opening post (thread death time) or a post for popup (we don't see OP here so we show the death time in the post itself). {% endcomment %} - {% if thread.archived %} + {% if thread.is_archived %} {% if is_opening %} — {% endif %} {% endif %} {% if is_opening %} {% if need_open_link %} - {% if thread.archived %} + {% if thread.is_archived %} {% trans "Open" %} {% else %} {% trans "Reply" %} @@ -41,7 +41,7 @@ {% endwith %} {% endif %} {% endif %} - {% if reply_link and not thread.archived %} + {% if reply_link and not thread.is_archived %} {% trans 'Reply' %} {% endif %} diff --git a/boards/templates/boards/thread_normal.html b/boards/templates/boards/thread_normal.html --- a/boards/templates/boards/thread_normal.html +++ b/boards/templates/boards/thread_normal.html @@ -38,7 +38,7 @@ {% endfor %} - {% if not thread.archived %} + {% if not thread.is_archived %}