diff --git a/boards/migrations/0026_post_opening.py b/boards/migrations/0026_post_opening.py new file mode 100644 --- /dev/null +++ b/boards/migrations/0026_post_opening.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('boards', '0025_auto_20150825_2049'), + ] + + operations = [ + migrations.AddField( + model_name='post', + name='opening', + field=models.BooleanField(default=False), + preserve_default=False, + ), + ] diff --git a/boards/migrations/0027_auto_20150912_1632.py b/boards/migrations/0027_auto_20150912_1632.py new file mode 100644 --- /dev/null +++ b/boards/migrations/0027_auto_20150912_1632.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + def build_opening_flag(apps, schema_editor): + Post = apps.get_model('boards', 'Post') + for post in Post.objects.all(): + op = Post.objects.filter(threads__in=[post.thread]).order_by('pub_time').first() + post.opening = op.id == post.id + post.save(update_fields=['opening']) + + dependencies = [ + ('boards', '0026_post_opening'), + ] + + operations = [ + migrations.RunPython(build_opening_flag), + ] 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 @@ -20,7 +20,7 @@ from boards import utils from boards.models.post.export import get_exporter, DIFF_TYPE_JSON from boards.models.user import Notification, Ban import boards.models.thread - +from boards.utils import cached_result APP_LABEL_BOARDS = 'boards' @@ -106,7 +106,8 @@ class PostManager(models.Manager): poster_ip=ip, thread=thread, last_edit_time=posting_time, - tripcode=tripcode) + tripcode=tripcode, + opening=new_thread) post.threads.add(thread) logger = logging.getLogger('boards.post.create') @@ -202,6 +203,7 @@ class Post(models.Model, Viewable): uid = models.TextField(db_index=True) tripcode = models.CharField(max_length=50, null=True) + opening = models.BooleanField() def __str__(self): return 'P#{}/{}'.format(self.id, self.title) @@ -241,7 +243,7 @@ class Post(models.Model, Viewable): Checks if this is an opening post or just a reply. """ - return self.get_thread().get_opening_post_id() == self.id + return self.opening def get_absolute_url(self): if self.url: @@ -271,12 +273,6 @@ class Post(models.Model, Viewable): """ thread = self.get_thread() - is_opening = kwargs.get(PARAMETER_IS_OPENING, self.is_opening()) - - if is_opening: - opening_post_id = self.id - else: - opening_post_id = thread.get_opening_post_id() css_class = 'post' if thread.archived: @@ -291,10 +287,9 @@ class Post(models.Model, Viewable): params.update({ PARAMETER_POST: self, - PARAMETER_IS_OPENING: is_opening, + PARAMETER_IS_OPENING: self.is_opening(), PARAMETER_THREAD: thread, PARAMETER_CSS_CLASS: css_class, - PARAMETER_OP_ID: opening_post_id, }) return render_to_string('boards/post.html', params) diff --git a/boards/models/thread.py b/boards/models/thread.py --- a/boards/models/thread.py +++ b/boards/models/thread.py @@ -151,7 +151,8 @@ class Thread(models.Model): """ query = Post.objects.filter(threads__in=[self]) - query = query.order_by('pub_time').prefetch_related('images', 'thread', 'threads') + query = query.order_by('pub_time').prefetch_related( + 'images', 'thread', 'threads', 'attachments') if view_fields_only: query = query.defer('poster_ip') return query.all() diff --git a/boards/templates/boards/all_threads.html b/boards/templates/boards/all_threads.html --- a/boards/templates/boards/all_threads.html +++ b/boards/templates/boards/all_threads.html @@ -93,7 +93,7 @@ {% for thread in threads %}
- {% post_view thread.get_opening_post moderator=moderator is_opening=True thread=thread truncated=True need_open_link=True %} + {% post_view thread.get_opening_post moderator=moderator thread=thread truncated=True need_open_link=True %} {% if not thread.archived %} {% with last_replies=thread.get_last_replies %} {% if last_replies %} @@ -108,7 +108,7 @@ {% endwith %}
{% for post in last_replies %} - {% post_view post is_opening=False moderator=moderator truncated=True %} + {% post_view post moderator=moderator truncated=True %} {% endfor %}
{% endif %} 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 @@ -57,16 +57,12 @@ Post images. Currently only 1 image can be posted and shown, but post model supports multiple. {% endcomment %} - {% if post.images.exists %} - {% with post.images.first as image %} - {{ image.get_view|safe }} - {% endwith %} - {% endif %} - {% if post.attachments.exists %} - {% with post.attachments.first as file %} - {{ file.get_view|safe }} - {% endwith %} - {% endif %} + {% for image in post.images.all %} + {{ image.get_view|safe }} + {% endfor %} + {% for file in post.attachments.all %} + {{ file.get_view|safe }} + {% endfor %} {% comment %} Post message (text) {% endcomment %}