changeset 120:f8f4514b806a

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.
author Brian Neal <bgneal@gmail.com>
date Sun, 25 Oct 2009 21:55:28 +0000 (2009-10-25)
parents b8f1dcc9fae4
children 98d658afd4bf
files gpp/bio/forms.py gpp/bio/models.py gpp/forums/templatetags/forum_tags.py gpp/forums/views.py gpp/templates/forums/display_post.html gpp/templates/forums/forum_index.html gpp/templates/forums/index.html gpp/templates/forums/last_post_info.html gpp/templates/forums/mod_forum.html media/css/base.css
diffstat 10 files changed, 76 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- 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 = {
--- 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):
--- 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)
--- 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))
 
--- 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 @@
       <div class="forum-post-info quiet">
       {% if post.unread %}<img src="{{ MEDIA_URL }}icons/new.png" alt="New" title="New" />{% endif %}
       <a href="{{ post.get_absolute_url }}"><img src="{{ MEDIA_URL }}icons/link.png" alt="Link" title="Link to this post" /></a>
-         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 %}
       </div>
       <div class="forum-post-body">
--- 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 @@
          <td class="forum-index_author"><a href="{% url bio-view_profile username=topic.user.username %}" title="View profile for {{ topic.user.username }}">{{ topic.user.username }}</a></td>
          <td class="forum-index_views">{{ topic.view_count }}</td>
          <td class="forum-index_last_post">
-            {% last_post_info topic.last_post MEDIA_URL %}
+            {% last_post_info topic.last_post %}
          </td>
       </tr>
    {% empty %}
@@ -62,5 +62,6 @@
 {% if can_moderate %}
 <p><a href="{% url forums-mod_forum slug=forum.slug %}">Moderate this forum</a></p>
 {% endif %}
+{% current_forum_time user %}
 </div>
 {% endblock %}
--- 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 @@
                   <p>{{ forum.description }}</p></td>
                <td class="forum-topics">{{ forum.topic_count }}</td>
                <td class="forum-posts">{{ forum.post_count }}</td>
-               <td class="forum-last_post">{% last_post_info forum.last_post MEDIA_URL %}</td>
+               <td class="forum-last_post">{% last_post_info forum.last_post %}</td>
             </tr>
          {% endfor %}
       </tbody>
    </table>
 {% endfor %}
+<p>{% current_forum_time user %}</p>
 </div>
 {% endblock %}
--- 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 %}
 <a href="{{ post.get_absolute_url }}" title="Goto last post"><img src="{{ MEDIA_URL }}icons/note_go.png" alt="Goto last post" />
-<a href="{{ post.get_absolute_url }}" title="Goto last post">{{ post.creation_date|date:"M d, Y H:i"}}</a><br />
+<a href="{{ post.get_absolute_url }}" title="Goto last post">{% forum_date post.creation_date user %}</a><br />
 <a href="{% url bio-view_profile username=post.user.username %}" title="View profile for {{ post.user.username }}">{{ post.user.username }}</a>
 {% else %}
 <i>No posts</i>
--- 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 @@
          <td class="forum-index_replies">{{ topic.reply_count }}</td>
          <td class="forum-index_author"><a href="{% url bio-view_profile username=topic.user.username %}" title="View profile for {{ topic.user.username }}">{{ topic.user.username }}</a></td>
          <td class="forum-index_last_post">
-            {% last_post_info topic.last_post MEDIA_URL %}
+            {% last_post_info topic.last_post %}
          </td>
          <td class="forum-index_select"><input type="checkbox" name="topic_ids" value="{{ topic.id }}" class="forums-topic_check" /></td>
       </tr>
--- 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 {