Ticket #2466: filesize.diff

File filesize.diff, 1.2 KB (added by parlar@…, 18 years ago)

[patch] Patch for ticket

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

     
    6262        return value
    6363    return ('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine')[value-1]
    6464register.filter(apnumber)
     65
     66
     67def filesize(value):
     68   """
     69   Used to convert filesizes returned by get_XXX_size on FileFields
     70   For sizes under a megabyte, it returns the number of kilobytes, with
     71   two numbers after the decimal, and 'KB' on the end.
     72   Over a megabyte and under a gigabyte returns the number of megabytes,
     73   with two numbers after the decimal, and 'MB' on the end.
     74   Over a gigabyte returns the number of gigabytes with two numbers
     75   after the decimal, and a 'GB' on the end
     76   """
     77   try:
     78        value = int(value)
     79   except ValueError:
     80       return value
     81
     82   if value < 0:
     83       return value
     84
     85   if 0 <= value < 1000000:
     86       return "%.2fKB" % (value/1000.0)
     87   elif 1000000 <= value < 1000000000:
     88       return "%.2fMB" % (value/1000000.0)
     89   else:
     90       return "%.2fGB" % (value/1000000000.0)
     91
     92register.filter(filesize)
Back to Top