My favorites | Sign in
Project Home Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
  Advanced search   Search tips   Subscriptions

Issue 26 attachment: optimize.diff (8.3 KB)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
Index: views.py
===================================================================
--- views.py (revision 20)
+++ views.py (working copy)
@@ -26,7 +26,7 @@
return render_to_response('forum/thread_list.html',
RequestContext(request, {
'forum': f,
- 'threads': f.thread_set.all()
+ 'threads': f.thread_set.select_related('latest_post', 'latest_post__author').all()
}))

def thread(request, thread):
@@ -35,11 +35,11 @@
posts for that thread, in chronological order.
"""
t = get_object_or_404(Thread, pk=thread)
- p = t.post_set.all().order_by('time')
+ p = t.post_set.select_related('author').order_by('time')
s = t.subscription_set.filter(author=request.user)

t.views += 1
- t.save()
+ t.save(update_forum=False)

return render_to_response('forum/thread.html',
RequestContext(request, {
Index: models.py
===================================================================
--- models.py (revision 20)
+++ models.py (working copy)
@@ -26,7 +26,18 @@
description = models.TextField(_("Description"))
threads = models.IntegerField(_("Threads"), default=0)
posts = models.IntegerField(_("Posts"), default=0)
+ latest_post = models.OneToOneField('Post', blank=True, null=True)

+ def get_latest_post(self):
+ """
+ Fetches latest post along with its author and thread to reduce sql queries number
+ """
+ try:
+ latest_post = Post.objects.filter(pk=self.latest_post_id).select_related('author', 'thread')[0]
+ except IndexError:
+ latest_post = None
+ return latest_post
+
def _get_forum_latest_post(self):
"""This gets the latest post for the forum"""
if not hasattr(self, '__forum_latest_post'):
@@ -154,25 +165,16 @@
posts = models.IntegerField(_("Posts"), default=0)
views = models.IntegerField(_("Views"), default=0)
latest_post_time = models.DateTimeField(_("Latest Post Time"), blank=True, null=True)
+ latest_post = models.OneToOneField('Post', related_name='thread_latest_post', blank=True, null=True)

- def _get_thread_latest_post(self):
- """This gets the latest post for the thread"""
- if not hasattr(self, '__thread_latest_post'):
- try:
- self.__thread_latest_post = Post.objects.filter(thread__pk=self.id).latest("time")
- except Post.DoesNotExist:
- self.__thread_latest_post = None
-
- return self.__thread_latest_post
- thread_latest_post = property(_get_thread_latest_post)
-
class Meta:
ordering = ('-sticky', '-latest_post_time')

- def save(self):
- f = self.forum
- f.threads = f.thread_set.count()
- f.save()
+ def save(self, update_forum=True):
+ if update_forum:
+ f = self.forum
+ f.threads = f.thread_set.count()
+ f.save()
super(Thread, self).save()

def delete(self):
@@ -209,34 +211,44 @@

super(Post, self).save()

+ # save info about the latest post
t = self.thread
- t.latest_post_time = t.post_set.latest('time').time
+ latest_post = t.post_set.latest('time')
+ t.latest_post = latest_post
+ t.latest_post_time = latest_post.time
t.posts = t.post_set.count()
t.save()

- f = self.thread.forum
+ f = t.forum
f.threads = f.thread_set.count()
f.posts = Post.objects.filter(thread__forum__pk=f.id).count()
+ f.latest_post = latest_post
f.save()

def delete(self):
- try:
- latest_post = Post.objects.execlude(pk=self.id).latest('time')
- latest_post_time = latest_post.time
- except Post.DoesNotExist:
- latest_post_time = None

t = self.thread
t.posts = t.post_set.exclude(pk=self.id).count()
- t.latest_post_time = latest_post_time
+ try:
+ latest_post = t.post_set.exclude(pk=self.id).latest('time')
+ t.latest_post = latest_post
+ t.latest_post_time = latest_post.time
+ except Post.DoesNotExist:
+ t.latest_post = None
+ t.latest_post_time = None
t.save()

- f = self.thread.forum
+ f = t.forum
f.posts = Post.objects.filter(thread__forum__pk=f.id).exclude(pk=self.id).count()
+ try:
+ latest_post = Post.objects.filter(thread__forum__pk=f.id).exclude(pk=self.id).latest('time')
+ f.latest_post = latest_post
+ except Post.DoesNotExist:
+ f.latest_post = None
f.save()

super(Post, self).delete()
-
+
class Meta:
ordering = ('-time',)

Index: templates/forum/thread_list.html
===================================================================
--- templates/forum/thread_list.html (revision 20)
+++ templates/forum/thread_list.html (working copy)
@@ -14,24 +14,28 @@

{% block content %}

-{% if forum.child.all %}
+{% with forum.child.all as subforums %}
+{% if subforums %}
<table id='djangoForumList'>
<tr>
<th>Sub-Forum</th>
<th>Last Post</th>
</tr>
-
-{% for subforum in forum.child.all %}
+{% for subforum in subforums %}
<tr>
<td class='djangoForumListDetails'><p><strong><a href='{{ subforum.get_absolute_url }}'>{{ subforum.title }}</a></strong><br /><span class='djangoForumStats'>{{ subforum.threads }} thread{{ subforum.threads|pluralize }}, {{ subforum.posts }} post{{ subforum.posts|pluralize }}</span></p>
<p>{{ subforum.description }}</p></td>
-<td class='djangoForumListLastPost'>{% if subforum.forum_latest_post %}{{ subforum.forum_latest_post.time|timesince }} ago by {{ subforum.forum_latest_post.author }} (<a href='{{ subforum.forum_latest_post.get_absolute_url }}'>view</a>){% else %}No Posts{% endif %}</td>
+{% with subforum.get_latest_post as latest_post %}
+<td class='djangoForumListLastPost'>{% if latest_post %}{{ latest_post.time|timesince }} ago by {{ latest_post.author }} (<a href='{{ latest_post.get_absolute_url }}'>view</a>){% else %}No Posts{% endif %}</td>
+{% endwith %}
</tr>
{% endfor %}
</table>
<br />
{% endif %}
+{% endwith %}

+
<table id='djangoForumThreadList'>

<tr>
@@ -46,7 +50,7 @@
<td>{% if t.sticky %}Sticky {% endif %}<a href='{{ t.get_absolute_url }}'>{{ t.title|escape }}</a>{% if t.closed %} (Closed){% endif %}</td>
<td style='width: 50px;'>{{ t.posts }}</td>
<td style='width: 50px;'>{{ t.views }}</td>
-<td style='width: 220px;' class='djangoForumThreadLastPost'>{{ t.thread_latest_post.time|timesince }} ago by {{ t.thread_latest_post.author }} (<a href='{{ t.thread_latest_post.get_absolute_url }}'>view</a>)</td>
+<td style='width: 220px;' class='djangoForumThreadLastPost'>{{ t.latest_post_time|timesince }} ago by {{ t.latest_post_author }} (<a href='{{ t.latest_post_url }}'>view</a>)</td>
</tr>
{% endfor %}
</table>
Index: templates/forum/forum_list.html
===================================================================
--- templates/forum/forum_list.html (revision 20)
+++ templates/forum/forum_list.html (working copy)
@@ -11,9 +11,11 @@

{% for forum in object_list %}
<tr>
-<td class='djangoForumListDetails'><p><strong><a href='{{ forum.get_absolute_url }}'>{{ forum.title }}</a></strong><br /><span class='djangoForumStats'>{% blocktrans with forum.threads as thread_count and forum.posts as post_count %}{{ thread_count }} threads, {{ post_count }} posts{% endblock %}</span></p>
+<td class='djangoForumListDetails'><p><strong><a href='{{ forum.get_absolute_url }}'>{{ forum.title }}</a></strong><br /><span class='djangoForumStats'>{% blocktrans with forum.threads as thread_count and forum.posts as post_count %}{{ thread_count }} threads, {{ post_count }} posts{% endblocktrans %}</span></p>
<p>{{ forum.description }}</p></td>
-<td class='djangoForumListLastPost'>{% if forum.forum_latest_post %}{{ forum.forum_latest_post.time|timesince }} ago by {{ forum.forum_latest_post.author }} (<a href='{{ forum.forum_latest_post.get_absolute_url }}'>{% trans "view" %}</a>){% else %}{% trans "No Posts" %}{% endif %}</td>
+{% with forum.get_latest_post as latest_post %}
+<td class='djangoForumListLastPost'>{% if latest_post %}{{ latest_post.time|timesince }} ago by {{ latest_post.author.username }} (<a href='{{ latest_post.get_absolute_url }}'>{% trans "view" %}</a>){% else %}{% trans "No Posts" %}{% endif %}</td>
+{% endwith %}
</tr>
{% endfor %}
</table>
Powered by Google Project Hosting