Пример #1
0
def format_resource_items(items):
    ''' Take a resource item list and format nicely with blacklisting etc. '''
    blacklist = ['name', 'description', 'url', 'tracking_summary']
    output = []
    # 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_int = '^-?\d{1,}$'
    reg_ex_float = '^-?\d{1,}\.\d{1,}$'
    for key, value in items:
        if not value or key in blacklist:
            continue
        # 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_float, value):
                value = formatters.localised_number(float(value))
            elif re.search(reg_ex_int, value):
                value = formatters.localised_number(int(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])
Пример #2
0
def format_resource_items(items):
    ''' Take a resource item list and format nicely with blacklisting etc. '''
    blacklist = ['name', 'description', 'url', 'tracking_summary', 'format', 'position', 'is_local_resource',
                 'datastore_active', 'on_same_domain', 'mimetype', 'state', 'url_type', 'has_views']
    output = []
    # 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_int = '^-?\d{1,}$'
    reg_ex_float = '^-?\d{1,}\.\d{1,}$'
    for key, value in items:
        if not value or key in blacklist:
            continue
        # size is treated specially as we want to show in MiB etc
        if key == 'size':
            try:
                value = formatters.localised_filesize(int(value))
            except ValueError:
                # Sometimes values that can't be converted to ints can sneak
                # into the db. In this case, just leave them as they are.
                pass
        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_float, value):
                value = formatters.localised_number(float(value))
            elif re.search(reg_ex_int, value):
                value = formatters.localised_number(int(value))
        elif ((isinstance(value, int) or isinstance(value, float))
                and value not in (True, False)):
            value = formatters.localised_number(value)
        key = key.replace('_', ' ')
        output.append((key, value))
    return sorted(output, key=lambda x: x[0])
Пример #3
0
def format_resource_items(items):
    ''' Take a resource item list and format nicely with blacklisting etc. '''
    blacklist = ['name', 'description', 'url', 'tracking_summary']
    output = []
    # 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_int = '^-?\d{1,}$'
    reg_ex_float = '^-?\d{1,}\.\d{1,}$'
    for key, value in items:
        if not value or key in blacklist:
            continue
        # 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_float, value):
                value = formatters.localised_number(float(value))
            elif re.search(reg_ex_int, value):
                value = formatters.localised_number(int(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])
Пример #4
0
def format_resource_items(items):
    ''' Take a resource item list and format nicely with blacklisting etc. '''
    blacklist = [
        'name', 'description', 'url', 'tracking_summary', 'format', 'position',
        'is_local_resource'
    ]
    output = []
    translations = [
        _('created'),
        _('format'),
        _('has views'),
        _('last modified'),
        _('package id'),
        _('revision id'),
        _('state'),
        _('url type'),
        _('active'),
        _('upload'),
        _('position')
    ]
    # 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_int = '^-?\d{1,}$'
    reg_ex_float = '^-?\d{1,}\.\d{1,}$'
    for key, value in items:
        if not value or key in blacklist:
            continue
        # size is treated specially as we want to show in MiB etc
        if key == 'size':
            try:
                value = formatters.localised_filesize(int(value))
            except ValueError:
                # Sometimes values that can't be converted to ints can sneak
                # into the db. In this case, just leave them as they are.
                pass
        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_float, value):
                value = formatters.localised_number(float(value))
            elif re.search(reg_ex_int, value):
                value = formatters.localised_number(int(value))
        elif isinstance(value, int) or isinstance(value, float):
            value = formatters.localised_number(value)
        key = key.replace('_', ' ')
        key = _(key)

        output.append((key, value))
    return sorted(output, key=lambda x: x[0])
Пример #5
0
def _SI_number_span_close(number):
    ''' outputs a span with the number in SI unit eg 14700 -> 14.7k '''
    number = int(number)
    if number < 1000:
        output = literal('<span>')
    else:
        output = literal('<span title="' + formatters.localised_number(number) + '">')
    return output + formatters.localised_SI_number(number) + literal('</span>')
Пример #6
0
def _SI_number_span_close(number):
    ''' outputs a span with the number in SI unit eg 14700 -> 14.7k '''
    number = int(number)
    if number < 1000:
        output = literal('<span>')
    else:
        output = literal('<span title="' + formatters.localised_number(number) + '">')
    return output + formatters.localised_SI_number(number) + literal('</span>')
Пример #7
0
def SI_number_span(number):
    """ outputs a span with the number in SI unit eg 14700 -> 14.7k """
    number = int(number)
    if number < 1000:
        output = literal("<span>")
    else:
        output = literal('<span title="' + formatters.localised_number(number) + '">')
    return output + formatters.localised_SI_number(number) + literal("</span>")
Пример #8
0
def format_resource_items(items):

    ''' Take a resource item list and format nicely with blacklisting etc. '''
    blacklist = ['name', 'description', 'url', 'tracking_summary', 'format', 'position', 'is_local_resource',  'cache_url', 'can_be_previewed', 'hash',
                 'datastore_active', 'on_same_domain', 'mimetype', 'state', 'url_type', 'has_views', 'cache_last_updated', 'mimetype_inner', 'resource_type',
                 '_authentication_token', 'ckan_url', 'datastore_contains_all_records_of_source_file', 'ignore_hash', 'original_url', 'resource_id',
                 'set_url_type', 'task_created', 'package_id', 'id', 'revision_id']
    output = []
    additioanl_fields = ['reference_number', 'demarcation', 'Language', 'spatial_coverage', 'geodetic']
    # 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_int = '^-?\d{1,}$'
    reg_ex_float = '^-?\d{1,}\.\d{1,}$'
    for key, value in items:
        if key in blacklist:
            continue

        # size is treated specially as we want to show in MiB etc
        if key == 'size':
            try:
                # in case of a url
                if value is None:
                    continue
                value = formatters.localised_filesize(int(value))

            except ValueError, e:

                # Sometimes values that can't be converted to ints can sneak
                # into the db. In this case, just leave them as they are.
                pass
        elif isinstance(value, basestring):
            try:
                # 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_float, value):
                    value = formatters.localised_number(float(value))
                elif re.search(reg_ex_int, value) and key not in additioanl_fields:
                    value = formatters.localised_number(int(value))
            except ValueError, e:
                log.info(e.message)
                pass
Пример #9
0
                value = formatters.localised_filesize(int(value))

            except ValueError, e:

                # Sometimes values that can't be converted to ints can sneak
                # into the db. In this case, just leave them as they are.
                pass
        elif isinstance(value, basestring):
            try:
                # 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_float, value):
                    value = formatters.localised_number(float(value))
                elif re.search(reg_ex_int, value) and key not in additioanl_fields:
                    value = formatters.localised_number(int(value))
            except ValueError, e:
                log.info(e.message)
                pass
        elif ((isinstance(value, int) or isinstance(value, float))
                and value not in (True, False)):
            try:
               value = formatters.localised_number(value)
            except ValueError, e:
               log.info(e.message)
               pass
        key = key.replace('_', ' ')
        output.append((key, value))
    return sorted(output, key=lambda x: x[0])