# HG changeset patch # User Brian Neal # Date 1256507728 0 # Node ID f8f4514b806a17f7154a2397d0a2fc228aa42448 # Parent b8f1dcc9fae43c43067bcbb8b06179fe1373c57d Added a 24-hour time preference flag in the user profile. Added forum template tags for showing forum dates/times adjusted for the user's time zone. diff -r b8f1dcc9fae4 -r f8f4514b806a gpp/bio/forms.py --- a/gpp/bio/forms.py Sat Oct 24 02:39:54 2009 +0000 +++ b/gpp/bio/forms.py Sun Oct 25 21:55:28 2009 +0000 @@ -31,10 +31,11 @@ occupation = forms.CharField(required=False, widget=forms.TextInput(attrs={'size' : 64 })) interests = forms.CharField(required=False, widget=forms.TextInput(attrs={'size' : 64 })) time_zone = forms.CharField(required=False, widget=forms.HiddenInput()) + use_24_time = forms.BooleanField(label='Show times in 24-hour mode', required=False) class Meta: model = UserProfile - exclude = ('user', 'avatar', 'profile_html', 'signature_html') + exclude = ('user', 'avatar', 'profile_html', 'signature_html', 'forum_post_count') class Media: css = { diff -r b8f1dcc9fae4 -r f8f4514b806a gpp/bio/models.py --- a/gpp/bio/models.py Sat Oct 24 02:39:54 2009 +0000 +++ b/gpp/bio/models.py Sun Oct 25 21:55:28 2009 +0000 @@ -36,6 +36,7 @@ avatar = models.ImageField(upload_to=avatar_file_path, blank=True) time_zone = models.CharField(max_length=64, blank=True, default='US/Pacific') + use_24_time = models.BooleanField(default=False) forum_post_count = models.IntegerField(default=0) def __unicode__(self): diff -r b8f1dcc9fae4 -r f8f4514b806a gpp/forums/templatetags/forum_tags.py --- a/gpp/forums/templatetags/forum_tags.py Sat Oct 24 02:39:54 2009 +0000 +++ b/gpp/forums/templatetags/forum_tags.py Sun Oct 25 21:55:28 2009 +0000 @@ -1,16 +1,31 @@ """ Template tags for the forums application. """ +import datetime + +from pytz import timezone from django import template +from django.conf import settings register = template.Library() -@register.inclusion_tag('forums/last_post_info.html') -def last_post_info(post, media_url): +TIME_FMT_24 = "%H:%M" +TIME_FMT_12 = "%I:%M %p" + +DATE_FMT = "%b %d %Y" +DATE_FMT_24 = "%s %s" % (DATE_FMT, TIME_FMT_24) +DATE_FMT_12 = "%s %s" % (DATE_FMT, TIME_FMT_12) + +SERVER_TZ = timezone(settings.TIME_ZONE) + + +@register.inclusion_tag('forums/last_post_info.html', takes_context=True) +def last_post_info(context, post): return { 'post': post, - 'MEDIA_URL': media_url, + 'MEDIA_URL': context['MEDIA_URL'], + 'user': context['user'], } @@ -27,3 +42,41 @@ 'show_button': show_button, 'MEDIA_URL': media_url, } + + +@register.simple_tag +def current_forum_time(user): + """ + This tag displays the current forum time, adjusted by the user's + time zone preferences. + """ + curr_time = SERVER_TZ.localize(datetime.datetime.now()) + + if user.is_authenticated(): + profile = user.get_profile() + user_tz = timezone(profile.time_zone) + curr_time = curr_time.astimezone(user_tz) + fmt = TIME_FMT_24 if profile.use_24_time else TIME_FMT_12 + else: + fmt = TIME_FMT_12 + + return 'The current time is %s. All times shown are %s.' % ( + curr_time.strftime(fmt), curr_time.strftime('%Z%z')) + + +@register.simple_tag +def forum_date(date, user): + """ + This tag displays an arbitrary datetime, adjusted by the user's + time zone preferences. + """ + date = SERVER_TZ.localize(date) + if user.is_authenticated(): + profile = user.get_profile() + user_tz = timezone(profile.time_zone) + date = date.astimezone(user_tz) + fmt = DATE_FMT_24 if profile.use_24_time else DATE_FMT_12 + else: + fmt = DATE_FMT_12 + + return date.strftime(fmt) diff -r b8f1dcc9fae4 -r f8f4514b806a gpp/forums/views.py --- a/gpp/forums/views.py Sat Oct 24 02:39:54 2009 +0000 +++ b/gpp/forums/views.py Sun Oct 25 21:55:28 2009 +0000 @@ -218,6 +218,7 @@ return render_to_response('forums/display_post.html', { 'post': post, 'can_moderate': _can_moderate(form.topic.forum, request.user), + 'can_reply': True, }, context_instance=RequestContext(request)) diff -r b8f1dcc9fae4 -r f8f4514b806a gpp/templates/forums/display_post.html --- a/gpp/templates/forums/display_post.html Sat Oct 24 02:39:54 2009 +0000 +++ b/gpp/templates/forums/display_post.html Sun Oct 25 21:55:28 2009 +0000 @@ -13,7 +13,7 @@
{% if post.unread %}New{% endif %} Link - Posted on {{ post.creation_date|date:"M d, Y H:i" }} + Posted on {% forum_date post.creation_date user %} {% if can_moderate %}from IP: {{ post.user_ip }}{% endif %}
diff -r b8f1dcc9fae4 -r f8f4514b806a gpp/templates/forums/forum_index.html --- a/gpp/templates/forums/forum_index.html Sat Oct 24 02:39:54 2009 +0000 +++ b/gpp/templates/forums/forum_index.html Sun Oct 25 21:55:28 2009 +0000 @@ -40,7 +40,7 @@ {{ topic.user.username }} {{ topic.view_count }} - {% last_post_info topic.last_post MEDIA_URL %} + {% last_post_info topic.last_post %} {% empty %} @@ -62,5 +62,6 @@ {% if can_moderate %}

Moderate this forum

{% endif %} +{% current_forum_time user %}
{% endblock %} diff -r b8f1dcc9fae4 -r f8f4514b806a gpp/templates/forums/index.html --- a/gpp/templates/forums/index.html Sat Oct 24 02:39:54 2009 +0000 +++ b/gpp/templates/forums/index.html Sun Oct 25 21:55:28 2009 +0000 @@ -27,11 +27,12 @@

{{ forum.description }}

{{ forum.topic_count }} {{ forum.post_count }} - {% last_post_info forum.last_post MEDIA_URL %} + {% last_post_info forum.last_post %} {% endfor %} {% endfor %} +

{% current_forum_time user %}

{% endblock %} diff -r b8f1dcc9fae4 -r f8f4514b806a gpp/templates/forums/last_post_info.html --- a/gpp/templates/forums/last_post_info.html Sat Oct 24 02:39:54 2009 +0000 +++ b/gpp/templates/forums/last_post_info.html Sun Oct 25 21:55:28 2009 +0000 @@ -1,6 +1,7 @@ +{% load forum_tags %} {% if post %} Goto last post -{{ post.creation_date|date:"M d, Y H:i"}}
+{% forum_date post.creation_date user %}
{{ post.user.username }} {% else %} No posts diff -r b8f1dcc9fae4 -r f8f4514b806a gpp/templates/forums/mod_forum.html --- a/gpp/templates/forums/mod_forum.html Sat Oct 24 02:39:54 2009 +0000 +++ b/gpp/templates/forums/mod_forum.html Sun Oct 25 21:55:28 2009 +0000 @@ -35,7 +35,7 @@ {{ topic.reply_count }} {{ topic.user.username }} - {% last_post_info topic.last_post MEDIA_URL %} + {% last_post_info topic.last_post %} diff -r b8f1dcc9fae4 -r f8f4514b806a media/css/base.css --- a/media/css/base.css Sat Oct 24 02:39:54 2009 +0000 +++ b/media/css/base.css Sun Oct 25 21:55:28 2009 +0000 @@ -160,18 +160,18 @@ background:teal; } table.forum-index-table .forum-title { - width:60%; + width:57%; } table.forum-index-table .forum-topics { - width:10%; + width:9%; text-align:center; } table.forum-index-table .forum-posts { - width:10%; + width:9%; text-align:center; } table.forum-index-table .forum-last_post { - width:20%; + width:25%; text-align:center; } @@ -179,19 +179,19 @@ width:50%; } table.forum-index-table .forum-index_replies { - width:10%; + width:8%; text-align:center; } table.forum-index-table .forum-index_author { - width:10%; + width:8%; text-align:center; } table.forum-index-table .forum-index_views { - width:10%; + width:8%; text-align:center; } table.forum-index-table .forum-index_last_post { - width:20%; + width:26%; text-align:center; } table.forum-index-table .forum-index_select {