Exemplo n.º 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])
Exemplo n.º 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])
Exemplo n.º 3
0
Arquivo: helpers.py Projeto: HHS/ckan
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])
Exemplo n.º 4
0
def get_localized_date(date_string):
    """
    Take a date string formatted as DD.MM.YYYY and return a localized date,
    e.g. '24. Juni 2020'.
    """
    try:
        date_format = tk.config.get('ckanext.switzerland.date_picker_format',
                                    '%d.%m.%Y')
        dt = datetime.datetime.strptime(date_string, date_format)
        return localised_nice_date(dt, show_date=True, with_hours=False)
    except ValueError:
        return ''
Exemplo n.º 5
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])
def get_localized_date(date_string):
    """
    Take a date string and return a localized date, e.g. '24. Juni 2020'.
    `parse` should be able to handle various date formats, including
    DD.MM.YYYY, DD.MM.YYY (necessary for collections with pre-1000 temporals)
    and DD.MM.YY (in this case, it assumes the century isn't specified and
    the year is between 50 years ago and 49 years in the future. This means
    that '01.01.60' => 01.01.2060, and '01.01.90' => 01.01.1990).
    """
    try:
        dt = parse(date_string, dayfirst=True)
        return localised_nice_date(dt, show_date=True, with_hours=False)
    except (TypeError, ParserError):
        return ''
Exemplo n.º 7
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
Exemplo n.º 8
0
 def test_relative_dates(self, dt, expected):
     assert f.localised_nice_date(dt) == expected
Exemplo n.º 9
0
 def test_params(self, dt, date, hours, seconds, expected):
     assert f.localised_nice_date(dt, date, hours, seconds) == expected
Exemplo n.º 10
0
 def test_with_dates(self, dt, hours, seconds, fmt, expected):
     assert f.localised_nice_date(dt, True, hours, seconds, fmt) == expected