# HG changeset patch # User Brian Neal # Date 1298432418 0 # Node ID 000c006fee97acd496651720e33df80bf40c9c0d # Parent 3f9b9fd54b019a8891c59207ef2711cf2183888d Various small changes to reduce database hits. diff -r 3f9b9fd54b01 -r 000c006fee97 gpp/bio/models.py --- 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.""" diff -r 3f9b9fd54b01 -r 000c006fee97 gpp/bio/views.py --- 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)) ####################################################################### diff -r 3f9b9fd54b01 -r 000c006fee97 gpp/forums/templatetags/forum_tags.py --- 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 diff -r 3f9b9fd54b01 -r 000c006fee97 gpp/forums/views/main.py --- 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, diff -r 3f9b9fd54b01 -r 000c006fee97 gpp/templates/base.html --- 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 %}
diff -r 3f9b9fd54b01 -r 000c006fee97 gpp/templates/forums/display_post.html --- 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 @@ {{ post.user.username }}
{% avatar post.user %} Joined: {{ post.user.date_joined|date:"M d, Y" }}
- Posts: {{ post.user_profile.forum_post_count }}
- {% if post.user_profile.location %} - Location: {{ post.user_profile.location }}
+ Posts: {{ post.user.user_profile.forum_post_count }}
+ {% if post.user.user_profile.location %} + Location: {{ post.user.user_profile.location }}
{% endif %} - {% for bo in post.user_profile.badge_ownership %} + {% for bo in post.user.user_profile.badge_ownership %} {{ bo.badge_count_str }} {% endfor %} {% if user.is_authenticated %}

PM - {% if not post.user_profile.hide_email %} + {% if not post.user.user_profile.hide_email %} Email{% endif %}

{% endif %} -