Skip to content

Commit

Permalink
some performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
GeyseR committed May 26, 2013
1 parent 2089e1c commit a25ffc2
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 11 deletions.
14 changes: 5 additions & 9 deletions pybb/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,13 @@ def get_absolute_url(self):
def posts(self):
return Post.objects.filter(topic__forum=self).select_related()

def get_last_post(self):
@property
def last_post(self):
try:
return self.posts.order_by('-created')[0]
except IndexError:
return None

@property
def last_post(self):
return self.get_last_post()

def get_parents(self):
"""
Used in templates for breadcrumb building
Expand Down Expand Up @@ -194,12 +191,11 @@ def head(self):
return None
return self._head[0]

def get_last_post(self):
return self.posts.order_by('-created').select_related('user')[0]

@property
def last_post(self):
return self.get_last_post()
if not getattr(self, '_last_post', None):
self._last_post = self.posts.order_by('-created').select_related('user')[0]
return self._last_post

def get_absolute_url(self):
return reverse('pybb:topic', kwargs={'pk': self.id})
Expand Down
2 changes: 1 addition & 1 deletion pybb/templates/pybb/user.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ <h1>{{ target_user }}</h1>
<div>
<a href="{% url 'pybb:user_posts' target_user.get_username %}">
{% trans "Number of posts" %}:
{{ target_user.posts.all.count }}
{{ target_profile.post_count }}
</a>
</div>
<div>{% trans "Date of registration" %}: {% pybb_time target_user.date_joined %}</div>
Expand Down
2 changes: 1 addition & 1 deletion pybb/templates/pybb/users.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ <h1>{% trans "Users" %}</h1>
{% for profile in page.object_list %}
<li class="user">
<span class="author">{{ profile }}</span>
<span class="post_count">{{ profile.posts.count }}</span>
<span class="post_count">{{ profile.post_count }}</span>
<span class="updated">{{ profile.date_joined|date:"d M Y" }}</span>
</li>
{% endfor %}
Expand Down
2 changes: 2 additions & 0 deletions pybb/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ def get_queryset(self):
self.topic.views += 1
self.topic.save()
qs = self.topic.posts.all().select_related('user')
if defaults.PYBB_PROFILE_RELATED_NAME:
qs = qs.select_related('user__%s' % defaults.PYBB_PROFILE_RELATED_NAME)
if not perms.may_moderate_topic(self.request.user, self.topic):
qs = perms.filter_posts(self.request.user, qs)
return qs
Expand Down

0 comments on commit a25ffc2

Please sign in to comment.