Created
July 27, 2016 09:30
-
-
Save Keda87/77422882896193251b2ef2a252e28e85 to your computer and use it in GitHub Desktop.
Django Template Filter for Humanize Day
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2016 Adiyat Mubarak <adiyatmubarak@gmail.com> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. | |
""" | |
from django import template | |
register = template.Library() | |
@register.filter | |
def statistics(value): | |
"""Convert given day integer to a string to be more human readable | |
representation. | |
Example: | |
* 1 -> 1 Day | |
* 30 -> 1 Month | |
* 90 -> 3 Months | |
* 366 -> 1 Year 1 Day | |
""" | |
if value == 0: | |
word = '' | |
elif value <= 1: | |
word = '%d Day' % value | |
elif 1 < value < 30: | |
word = '%d Days' % value | |
elif value > 1 and value == 30: | |
word = '%d Month' % (value / 30) | |
elif value > 1 and 30 < value <= 365: | |
month = value / 30 | |
days = statistics(value % 30) | |
if month > 1: | |
month_label = 'Months' | |
word = '%s %s %s' % (month, month_label, days) | |
else: | |
month_label = 'Month' | |
word = '%s %s %s' % (month, month_label, days) | |
else: | |
year = value / 365 | |
if year > 1: | |
year_label = 'Years' | |
else: | |
year_label = 'Year' | |
word = '%d %s %s' % (year, year_label, statistics(value % 365)) | |
return str(word).strip() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
from rent_statistics import statistics | |
class CustomFilterTest(unittest.TestCase): | |
def test_day_singular(self): | |
result = statistics(1) | |
self.assertEqual('1 Day', result) | |
def test_day_plural(self): | |
result = statistics(20) | |
self.assertEqual('20 Days', result) | |
def test_month_singular(self): | |
result = statistics(30) | |
self.assertEqual('1 Month', result) | |
def test_month_singular_day_singular(self): | |
result = statistics(31) | |
self.assertEqual('1 Month 1 Day', result) | |
def test_month_plural_day_singular(self): | |
result = statistics(61) | |
self.assertEqual('2 Months 1 Day', result) | |
def test_month_plural_day_plural(self): | |
result = statistics(98) | |
self.assertEqual('3 Months 8 Days', result) | |
def test_day_in_a_month(self): | |
result = statistics(365) | |
self.assertEqual('12 Months 5 Days', result) | |
def test_year_singular(self): | |
result = statistics(366) | |
self.assertEqual('1 Year 1 Day', result) | |
def test_everything_plural(self): | |
result = statistics(366 * 2 + 60) | |
self.assertEqual('2 Years 2 Months 2 Days', result) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment