# HG changeset patch # User Brian Neal # Date 1319242070 0 # Node ID 3c48a555298d3597f1b9e7e247c1d3775ecb5e8d # Parent 7dbdbb08e68c197d41b62069ca6eaebac2e7e8d5 Added a custom tag to display a link to a profile. Refactored the avatar tag to optionally display a profile link around the image. Removed the width and height attributes from the avatar image tag. I think this was causing disk hits whenever those properties were not cached. The avatar tag is now an inclusion tag. diff -r 7dbdbb08e68c -r 3c48a555298d gpp/bio/templatetags/avatar_tags.py --- a/gpp/bio/templatetags/avatar_tags.py Thu Oct 20 01:00:19 2011 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,70 +0,0 @@ -""" -Template tags for the bio application. -""" -from django import template -from django.conf import settings -from django.core.cache import cache - -from bio.models import UserProfile - - -register = template.Library() - - -def get_img_info(profile=None): - """ - This function returns a 3-tuple: (url, width, height) for a user profile - avatar. If the profile is None or the profile doesn't contain a valid - avatar, info for the default avatar is returned. - - """ - if profile is None or profile.avatar.name == '': - return (settings.AVATAR_DEFAULT_URL, - settings.MAX_AVATAR_SIZE_PIXELS, - settings.MAX_AVATAR_SIZE_PIXELS) - else: - return (profile.avatar.url, - profile.avatar.width, - profile.avatar.height) - - -@register.simple_tag -def avatar(user, align='bottom'): - """ - Returns the HTML for a user's avatar image. - - If the user object has an attribute 'user_profile', this is assumed to be - the user's profile that has been pre-fetched. Otherwise, the cache is - consulted to retrieve the avatar info for the user. If there is a cache - miss, only then will a get_profile() call be made. - """ - # img_info is a tuple that contains info about the avatar: - # (url, width, height) - - if hasattr(user, 'user_profile'): - img_info = get_img_info(user.user_profile) - else: - # try the cache - cache_key = 'avatar_' + user.username - img_info = cache.get(cache_key) - if img_info is None: - try: - profile = user.get_profile() - except UserProfile.DoesNotExist: - profile = None - - img_info = get_img_info(profile) - cache.set(cache_key, img_info) - - alt = user.username - title = alt - - style = '' - if align == 'left': - style = 'style="float:left;margin-right:3px;"' - # other styles not supported - - img_tag = (u'%s') % ( - img_info[0], alt, title, img_info[1], img_info[2], style) - return img_tag diff -r 7dbdbb08e68c -r 3c48a555298d gpp/bio/templatetags/bio_tags.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/bio/templatetags/bio_tags.py Sat Oct 22 00:07:50 2011 +0000 @@ -0,0 +1,82 @@ +""" +Template tags for the bio application. +""" +from django import template +from django.conf import settings +from django.core.cache import cache + +from bio.models import UserProfile + + +register = template.Library() + + +def get_img_url(profile=None): + """ + This function returns a URL for a user profile avatar. + If the profile is None or the profile doesn't contain a valid + avatar, the URL for the default avatar is returned. + + """ + if profile is None or profile.avatar.name == '': + return settings.AVATAR_DEFAULT_URL + else: + return profile.avatar.url + + +@register.inclusion_tag('bio/avatar_tag.html') +def avatar(user, profile_link=True, align='bottom'): + """ + Returns the HTML for a user's avatar image. + + If the user object has an attribute 'user_profile', this is assumed to be + the user's profile that has been pre-fetched. Otherwise, the cache is + consulted to retrieve the avatar info for the user. If there is a cache + miss, only then will a get_profile() call be made. + """ + # img_info is a tuple that contains info about the avatar: + # (url, width, height) + + if hasattr(user, 'user_profile'): + img_url = get_img_url(user.user_profile) + else: + # try the cache + cache_key = 'avatar_' + user.username + img_url = cache.get(cache_key) + if img_url is None: + try: + profile = user.get_profile() + except UserProfile.DoesNotExist: + profile = None + + img_url = get_img_url(profile) + cache.set(cache_key, img_url) + + title = user.username + style = '' + if align == 'left': + style = 'style="float:left;margin-right:3px;"' + # other styles not supported + + return { + 'url': img_url, + 'title': title, + 'style': style, + 'username': user.username, + 'profile_link': profile_link, + } + + +@register.inclusion_tag('bio/profile_link_tag.html') +def profile_link(username, trailing_text=''): + """ + Renders a link to a given user's profile page. + Trailing text is any text that you want displayed after the final tag. + Because of the way the Django template system works, a newline will + automatically be inserted after this tag is expanded. If you want a period + to follow immediately after the link, then set trailing_text to '.'. + Otherwise a space will appear between the linked text and any text that + follows the tag. + + """ + return {'username': username, 'trailing_text': trailing_text } diff -r 7dbdbb08e68c -r 3c48a555298d gpp/templates/bio/avatar.html --- a/gpp/templates/bio/avatar.html Thu Oct 20 01:00:19 2011 +0000 +++ b/gpp/templates/bio/avatar.html Sat Oct 22 00:07:50 2011 +0000 @@ -1,11 +1,11 @@ {% extends 'bio/base.html' %} {% load url from future %} -{% load avatar_tags %} +{% load bio_tags %} {% block title %}Change My Avatar{% endblock %} {% block content %}

