Skip to content

Commit

Permalink
[#2707] More Resource item listing improvements i18n etc
Browse files Browse the repository at this point in the history
  • Loading branch information
tobes committed Aug 9, 2012
1 parent 4b9e6d9 commit f49437c
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 4 deletions.
122 changes: 122 additions & 0 deletions ckan/lib/formatters.py
@@ -0,0 +1,122 @@
import datetime

from pylons.i18n import _, ungettext
from babel import numbers

import ckan.lib.i18n as i18n


##################################################
# #
# Month translations #
# #
##################################################

def month_jan():
return _('January')


def month_feb():
return _('February')


def month_mar():
return _('March')


def month_apr():
return _('April')


def month_may():
return _('May')


def month_june():
return _('June')


def month_july():
return _('July')


def month_aug():
return _('August')


def month_sept():
return _('September')


def month_oct():
return _('October')


def month_nov():
return _('November')


def month_dec():
return _('December')


# MONTH_FUNCTIONS provides an easy way to get a localised month via
# MONTH_FUNCTIONS[month]() where months are zero based ie jan = 0, dec = 11
MONTH_FUNCTIONS = [month_jan, month_feb, month_mar, month_apr,
month_may, month_june, month_july, month_aug,
month_sept, month_oct, month_nov, month_dec]


def localised_nice_date(datetime_):
''' Returns a friendly localised unicode representation of a datetime. '''
now = datetime.datetime.now()
date_diff = now - datetime_
days = date_diff.days
if days < 1 and now > datetime_:
# less than one day
seconds = date_diff.seconds
if seconds < 3600:
# less than one hour
if seconds < 60:
return _('Just now')
else:
return ungettext('{mins} minute ago}', '{mins} minutes ago',
seconds / 60).format(mins=seconds / 60)
else:
return ungettext('{hours} hour ago}', '{hours} hours ago',
seconds / 3600).format(hours=seconds / 3600)
# more than one day
if days < 31:
return ungettext('{days} day ago}', '{days} days ago',
days).format(days=days)
# actual date
month = datetime_.month
day = datetime_.day
year = datetime_.year
month_name = MONTH_FUNCTIONS[month]()
return _('{month} {day}, {year}').format(month=month_name, day=day,
year=year)


def localised_number(number):
''' Returns a localised unicode representation of number '''
return numbers.format_number(number, locale=i18n.get_lang())


def localised_filesize(number):
''' Returns a localised unicode representation of a number in bytes, MiB
etc '''
def rnd(number, divisor):
# round to 1 decimal place
return localised_number(float(number * 10 / divisor) / 10)

if number < 1024:
return _('{bytes} bytes').format(bytes=number)
elif number < 1024 ** 2:
return _('{kibibytes} KiB').format(kibibytes=rnd(number, 1024))
elif number < 1024 ** 3:
return _('{mebibytes} KiB').format(mebibytes=rnd(number, 1024 ** 2))
elif number < 1024 ** 4:
return _('{gibibytes} KiB').format(gibibytes=rnd(number, 1024 ** 3))
else:
return _('{tebibytes} KiB').format(tebibytes=rnd(number, 1024 ** 4))
23 changes: 19 additions & 4 deletions ckan/lib/helpers.py
Expand Up @@ -39,6 +39,7 @@
import html_resources
from lib.maintain import deprecated
import ckan.model as model
import ckan.lib.formatters as formatters

get_available_locales = i18n.get_available_locales
get_locales_dict = i18n.get_locales_dict
Expand Down Expand Up @@ -1213,20 +1214,34 @@ def render_markdown(data):
return ''
return literal(ckan.misc.MarkdownFormat().to_html(data))


def format_resource_items(items):
''' Take a resource item list and format nicely with blacklisting etc. '''
blacklist = ['name', 'description', 'url', 'tracking_summary']
output = []
reg_ex = '^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}$'
# regular expressions for detecting types in strings
reg_ex_datetime = '^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{6})?$'
reg_ex_number = '^-?\d{1,}\.?\d*$' # int/float
for key, value in items:
if not value or key in blacklist:
continue
if isinstance(value, basestring) and re.search(reg_ex, value):
value = render_datetime(date_str_to_datetime(value),
with_hours=True)
# size is treated specially as we want to show in MiB etc
if key == 'size':
value = formatters.localised_filesize(int(value))
elif isinstance(value, basestring):
# check if strings are actually datetime/number etc
if re.search(reg_ex_datetime, value):
datetime_ = date_str_to_datetime(value)
value = formatters.localised_nice_date(datetime_)
elif re.search(reg_ex_number, value):
value = formatters.localised_number(float(value))
elif isinstance(value, int) or isinstance(value, float):
value = formatters.localised_number(value)
key = key.replace('_', ' ')
output.append((key, value))
return sorted(output, key=lambda x:x[0])


# these are the functions that will end up in `h` template helpers
# if config option restrict_template_vars is true
__allowed_functions__ = [
Expand Down

0 comments on commit f49437c

Please sign in to comment.