Mercurial > public > sg101
changeset 329:000c006fee97
Various small changes to reduce database hits.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Wed, 23 Feb 2011 03:40:18 +0000 |
parents | 3f9b9fd54b01 |
children | 3c951521e0ec |
files | gpp/bio/models.py gpp/bio/views.py gpp/forums/templatetags/forum_tags.py gpp/forums/views/main.py gpp/templates/base.html gpp/templates/forums/display_post.html |
diffstat | 6 files changed, 57 insertions(+), 37 deletions(-) [+] |
line wrap: on
line diff
--- a/gpp/bio/models.py Tue Feb 22 05:40:29 2011 +0000 +++ b/gpp/bio/models.py Wed Feb 23 03:40:18 2011 +0000 @@ -112,19 +112,7 @@ return ('bio-view_profile', (), {'username': self.user.username}) def badge_ownership(self): - if hasattr(self, '_badges'): - return self._badges - - cache_key = 'badges-%s' % self.user.username - self._badges = cache.get(cache_key) - if self._badges is not None: - return self._badges - - self._badges = BadgeOwnership.objects.filter(profile=self).select_related( - "badge") - - cache.set(cache_key, self._badges, 15 * 60) - return self._badges + return BadgeOwnership.objects.filter(profile=self).select_related('badge') def is_stranger(self): """Returns True if this user profile status is STA_STRANGER."""
--- a/gpp/bio/views.py Tue Feb 22 05:40:29 2011 +0000 +++ b/gpp/bio/views.py Wed Feb 23 03:40:18 2011 +0000 @@ -52,7 +52,7 @@ except InvalidPage: raise Http404 - # Attach user profiles to each post to avoid using get_user_profile() in + # Attach user profiles to each user to avoid using get_user_profile() in # the template. users = set(user.id for user in the_page.object_list) @@ -66,7 +66,7 @@ 'page': the_page, 'type': type, 'num_members': num_members, - }, + }, context_instance = RequestContext(request)) #######################################################################
--- a/gpp/forums/templatetags/forum_tags.py Tue Feb 22 05:40:29 2011 +0000 +++ b/gpp/forums/templatetags/forum_tags.py Wed Feb 23 03:40:18 2011 +0000 @@ -60,6 +60,24 @@ } +def get_time_prefs(user): + """ + Return the supplied user's time preferences in the form of a 2-tuple: + (use_24_time, time_zone_name) + + These preferences are cached to reduce database hits. + + """ + cache_key = '%s_tz_prefs' % user.username + tz_prefs = cache.get(cache_key) + if tz_prefs is None: + profile = user.get_profile() + tz_prefs = profile.use_24_time, profile.time_zone + cache.set(cache_key, tz_prefs) + + return tz_prefs + + @register.simple_tag def current_forum_time(user): """ @@ -69,10 +87,10 @@ curr_time = SERVER_TZ.localize(datetime.datetime.now()) if user.is_authenticated(): - profile = user.get_profile() - user_tz = timezone(profile.time_zone) + tz_prefs = get_time_prefs(user) + user_tz = timezone(tz_prefs[1]) curr_time = curr_time.astimezone(user_tz) - fmt = TIME_FMT_24 if profile.use_24_time else TIME_FMT_12 + fmt = TIME_FMT_24 if tz_prefs[0] else TIME_FMT_12 else: fmt = TIME_FMT_12 @@ -90,10 +108,10 @@ date = SERVER_TZ.localize(date) if user.is_authenticated(): - profile = user.get_profile() - user_tz = timezone(profile.time_zone) + tz_prefs = get_time_prefs(user) + user_tz = timezone(tz_prefs[1]) date = date.astimezone(user_tz) - fmt = DATE_FMT_24 if profile.use_24_time else DATE_FMT_12 + fmt = DATE_FMT_24 if tz_prefs[0] else DATE_FMT_12 else: fmt = DATE_FMT_12
--- a/gpp/forums/views/main.py Tue Feb 22 05:40:29 2011 +0000 +++ b/gpp/forums/views/main.py Wed Feb 23 03:40:18 2011 +0000 @@ -1,6 +1,7 @@ """ Views for the forums application. """ +import collections import datetime from django.contrib.auth.decorators import login_required @@ -29,7 +30,7 @@ from forums.unread import get_forum_unread_status, get_topic_unread_status, \ get_post_unread_status, get_unread_topics -from bio.models import UserProfile +from bio.models import UserProfile, BadgeOwnership import antispam import antispam.utils from forums.attachments import AttachmentProcessor @@ -175,17 +176,28 @@ raise Http404 get_post_unread_status(topic, page.object_list, request.user) - # Attach user profiles to each post to avoid using get_user_profile() in - # the template. + # Attach user profiles to each post's user to avoid using + # get_user_profile() in the template. users = set(post.user.id for post in page.object_list) profiles = UserProfile.objects.filter(user__id__in=users).select_related() user_profiles = dict((profile.user.id, profile) for profile in profiles) for post in page.object_list: - post.user_profile = user_profiles[post.user.id] + post.user.user_profile = user_profiles[post.user.id] post.attach_list = [] + # Attach badge ownership info to the user profiles to avoid lots + # of database hits in the template: + bos_qs = BadgeOwnership.objects.filter( + profile__id__in=user_profiles.keys()).select_related() + bos = collections.defaultdict(list) + for bo in bos_qs: + bos[bo.profile.id].append(bo) + + for pk, profile in user_profiles.iteritems(): + profile.badge_ownership = bos[pk] + # Attach any attachments post_ids = [post.pk for post in page.object_list] attachments = Attachment.objects.filter(post__in=post_ids).select_related( @@ -292,7 +304,7 @@ post = form.save(request.user, request.META.get("REMOTE_ADDR", "")) post.unread = True - post.user_profile = request.user.get_profile() + post.user.user_profile = request.user.get_profile() post.attach_list = post.attachments.all() _bump_post_count(request.user) _update_last_visit(request.user, form.topic) @@ -388,7 +400,7 @@ else: form = PostForm(instance=post, topic_name=topic_name) - post.user_profile = post.user.get_profile() + post.user.user_profile = post.user.get_profile() return render_to_response('forums/edit_post.html', { 'forum': post.topic.forum,
--- a/gpp/templates/base.html Tue Feb 22 05:40:29 2011 +0000 +++ b/gpp/templates/base.html Wed Feb 23 03:40:18 2011 +0000 @@ -75,7 +75,9 @@ {% photo_of_the_day %} {% endcache %} {% shoutbox %} - {% irc_status %} + {% cache 60 irc_block %} + {% irc_status %} + {% endcache %} </div> <div id="content-primary" class="span-19 last">
--- a/gpp/templates/forums/display_post.html Tue Feb 22 05:40:29 2011 +0000 +++ b/gpp/templates/forums/display_post.html Wed Feb 23 03:40:18 2011 +0000 @@ -7,24 +7,24 @@ <a href="{% url 'bio-view_profile' username=post.user.username %}" title="View Profile for {{ post.user.username }}">{{ post.user.username }}</a><br /> <a href="{% url 'bio-view_profile' username=post.user.username %}">{% avatar post.user %}</a> Joined: {{ post.user.date_joined|date:"M d, Y" }}<br /> - Posts: {{ post.user_profile.forum_post_count }}<br /> - {% if post.user_profile.location %} - Location: {{ post.user_profile.location }}<br /> + Posts: {{ post.user.user_profile.forum_post_count }}<br /> + {% if post.user.user_profile.location %} + Location: {{ post.user.user_profile.location }}<br /> {% endif %} - {% for bo in post.user_profile.badge_ownership %} + {% for bo in post.user.user_profile.badge_ownership %} <img src="{{ bo.badge.image.url }}" alt="{{ bo.badge_count_str }}" title="{{ bo.badge_count_str }}" /> {% endfor %} {% if user.is_authenticated %} <p> <a href="{% url 'messages-compose_to' post.user.username %}"> <img src="{{ STATIC_URL }}icons/note.png" alt="PM" title="Send Private Message to {{ post.user.username }}" /></a> - {% if not post.user_profile.hide_email %}<a href="mailto:{{ post.user.email }}"> + {% if not post.user.user_profile.hide_email %}<a href="mailto:{{ post.user.email }}"> <img src="{{ STATIC_URL }}icons/email.png" alt="Email" title="Send Email to {{ post.user.username}}" /></a>{% endif %} </p> {% endif %} </td> <td class="forum-post-body"> - <div class="forum-post-info quiet{% if post.user_profile.is_stranger %} stranger{% endif %}"> + <div class="forum-post-info quiet{% if post.user.user_profile.is_stranger %} stranger{% endif %}"> {% if post.unread %}<img src="{{ STATIC_URL }}icons/new.png" alt="New" title="New" />{% endif %} <a href="{{ post.get_absolute_url }}"><img src="{{ STATIC_URL }}icons/link.png" alt="Link" title="Link to this post" /></a> Posted on {% forum_date post.creation_date user %} @@ -32,8 +32,8 @@ </div> <div class="forum-post-body"> {{ post.html|safe }} - {% if post.user_profile.signature_html %} - —{{ post.user_profile.signature_html|safe }} + {% if post.user.user_profile.signature_html %} + —{{ post.user.user_profile.signature_html|safe }} {% endif %} {% if post.has_been_edited %} <p class="small quiet">Last edited: {{ post.update_date|date:"M d, Y H:i:s" }}</p> @@ -57,7 +57,7 @@ {% if can_moderate %} <a href="#" class="post-delete" id="dp-{{ post.id }}" title="Delete this post"><img src="{{ STATIC_URL }}icons/cross.png" alt="Delete post" /></a> - {% if post.user != user and post.user_profile.is_stranger %} + {% if post.user != user and post.user.user_profile.is_stranger %} <br /> <span class="quiet">Stranger options:</span> <a href="{% url 'forums-stranger' post.id %}" title="This stranger seems legitimate">