##// END OF EJS Templates
Save "opening post" flag with the post itself and don't count it every time. Speed up getting posts with attachments and images
neko259 -
r1337:4c8c3ec5 default
parent child Browse files
Show More
@@ -0,0 +1,20 b''
1 # -*- coding: utf-8 -*-
2 from __future__ import unicode_literals
3
4 from django.db import models, migrations
5
6
7 class Migration(migrations.Migration):
8
9 dependencies = [
10 ('boards', '0025_auto_20150825_2049'),
11 ]
12
13 operations = [
14 migrations.AddField(
15 model_name='post',
16 name='opening',
17 field=models.BooleanField(default=False),
18 preserve_default=False,
19 ),
20 ]
@@ -0,0 +1,22 b''
1 # -*- coding: utf-8 -*-
2 from __future__ import unicode_literals
3
4 from django.db import migrations
5
6
7 class Migration(migrations.Migration):
8
9 def build_opening_flag(apps, schema_editor):
10 Post = apps.get_model('boards', 'Post')
11 for post in Post.objects.all():
12 op = Post.objects.filter(threads__in=[post.thread]).order_by('pub_time').first()
13 post.opening = op.id == post.id
14 post.save(update_fields=['opening'])
15
16 dependencies = [
17 ('boards', '0026_post_opening'),
18 ]
19
20 operations = [
21 migrations.RunPython(build_opening_flag),
22 ]
@@ -20,7 +20,7 b' from boards import utils'
20 20 from boards.models.post.export import get_exporter, DIFF_TYPE_JSON
21 21 from boards.models.user import Notification, Ban
22 22 import boards.models.thread
23
23 from boards.utils import cached_result
24 24
25 25 APP_LABEL_BOARDS = 'boards'
26 26
@@ -106,7 +106,8 b' class PostManager(models.Manager):'
106 106 poster_ip=ip,
107 107 thread=thread,
108 108 last_edit_time=posting_time,
109 tripcode=tripcode)
109 tripcode=tripcode,
110 opening=new_thread)
110 111 post.threads.add(thread)
111 112
112 113 logger = logging.getLogger('boards.post.create')
@@ -202,6 +203,7 b' class Post(models.Model, Viewable):'
202 203 uid = models.TextField(db_index=True)
203 204
204 205 tripcode = models.CharField(max_length=50, null=True)
206 opening = models.BooleanField()
205 207
206 208 def __str__(self):
207 209 return 'P#{}/{}'.format(self.id, self.title)
@@ -241,7 +243,7 b' class Post(models.Model, Viewable):'
241 243 Checks if this is an opening post or just a reply.
242 244 """
243 245
244 return self.get_thread().get_opening_post_id() == self.id
246 return self.opening
245 247
246 248 def get_absolute_url(self):
247 249 if self.url:
@@ -271,12 +273,6 b' class Post(models.Model, Viewable):'
271 273 """
272 274
273 275 thread = self.get_thread()
274 is_opening = kwargs.get(PARAMETER_IS_OPENING, self.is_opening())
275
276 if is_opening:
277 opening_post_id = self.id
278 else:
279 opening_post_id = thread.get_opening_post_id()
280 276
281 277 css_class = 'post'
282 278 if thread.archived:
@@ -291,10 +287,9 b' class Post(models.Model, Viewable):'
291 287
292 288 params.update({
293 289 PARAMETER_POST: self,
294 PARAMETER_IS_OPENING: is_opening,
290 PARAMETER_IS_OPENING: self.is_opening(),
295 291 PARAMETER_THREAD: thread,
296 292 PARAMETER_CSS_CLASS: css_class,
297 PARAMETER_OP_ID: opening_post_id,
298 293 })
299 294
300 295 return render_to_string('boards/post.html', params)
@@ -151,7 +151,8 b' class Thread(models.Model):'
151 151 """
152 152
153 153 query = Post.objects.filter(threads__in=[self])
154 query = query.order_by('pub_time').prefetch_related('images', 'thread', 'threads')
154 query = query.order_by('pub_time').prefetch_related(
155 'images', 'thread', 'threads', 'attachments')
155 156 if view_fields_only:
156 157 query = query.defer('poster_ip')
157 158 return query.all()
@@ -93,7 +93,7 b''
93 93
94 94 {% for thread in threads %}
95 95 <div class="thread">
96 {% post_view thread.get_opening_post moderator=moderator is_opening=True thread=thread truncated=True need_open_link=True %}
96 {% post_view thread.get_opening_post moderator=moderator thread=thread truncated=True need_open_link=True %}
97 97 {% if not thread.archived %}
98 98 {% with last_replies=thread.get_last_replies %}
99 99 {% if last_replies %}
@@ -108,7 +108,7 b''
108 108 {% endwith %}
109 109 <div class="last-replies">
110 110 {% for post in last_replies %}
111 {% post_view post is_opening=False moderator=moderator truncated=True %}
111 {% post_view post moderator=moderator truncated=True %}
112 112 {% endfor %}
113 113 </div>
114 114 {% endif %}
@@ -57,16 +57,12 b''
57 57 Post images. Currently only 1 image can be posted and shown, but post model
58 58 supports multiple.
59 59 {% endcomment %}
60 {% if post.images.exists %}
61 {% with post.images.first as image %}
62 {{ image.get_view|safe }}
63 {% endwith %}
64 {% endif %}
65 {% if post.attachments.exists %}
66 {% with post.attachments.first as file %}
67 {{ file.get_view|safe }}
68 {% endwith %}
69 {% endif %}
60 {% for image in post.images.all %}
61 {{ image.get_view|safe }}
62 {% endfor %}
63 {% for file in post.attachments.all %}
64 {{ file.get_view|safe }}
65 {% endfor %}
70 66 {% comment %}
71 67 Post message (text)
72 68 {% endcomment %}
General Comments 0
You need to be logged in to leave comments. Login now