Add age_format template tag

We are still yet to internationalize that, but for now, it works fine in
English
This commit is contained in:
Gergely Polonkai 2014-12-18 16:14:31 +01:00 committed by Gergely Polonkai
parent b114fbfc07
commit 9cba0a8090
3 changed files with 124 additions and 0 deletions

View File

View File

@ -0,0 +1,87 @@
from django import template
import math
register = template.Library()
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
@register.filter
def age_format(value, arg = None):
if not is_number(value):
return value
ret = u""
years = math.floor(value / 31536000)
remainder = value % 31536000
if years > 0:
ret += u"%d year%s" % (years, "" if years == 1 else "s")
if arg != None:
return ret
months = math.floor(remainder / 2592000)
remainder = remainder % 2592000
if months > 0:
if (ret != ""):
ret += " "
ret += u"%d month%s" % (months, "" if months == 1 else "s")
if arg != None:
return ret
days = math.floor(remainder / 86400)
remainder = remainder % 86400
if arg != None and days == 0:
days = 1
if days > 0:
if (ret != ""):
ret += " "
ret += u"%d day%s" % (days, "" if days == 1 else "s")
if arg != None:
return ret
if arg != None:
raise RuntimeError("Value is strange, we should never arrive here!")
hours = math.floor(remainder / 3600)
remainder = remainder % 3600
if hours > 0:
if (ret != ""):
ret += " "
ret += u"%d hour%s" % (hours, "" if hours == 1 else "s")
minutes = math.floor(remainder / 60)
if minutes > 0:
if (ret != ""):
ret += " "
ret += u"%d minute%s" % (minutes, "" if minutes == 1 else "s")
seconds = round(remainder % 60)
if seconds > 0:
if (ret != ""):
ret += " "
ret += u"%d second%s" % (seconds, "" if seconds == 1 else "s")
if ret == "":
ret = "a few moments"
return ret

View File

@ -3,6 +3,7 @@ from django.utils import timezone
import datetime import datetime
from .templatetags import booking_tags
from .models import Duck from .models import Duck
class FrontTest(TestCase): class FrontTest(TestCase):
@ -25,3 +26,39 @@ class DuckAgeTest(TestCase):
def test_duck_is_from_the_future(self): def test_duck_is_from_the_future(self):
future_duck = Duck(donated_at = timezone.now() + datetime.timedelta(days = 2)) future_duck = Duck(donated_at = timezone.now() + datetime.timedelta(days = 2))
self.assertEqual(future_duck.age(), -1) self.assertEqual(future_duck.age(), -1)
def test_duck_age_formatter(self):
self.assertEqual(booking_tags.age_format("aoeu"), "aoeu")
self.assertEqual(booking_tags.age_format(0), "a few moments")
self.assertEqual(booking_tags.age_format(1), "1 second")
self.assertEqual(booking_tags.age_format(2), "2 seconds")
self.assertEqual(booking_tags.age_format(60), "1 minute")
self.assertEqual(booking_tags.age_format(61), "1 minute 1 second")
self.assertEqual(booking_tags.age_format(62), "1 minute 2 seconds")
self.assertEqual(booking_tags.age_format(120), "2 minutes")
self.assertEqual(booking_tags.age_format(3600), "1 hour")
self.assertEqual(booking_tags.age_format(3601), "1 hour 1 second")
self.assertEqual(booking_tags.age_format(3660), "1 hour 1 minute")
self.assertEqual(booking_tags.age_format(3720), "1 hour 2 minutes")
self.assertEqual(booking_tags.age_format(7200), "2 hours")
self.assertEqual(booking_tags.age_format(86400), "1 day")
self.assertEqual(booking_tags.age_format(86401), "1 day 1 second")
self.assertEqual(booking_tags.age_format(86460), "1 day 1 minute")
self.assertEqual(booking_tags.age_format(90000), "1 day 1 hour")
self.assertEqual(booking_tags.age_format(93600), "1 day 2 hours")
self.assertEqual(booking_tags.age_format(172800), "2 days")
self.assertEqual(booking_tags.age_format(2592000), "1 month")
self.assertEqual(booking_tags.age_format(2592001), "1 month 1 second")
self.assertEqual(booking_tags.age_format(2592060), "1 month 1 minute")
self.assertEqual(booking_tags.age_format(2595600), "1 month 1 hour")
self.assertEqual(booking_tags.age_format(2678400), "1 month 1 day")
self.assertEqual(booking_tags.age_format(2764800), "1 month 2 days")
self.assertEqual(booking_tags.age_format(5184000), "2 months")
self.assertEqual(booking_tags.age_format(31536000), "1 year")
self.assertEqual(booking_tags.age_format(31536001), "1 year 1 second")
self.assertEqual(booking_tags.age_format(31536060), "1 year 1 minute")
self.assertEqual(booking_tags.age_format(31539600), "1 year 1 hour")
self.assertEqual(booking_tags.age_format(31622400), "1 year 1 day")
self.assertEqual(booking_tags.age_format(34128000), "1 year 1 month")
self.assertEqual(booking_tags.age_format(36720000), "1 year 2 months")
self.assertEqual(booking_tags.age_format(63072000), "2 years")