PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/django/contrib/humanize/templatetags/humanize.py

https://code.google.com/p/mango-py/
Python | 102 lines | 98 code | 2 blank | 2 comment | 4 complexity | 2930d2bbd5bb61303793b51ba683984b MD5 | raw file
Possible License(s): BSD-3-Clause
  1. from django.utils.translation import ungettext, ugettext as _
  2. from django.utils.encoding import force_unicode
  3. from django import template
  4. from django.template import defaultfilters
  5. from datetime import date
  6. import re
  7. register = template.Library()
  8. def ordinal(value):
  9. """
  10. Converts an integer to its ordinal as a string. 1 is '1st', 2 is '2nd',
  11. 3 is '3rd', etc. Works for any integer.
  12. """
  13. try:
  14. value = int(value)
  15. except (TypeError, ValueError):
  16. return value
  17. t = (_('th'), _('st'), _('nd'), _('rd'), _('th'), _('th'), _('th'), _('th'), _('th'), _('th'))
  18. if value % 100 in (11, 12, 13): # special case
  19. return u"%d%s" % (value, t[0])
  20. return u'%d%s' % (value, t[value % 10])
  21. ordinal.is_safe = True
  22. register.filter(ordinal)
  23. def intcomma(value):
  24. """
  25. Converts an integer to a string containing commas every three digits.
  26. For example, 3000 becomes '3,000' and 45000 becomes '45,000'.
  27. """
  28. orig = force_unicode(value)
  29. new = re.sub("^(-?\d+)(\d{3})", '\g<1>,\g<2>', orig)
  30. if orig == new:
  31. return new
  32. else:
  33. return intcomma(new)
  34. intcomma.is_safe = True
  35. register.filter(intcomma)
  36. def intword(value):
  37. """
  38. Converts a large integer to a friendly text representation. Works best for
  39. numbers over 1 million. For example, 1000000 becomes '1.0 million', 1200000
  40. becomes '1.2 million' and '1200000000' becomes '1.2 billion'.
  41. """
  42. try:
  43. value = int(value)
  44. except (TypeError, ValueError):
  45. return value
  46. if value < 1000000:
  47. return value
  48. if value < 1000000000:
  49. new_value = value / 1000000.0
  50. return ungettext('%(value).1f million', '%(value).1f million', new_value) % {'value': new_value}
  51. if value < 1000000000000:
  52. new_value = value / 1000000000.0
  53. return ungettext('%(value).1f billion', '%(value).1f billion', new_value) % {'value': new_value}
  54. if value < 1000000000000000:
  55. new_value = value / 1000000000000.0
  56. return ungettext('%(value).1f trillion', '%(value).1f trillion', new_value) % {'value': new_value}
  57. return value
  58. intword.is_safe = False
  59. register.filter(intword)
  60. def apnumber(value):
  61. """
  62. For numbers 1-9, returns the number spelled out. Otherwise, returns the
  63. number. This follows Associated Press style.
  64. """
  65. try:
  66. value = int(value)
  67. except (TypeError, ValueError):
  68. return value
  69. if not 0 < value < 10:
  70. return value
  71. return (_('one'), _('two'), _('three'), _('four'), _('five'), _('six'), _('seven'), _('eight'), _('nine'))[value-1]
  72. apnumber.is_safe = True
  73. register.filter(apnumber)
  74. def naturalday(value, arg=None):
  75. """
  76. For date values that are tomorrow, today or yesterday compared to
  77. present day returns representing string. Otherwise, returns a string
  78. formatted according to settings.DATE_FORMAT.
  79. """
  80. try:
  81. value = date(value.year, value.month, value.day)
  82. except AttributeError:
  83. # Passed value wasn't a date object
  84. return value
  85. except ValueError:
  86. # Date arguments out of range
  87. return value
  88. delta = value - date.today()
  89. if delta.days == 0:
  90. return _(u'today')
  91. elif delta.days == 1:
  92. return _(u'tomorrow')
  93. elif delta.days == -1:
  94. return _(u'yesterday')
  95. return defaultfilters.date(value, arg)
  96. register.filter(naturalday)