Browse Source

Extra template helper

Rafał Pitoń 11 years ago
parent
commit
b0b7700b2a
2 changed files with 66 additions and 13 deletions
  1. 6 2
      misago/templatetags/datetime.py
  2. 60 11
      misago/utils/datesformats.py

+ 6 - 2
misago/templatetags/datetime.py

@@ -1,5 +1,5 @@
 from django_jinja.library import Library
 from django_jinja.library import Library
-from misago.utils.datesformats import date, reldate, reltimesince, compact, relcompact
+from misago.utils.datesformats import date, reldate, reltimesince, compact, relcompact, timeamount
 
 
 register = Library()
 register = Library()
 
 
@@ -26,4 +26,8 @@ def compact_filter(val):
 
 
 @register.filter(name='relcompact')
 @register.filter(name='relcompact')
 def relcompact_filter(val):
 def relcompact_filter(val):
-    return relcompact(val)
+    return relcompact(val)
+
+@register.filter(name='timeamount')
+def timeamount_filter(val, unit='minutes'):
+    return timeamount(val, unit)

+ 60 - 11
misago/utils/datesformats.py

@@ -19,7 +19,7 @@ formats = {
 
 
 for key in formats:
 for key in formats:
     formats[key] = get_format(key).replace('P', 'g:i a')
     formats[key] = get_format(key).replace('P', 'g:i a')
-    
+
 def date(val, arg=""):
 def date(val, arg=""):
     if not val:
     if not val:
         return _("Never")
         return _("Never")
@@ -41,22 +41,22 @@ def reldate(val, arg=""):
     # Today check
     # Today check
     if format(local, 'Y-z') == format(local_now, 'Y-z'):
     if format(local, 'Y-z') == format(local_now, 'Y-z'):
         return _("Today, %(hour)s") % {'hour': time_format(local, formats['TIME_FORMAT'])}
         return _("Today, %(hour)s") % {'hour': time_format(local, formats['TIME_FORMAT'])}
-        
+
     # Yesteday check
     # Yesteday check
     yesterday = localtime(now - timedelta(days=1))
     yesterday = localtime(now - timedelta(days=1))
     if format(local, 'Y-z') == format(yesterday, 'Y-z'):
     if format(local, 'Y-z') == format(yesterday, 'Y-z'):
         return _("Yesterday, %(hour)s") % {'hour': time_format(local, formats['TIME_FORMAT'])}
         return _("Yesterday, %(hour)s") % {'hour': time_format(local, formats['TIME_FORMAT'])}
-        
+
     # Tomorrow Check
     # Tomorrow Check
     tomorrow = localtime(now + timedelta(days=1))
     tomorrow = localtime(now + timedelta(days=1))
     if format(local, 'Y-z') == format(tomorrow, 'Y-z'):
     if format(local, 'Y-z') == format(tomorrow, 'Y-z'):
         return _("Tomorrow, %(hour)s") % {'hour': time_format(local, formats['TIME_FORMAT'])}
         return _("Tomorrow, %(hour)s") % {'hour': time_format(local, formats['TIME_FORMAT'])}
-        
+
     # Day of Week check
     # Day of Week check
     if format(local, 'D') != format(local_now, 'D') and diff.days > -7 and diff.days < 7:
     if format(local, 'D') != format(local_now, 'D') and diff.days > -7 and diff.days < 7:
         return _("%(day)s, %(hour)s") % {'day': format(local, 'l'), 'hour': time_format(local, formats['TIME_FORMAT'])}
         return _("%(day)s, %(hour)s") % {'day': format(local, 'l'), 'hour': time_format(local, formats['TIME_FORMAT'])}
 
 
-    # Fallback to date      
+    # Fallback to date
     return date(val, arg)
     return date(val, arg)
 
 
 
 
@@ -75,7 +75,7 @@ def reltimesince(val, arg=""):
     if diff.seconds >= 0:
     if diff.seconds >= 0:
         if diff.seconds <= 5:
         if diff.seconds <= 5:
             return _("Just now")
             return _("Just now")
-                        
+
         if diff.seconds < 3540:
         if diff.seconds < 3540:
             minutes = int(math.ceil(diff.seconds / 60.0))
             minutes = int(math.ceil(diff.seconds / 60.0))
             return ungettext(
             return ungettext(
@@ -85,11 +85,11 @@ def reltimesince(val, arg=""):
 
 
         if diff.seconds < 3660:
         if diff.seconds < 3660:
             return _("Hour ago")
             return _("Hour ago")
-        
+
         if diff.seconds < 10800:
         if diff.seconds < 10800:
             hours = int(math.floor(diff.seconds / 3600.0))
             hours = int(math.floor(diff.seconds / 3600.0))
             minutes = (diff.seconds - (hours * 3600)) / 60
             minutes = (diff.seconds - (hours * 3600)) / 60
-            
+
             if minutes > 0:
             if minutes > 0:
                 return ungettext(
                 return ungettext(
                     "Hour and %(minutes)s ago",
                     "Hour and %(minutes)s ago",
@@ -98,7 +98,7 @@ def reltimesince(val, arg=""):
                         "%(minutes)s minute",
                         "%(minutes)s minute",
                         "%(minutes)s minutes",
                         "%(minutes)s minutes",
                     minutes) % {'minutes': minutes}}
                     minutes) % {'minutes': minutes}}
-                
+
             return ungettext(
             return ungettext(
                     "Hour ago",
                     "Hour ago",
                     "%(hours)s hours ago",
                     "%(hours)s hours ago",
@@ -114,7 +114,7 @@ def compact(val):
     now = datetime.now(utc if is_aware(val) else None)
     now = datetime.now(utc if is_aware(val) else None)
     local = localtime(val)
     local = localtime(val)
 
 
-    if now.year == local.year:        
+    if now.year == local.year:
         return format(localtime(val), _('j M'))
         return format(localtime(val), _('j M'))
     return format(localtime(val), _('j M y'))
     return format(localtime(val), _('j M y'))
 
 
@@ -140,4 +140,53 @@ def relcompact(val):
             hours = int(math.ceil(diff.seconds / 3600.0))
             hours = int(math.ceil(diff.seconds / 3600.0))
             return pgettext("number of hours", "%(hour)sh") % {'hour': hours}
             return pgettext("number of hours", "%(hour)sh") % {'hour': hours}
 
 
-    return compact(val)
+    return compact(val)
+
+
+def timeamount(val, unit='minutes'):
+    if unit == 'hours':
+        seconds = val * 3600
+    elif unit == 'minutes':
+        seconds = val * 60
+    elif unit == 'seconds':
+        seconds = val
+    else:
+        raise ValueError('Only hours, minutes and seconds are supported')
+
+    segments = []
+
+    if seconds >= 86400:
+        days = int(seconds / 86400)
+        seconds -= days * 86400
+        segments.append(ungettext(
+                "day",
+                "%(days)s days",
+            days) % {'days': days})
+
+    if seconds >= 3600:
+        hours = int(seconds / 3600)
+        seconds -= hours * 3600
+        segments.append(ungettext(
+                "hour",
+                "%(hours)s hours",
+            hours) % {'hours': hours})
+
+    if seconds >= 60:
+        minutes = int(seconds / 60)
+        seconds -= minutes * 60
+        segments.append(ungettext(
+                "minute",
+                "%(minutes)s minutes",
+            minutes) % {'minutes': minutes})
+
+    if seconds > 0:
+        segments.append(ungettext(
+                "second",
+                "%(seconds)s seconds",
+            seconds) % {'seconds': seconds})
+
+    if len(segments) > 1:
+        humane = ', '.join(segments[:-1])
+        return _("%(segments)s and %(last)s") % {'segments': humane, 'last': segments[-1]}
+    return segments[0]
+