Change My Avatar

This is your current avatar:

-

{% avatar user %}

+

{% avatar user 0 %}

To change your avatar, upload a file using the form, below. You may leave the form blank to reset your avatar to the default. diff -r 7dbdbb08e68c -r 3c48a555298d gpp/templates/bio/avatar_tag.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/templates/bio/avatar_tag.html Sat Oct 22 00:07:50 2011 +0000 @@ -0,0 +1,2 @@ +{% load url from future %} +{% if profile_link %}{% endif %}avatar{% if profile_link %}{% endif %} diff -r 7dbdbb08e68c -r 3c48a555298d gpp/templates/bio/edit_profile.html --- a/gpp/templates/bio/edit_profile.html Thu Oct 20 01:00:19 2011 +0000 +++ b/gpp/templates/bio/edit_profile.html Sat Oct 22 00:07:50 2011 +0000 @@ -1,6 +1,6 @@ {% extends 'bio/base.html' %} {% load url from future %} -{% load avatar_tags %} +{% load bio_tags %} {% load elsewhere_tags %} {% block title %}Edit Profile{% endblock %} {% block custom_js %} @@ -15,7 +15,7 @@ Change Avatar Change Avatar - {% avatar user %} + {% avatar user 0 %} {{ user_form.as_table }} {{ profile_form.as_table }} diff -r 7dbdbb08e68c -r 3c48a555298d gpp/templates/bio/members.html --- a/gpp/templates/bio/members.html Thu Oct 20 01:00:19 2011 +0000 +++ b/gpp/templates/bio/members.html Sat Oct 22 00:07:50 2011 +0000 @@ -1,6 +1,6 @@ {% extends 'bio/base.html' %} {% load url from future %} -{% load avatar_tags %} +{% load bio_tags %} {% block title %}Member List{% endblock %} {% block bio_css %} @@ -34,8 +34,8 @@ {% for u in page.object_list %} - {% avatar u %} - {{ u.username }} + {% avatar u %} + {% profile_link u.username %} {{ u.get_full_name }} {{ u.user_profile.location }} {{ u.date_joined|date:"M. d, Y" }} diff -r 7dbdbb08e68c -r 3c48a555298d gpp/templates/bio/profile_link_tag.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/templates/bio/profile_link_tag.html Sat Oct 22 00:07:50 2011 +0000 @@ -0,0 +1,2 @@ +{% load url from future %} +{{ username }}{{ trailing_text }} diff -r 7dbdbb08e68c -r 3c48a555298d gpp/templates/bio/view_profile.html --- a/gpp/templates/bio/view_profile.html Thu Oct 20 01:00:19 2011 +0000 +++ b/gpp/templates/bio/view_profile.html Sat Oct 22 00:07:50 2011 +0000 @@ -1,6 +1,6 @@ {% extends 'bio/base.html' %} {% load url from future %} -{% load avatar_tags %} +{% load bio_tags %} {% load elsewhere_tags %} {% load core_tags %} {% load forum_tags %} @@ -24,7 +24,7 @@ {% endif %} {% if this_is_me %} -

{% avatar subject %} +

{% avatar subject 0 %}

{% else %} -

{% avatar subject %}

+

{% avatar subject 0 %}

{% endif %} diff -r 7dbdbb08e68c -r 3c48a555298d gpp/templates/comments/comment.html --- a/gpp/templates/comments/comment.html Thu Oct 20 01:00:19 2011 +0000 +++ b/gpp/templates/comments/comment.html Sat Oct 22 00:07:50 2011 +0000 @@ -1,9 +1,9 @@ {% load url from future %} -{% load avatar_tags %} +{% load bio_tags %}
{{ forloop.counter }}.
-{% avatar comment.user %} +{% avatar comment.user %}
{% if comment.is_removed %}

This comment has been removed.

