Ticket #4976: humanize.diff

File humanize.diff, 3.4 KB (added by Simon G. <dev@…>, 17 years ago)
  • django/contrib/humanize/templatetags/humanize.py

     
    1212    """
    1313    try:
    1414        value = int(value)
    15     except ValueError:
     15    except (ValueError, TypeError):
    1616        return value
    1717    t = (_('th'), _('st'), _('nd'), _('rd'), _('th'), _('th'), _('th'), _('th'), _('th'), _('th'))
    1818    if value % 100 in (11, 12, 13): # special case
     
    3939    numbers over 1 million. For example, 1000000 becomes '1.0 million', 1200000
    4040    becomes '1.2 million' and '1200000000' becomes '1.2 billion'.
    4141    """
    42     value = int(value)
     42    try:
     43        value = int(value)
     44    except (ValueError, TypeError):
     45        return value
     46
    4347    if value < 1000000:
    4448        return value
    4549    if value < 1000000000:
     
    6165    """
    6266    try:
    6367        value = int(value)
    64     except ValueError:
     68    except (ValueError, TypeError):
    6569        return value
    6670    if not 0 < value < 10:
    6771        return value
  • tests/regressiontests/humanize/tests.py

     
    1919    def test_ordinal(self):
    2020        test_list = ('1','2','3','4','11','12',
    2121                     '13','101','102','103','111',
    22                      'something else')
     22                     'something else', None)
    2323        result_list = ('1st', '2nd', '3rd', '4th', '11th',
    2424                       '12th', '13th', '101st', '102nd', '103rd',
    25                        '111th', 'something else')
     25                       '111th', 'something else', 'None')
    2626
    2727        self.humanize_tester(test_list, result_list, 'ordinal')
    2828
    2929    def test_intcomma(self):
    3030        test_list = (100, 1000, 10123, 10311, 1000000, 1234567.25,
    31                      '100', '1000', '10123', '10311', '1000000', '1234567.1234567')
     31                     '100', '1000', '10123', '10311', '1000000', '1234567.1234567', None)
    3232        result_list = ('100', '1,000', '10,123', '10,311', '1,000,000', '1,234,567.25',
    33                        '100', '1,000', '10,123', '10,311', '1,000,000', '1,234,567.1234567')
     33                       '100', '1,000', '10,123', '10,311', '1,000,000', '1,234,567.1234567', 'None')
    3434
    3535        self.humanize_tester(test_list, result_list, 'intcomma')
    3636
    3737    def test_intword(self):
    3838        test_list = ('100', '1000000', '1200000', '1290000',
    39                      '1000000000','2000000000','6000000000000')
     39                     '1000000000','2000000000','6000000000000', None)
    4040        result_list = ('100', '1.0 million', '1.2 million', '1.3 million',
    41                        '1.0 billion', '2.0 billion', '6.0 trillion')
     41                       '1.0 billion', '2.0 billion', '6.0 trillion', 'None')
    4242
    4343        self.humanize_tester(test_list, result_list, 'intword')
    4444
    4545    def test_apnumber(self):
    4646        test_list = [str(x) for x in range(1, 11)]
     47        test_list.append(None)
    4748        result_list = (u'one', u'two', u'three', u'four', u'five', u'six',
    48                        u'seven', u'eight', u'nine', u'10')
     49                       u'seven', u'eight', u'nine', u'10', 'None')
    4950
    5051        self.humanize_tester(test_list, result_list, 'apnumber')
    5152
Back to Top