Tim Savage avatar Tim Savage committed 284b660 Draft

Added humanize support

Comments (0)

Files changed (5)

django_extras/db/models/fields/__init__.py

+#from django.conf import settings
 from django.core import exceptions
 from django.core.validators import MinValueValidator, MaxValueValidator
 from django.db import models
     default_validators = [
         MinValueValidator(0),
         MaxValueValidator(100),
-        ]
+    ]
 
 
 class LatitudeField(models.FloatField):
         kwargs.setdefault('validators', [
             validators.MinValueValidator(-90.0),
             validators.MaxValueValidator(90.0),
-            ])
+        ])
         super(LatitudeField, self).__init__(*args, **kwargs)
 
 
         kwargs.setdefault('validators', [
             validators.MinValueValidator(-180.0),
             validators.MaxValueValidator(180.0),
-            ])
+        ])
         super(LongitudeField, self).__init__(*args, **kwargs)
 
+
 # Register special admin widgets if admin is in use.
 #if 'django.contrib.admin' in settings.INSTALLED_APPS:
 #    from django.contrib.admin import options
-#    options.FORMFIELD_FOR_DBFIELD_DEFAULTS[ColorField] = { 'widget':  }
+#    from django_extras.forms.widgets import JQueryColorPicker
+#    options.FORMFIELD_FOR_DBFIELD_DEFAULTS[ColorField] = { 'widget': JQueryColorPicker }
 
 # Register fields with south.
 try:

django_extras/templatetags/humanize_extras.py

+from django import template
+from django_extras.utils import humanize
+
+
+register = template.Library()
+
+
+@register.filter(is_safe=True)
+def describe_seconds(value):
+    """
+    Convert a seconds value into a human readable (ie week, day, hour) value.
+    :param value: integer value of the number of seconds.
+    :return: a string with the humanized value.
+    """
+    return humanize.describe_seconds(value)

django_extras/tests/__init__.py

 from django_extras.tests.http.responses import *
 from django_extras.tests.middleware.timing import *
 from django_extras.tests.utils.cache import *
+from django_extras.tests.utils.humanize import *

django_extras/tests/utils/humanize.py

+from django import test
+from django_extras.utils import humanize
+
+
+class HumanizeDescribeSecondsTestCase(test.TestCase):
+    def test_seconds(self):
+        self.assertEqual('12s', humanize.describe_seconds(12))
+
+    def test_minutes(self):
+        self.assertEqual('12m', humanize.describe_seconds(720))
+
+    def test_hours(self):
+        self.assertEqual('12h', humanize.describe_seconds(43200))
+
+    def test_days(self):
+        self.assertEqual('5d', humanize.describe_seconds(432000))
+
+    def test_weeks(self):
+        self.assertEqual('12w', humanize.describe_seconds(7257600))
+
+    def test_all_one(self):
+        self.assertEqual('1w 1d 1h 1m 1s', humanize.describe_seconds(694861))
+
+    def test_mix_it_up(self):
+        self.assertEqual('2w 3d 14h 42s', humanize.describe_seconds(1519242))

django_extras/utils/humanize.py

+TIME_MAP = (
+    ('w', 604800),
+    ('d', 86400),
+    ('h', 3600),
+    ('m', 60),
+    ('s', 1)
+    )
+
+def describe_seconds(value):
+    """
+    Convert a seconds value into a human readable (ie week, day, hour) value.
+    :param value: integer value of the number of seconds.
+    :return: a string with the humanized value.
+    """
+    value_vector = []
+    for unit, factor in iter(TIME_MAP):
+        component, value = value / factor, value % factor
+        if component:
+            value_vector.append('%s%s' % (component, unit))
+    return ' '.join(value_vector)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.