@@ -11,8 +11,7 @@
{{ comment.html|safe }}
{% endif %}
-{{ comment.user.username }} | +{% profile_link comment.user.username %} | {{ comment.creation_date|date:"d-M-Y H:i:s" }} {% if not comment.is_removed %} | {{ today|date:"F" }} Birthdays Cake {% endblock %} {% block block_content %} @@ -10,7 +11,7 @@ {% if bday.day == today.day %}{% endif %} {{ bday.day|ordinal }} – {% for profile in bday.profiles %} - {{ profile.user.username }}{% if not forloop.last %}, {% endif %} + {% profile_link profile.user.username %}{% if not forloop.last %}, {% endif %} {% endfor %} {% if bday.day == today.day %}{% endif %} diff -r 7dbdbb08e68c -r 3c48a555298d gpp/templates/core/whos_online_tag.html --- a/gpp/templates/core/whos_online_tag.html Thu Oct 20 01:00:19 2011 +0000 +++ b/gpp/templates/core/whos_online_tag.html Sat Oct 22 00:07:50 2011 +0000 @@ -1,11 +1,12 @@ {% load url from future %} +{% load bio_tags %}
There {{ total|pluralize:"is,are"}} {{ total }} user{{ total|pluralize }} online: {{ num_users }} registered user{{ num_users|pluralize }} and {{ num_guests }} guest{{ num_guests|pluralize }}. {% if num_users %} Registered users:
    {% for user in users %} -
  • {{ user }}
  • +
  • {% profile_link user %}
  • {% endfor %}
{% endif %} diff -r 7dbdbb08e68c -r 3c48a555298d gpp/templates/donations/index.html --- a/gpp/templates/donations/index.html Thu Oct 20 01:00:19 2011 +0000 +++ b/gpp/templates/donations/index.html Sat Oct 22 00:07:50 2011 +0000 @@ -1,5 +1,6 @@ {% extends 'base.html' %} {% load url from future %} +{% load bio_tags %} {% block title %}Donations{% endblock %} {% block content %}

Donations

@@ -34,7 +35,7 @@ {{ anonymous }} {% else %} {% if donation.user %} - {{ donation.user.username }} + {% profile_link donation.user.username %} {% else %} {{ donation.donor }} {% endif %} diff -r 7dbdbb08e68c -r 3c48a555298d gpp/templates/downloads/download.html --- a/gpp/templates/downloads/download.html Thu Oct 20 01:00:19 2011 +0000 +++ b/gpp/templates/downloads/download.html Sat Oct 22 00:07:50 2011 +0000 @@ -1,5 +1,6 @@ {% load url from future %} {% load comment_tags %} +{% load bio_tags %} {% get_comment_count for download as comment_count %}
{{ download.title }} @@ -9,7 +10,7 @@
Full Name{{ subject.get_full_name }}
- + diff -r 7dbdbb08e68c -r 3c48a555298d gpp/templates/forums/display_post.html --- a/gpp/templates/forums/display_post.html Thu Oct 20 01:00:19 2011 +0000 +++ b/gpp/templates/forums/display_post.html Sat Oct 22 00:07:50 2011 +0000 @@ -1,11 +1,11 @@ {% load url from future %} -{% load avatar_tags %} +{% load bio_tags %} {% load forum_tags %} - + - + diff -r 7dbdbb08e68c -r 3c48a555298d gpp/templates/forums/post_ip.html --- a/gpp/templates/forums/post_ip.html Thu Oct 20 01:00:19 2011 +0000 +++ b/gpp/templates/forums/post_ip.html Sat Oct 22 00:07:50 2011 +0000 @@ -1,19 +1,19 @@ {% extends 'base.html' %} {% load url from future %} +{% load bio_tags %} {% block title %}Post IP Address Info: {{ post.user_ip }}{% endblock %} {% block content %}

Post IP Address Info: {{ post.user_ip }}

This post was created by -{{ post.user.username }} from the IP address +{% profile_link post.user.username %} from the IP address {{ post.user_ip }}.

{% if ip_users %}

All users who have posted from {{ post.user_ip }}:

{% endif %} diff -r 7dbdbb08e68c -r 3c48a555298d gpp/templates/forums/spammer.html --- a/gpp/templates/forums/spammer.html Thu Oct 20 01:00:19 2011 +0000 +++ b/gpp/templates/forums/spammer.html Sat Oct 22 00:07:50 2011 +0000 @@ -1,12 +1,13 @@ {% extends 'base.html' %} {% load url from future %} +{% load bio_tags %} {% block title %}Deactivate Spammer: {{ post.user.username }}{% endblock %} {% block content %}

Deactivate Spammer: {{ post.user.username }}

{% if can_moderate and can_deactivate %}

Please confirm that you wish to mark the user -{{ post.user.username }} as a +{% profile_link post.user.username %} as a spammer based on this post. If you confirm, the user's account will be deactivated, and all posts and comments left by the user will be deleted.

diff -r 7dbdbb08e68c -r 3c48a555298d gpp/templates/forums/spammer_nailed.html --- a/gpp/templates/forums/spammer_nailed.html Thu Oct 20 01:00:19 2011 +0000 +++ b/gpp/templates/forums/spammer_nailed.html Sat Oct 22 00:07:50 2011 +0000 @@ -1,17 +1,18 @@ {% extends 'base.html' %} {% load url from future %} +{% load bio_tags %} {% block title %}Spammer Nailed: {{ spammer.username }}{% endblock %} {% block content %}

Spammer Nailed: {{ spammer.username }}

{% if success %} -The user {{ spammer.username }} +The user {% profile_link spammer.username %} has had his/her account deactivated for spamming. All forum posts and comments this user has made have been deleted. The site admin has been notified of this action. Thanks for helping to keep our site spam-free! {% else %} Whoops, something went wrong deactivating the account of -{{ spammer.username }}. +{% profile_link spammer.username '.' %} Or, possibly some time has passed and the account was reinstated. If you have any questions, contact the site admin. {% endif %} diff -r 7dbdbb08e68c -r 3c48a555298d gpp/templates/forums/stranger.html --- a/gpp/templates/forums/stranger.html Thu Oct 20 01:00:19 2011 +0000 +++ b/gpp/templates/forums/stranger.html Sat Oct 22 00:07:50 2011 +0000 @@ -1,5 +1,6 @@ {% extends 'base.html' %} {% load url from future %} +{% load bio_tags %} {% block title %}Promote Stranger: {{ post.user.username }}{% endblock %} {% block content %}

Promote Stranger: {{ post.user.username }}

@@ -12,7 +13,7 @@ and moderators won't be able to deactivate them on the spot. You may wish to wait until the user has posted at least 10 times before making your decision.

Please confirm that you wish to promote the new user -{{ post.user.username }} from +{% profile_link post.user.username %} from stranger status based on this post.

{% csrf_token %} diff -r 7dbdbb08e68c -r 3c48a555298d gpp/templates/forums/topic_list.html --- a/gpp/templates/forums/topic_list.html Thu Oct 20 01:00:19 2011 +0000 +++ b/gpp/templates/forums/topic_list.html Sat Oct 22 00:07:50 2011 +0000 @@ -1,5 +1,6 @@ {% extends 'base.html' %} {% load url from future %} +{% load bio_tags %} {% load forum_tags %} {% block title %}Forums: {{ title }}{% endblock %} {% block custom_js %} @@ -33,7 +34,7 @@ {% endif %}
Added By:{{ download.user.username }}{% profile_link download.user.username %} Date:{{ download.date_added|date:"M d, Y" }} Size:{{ download.size }}
{{ topic.reply_count }}{{ topic.user.username }}{% profile_link topic.user.username %} {{ topic.view_count }} {% last_post_info topic.last_post %} diff -r 7dbdbb08e68c -r 3c48a555298d gpp/templates/forums/forum_stats_tag.html --- a/gpp/templates/forums/forum_stats_tag.html Thu Oct 20 01:00:19 2011 +0000 +++ b/gpp/templates/forums/forum_stats_tag.html Sat Oct 22 00:07:50 2011 +0000 @@ -1,8 +1,9 @@ {% load url from future %} +{% load bio_tags %} {% load humanize %}
Our {{ user_count|intcomma }} users have posted a total of {{ post_count|intcomma }} posts.
-The newest registered user is {{ latest_user }}. +Our newest registered user is {% profile_link latest_user '.' %} {% if stats %}
The most users ever online was {{ stats.max_users }} on {{ stats.max_users_date|date:"P l, N d, Y" }}. diff -r 7dbdbb08e68c -r 3c48a555298d gpp/templates/forums/last_post_info.html --- a/gpp/templates/forums/last_post_info.html Thu Oct 20 01:00:19 2011 +0000 +++ b/gpp/templates/forums/last_post_info.html Sat Oct 22 00:07:50 2011 +0000 @@ -1,9 +1,10 @@ {% load url from future %} +{% load bio_tags %} {% load forum_tags %} {% if post %} Goto last post {% forum_date post.creation_date user %}
-{{ post.user.username }} +{% profile_link post.user.username %} {% else %} No posts {% endif %} diff -r 7dbdbb08e68c -r 3c48a555298d gpp/templates/forums/mod_forum.html --- a/gpp/templates/forums/mod_forum.html Thu Oct 20 01:00:19 2011 +0000 +++ b/gpp/templates/forums/mod_forum.html Sat Oct 22 00:07:50 2011 +0000 @@ -1,5 +1,6 @@ {% extends 'base.html' %} {% load url from future %} +{% load bio_tags %} {% load forum_tags %} {% block title %}Moderate Forum: {{ forum.name }}{% endblock %} {% block custom_js %} @@ -29,7 +30,7 @@ class="forums-topic-icon" />{% endif %}

{{ topic.name }}

{{ topic.reply_count }}{{ topic.user.username }}{% profile_link topic.user.username %} {% last_post_info topic.last_post %}
- {{ topic.user.username }} + {% profile_link topic.user.username %} {{ topic.reply_count }} diff -r 7dbdbb08e68c -r 3c48a555298d gpp/templates/membermap/balloon.html --- a/gpp/templates/membermap/balloon.html Thu Oct 20 01:00:19 2011 +0000 +++ b/gpp/templates/membermap/balloon.html Sat Oct 22 00:07:50 2011 +0000 @@ -1,7 +1,8 @@ {% load url from future %} +{% load bio_tags %} {% if avatar_url %} {{ user.name }} {% endif %} -{{ user.name }}:
+{% profile_link user.name %}:
{{ user.message|safe }} diff -r 7dbdbb08e68c -r 3c48a555298d gpp/templates/membermap/markdown.html --- a/gpp/templates/membermap/markdown.html Thu Oct 20 01:00:19 2011 +0000 +++ b/gpp/templates/membermap/markdown.html Sat Oct 22 00:07:50 2011 +0000 @@ -1,5 +1,5 @@ {% load url from future %} {% load markup %} {% load smiley_tags %} -{% load avatar_tags %} -{% avatar user "left" %}{{ user.username }}:
{{ msg|safe }} +{% load bio_tags %} +{% avatar user 0 "left" %}{% profile_link user.username %}:
{{ msg|safe }} diff -r 7dbdbb08e68c -r 3c48a555298d gpp/templates/potd/view.html --- a/gpp/templates/potd/view.html Thu Oct 20 01:00:19 2011 +0000 +++ b/gpp/templates/potd/view.html Sat Oct 22 00:07:50 2011 +0000 @@ -1,5 +1,6 @@ {% extends 'base.html' %} {% load url from future %} +{% load bio_tags %} {% load core_tags %} {% load comment_tags %} {% load script_tags %} @@ -29,8 +30,7 @@

{{ potd.caption }}

Submitted by -{{ potd.user.username }} -on {{ potd.date_added|date:"d F Y" }}.
+{% profile_link potd.user.username %} on {{ potd.date_added|date:"d F Y" }}.
This photo has been Photo of the Day {{ potd.potd_count }} time{{ potd.potd_count|pluralize }}.

{{ potd.description|safe }}
diff -r 7dbdbb08e68c -r 3c48a555298d gpp/templates/shoutbox/shout_detail.html --- a/gpp/templates/shoutbox/shout_detail.html Thu Oct 20 01:00:19 2011 +0000 +++ b/gpp/templates/shoutbox/shout_detail.html Sat Oct 22 00:07:50 2011 +0000 @@ -1,9 +1,9 @@ {% load url from future %} -{% load avatar_tags %} +{% load bio_tags %}
-{% avatar shout.user %}
-{{ shout.user.username }} +{% avatar shout.user %}
+{% profile_link shout.user.username %}
{{ shout.html|safe }}
diff -r 7dbdbb08e68c -r 3c48a555298d gpp/templates/shoutbox/view.html --- a/gpp/templates/shoutbox/view.html Thu Oct 20 01:00:19 2011 +0000 +++ b/gpp/templates/shoutbox/view.html Sat Oct 22 00:07:50 2011 +0000 @@ -1,5 +1,5 @@ {% extends 'base.html' %} -{% load avatar_tags %} +{% load bio_tags %} {% load script_tags %} {% block custom_css %} diff -r 7dbdbb08e68c -r 3c48a555298d gpp/templates/weblinks/link.html --- a/gpp/templates/weblinks/link.html Thu Oct 20 01:00:19 2011 +0000 +++ b/gpp/templates/weblinks/link.html Sat Oct 22 00:07:50 2011 +0000 @@ -1,4 +1,5 @@ {% load url from future %} +{% load bio_tags %}

{{ link.title }}

@@ -7,7 +8,7 @@ {% csrf_token %} - +