Ejemplo n.º 1
0
def prettify(request, value):
    """Generic function used in template rendering to prettify a given
    pythonic value into to a more human readable form. Depending on the
    datatype the function will call a specialised formatting function
    which takes care of localisation etc.

    The locale is determined from the given request.

    :request: Current request
    :value: Pythonic value
    :returns: Prettified (localized) value

    """
    if not request:
        request = get_current_request()
    try:
        #  FIXME: While testing get_current_request returns a dummy
        #  request object which does not have a ringo attribute.
        #  Currenlty do not now how to add this. (ti) <2017-05-09 09:05>
        locale_name = request.ringo.locale
    except AttributeError:
        locale_name = None

    # Special handling for Dummyrequest from testing
    if not isinstance(locale_name, basestring):
        locale_name = None

    if isinstance(value, datetime):
        return format_datetime(get_local_datetime(value,
                                                  get_timezone(request)),
                               locale_name=locale_name,
                               format="medium")
    elif isinstance(value, date):
        return format_date(value, locale_name=locale_name, format="medium")
    elif isinstance(value, timedelta):
        return from_timedelta(value)
    elif isinstance(value, list):
        values = []
        is_literal = False
        for v in value:
            r = prettify(request, v)
            if isinstance(r, literal):
                is_literal = True
            values.append(r)
        if is_literal:
            return literal(", ").join(values)
        else:
            return ", ".join(values)
    elif hasattr(value, "render"):
        try:
            return value.render()
        except TypeError:
            return value.render(request)
    elif value is None:
        return ""
    return value
Ejemplo n.º 2
0
def prettify(request, value):
    """Generic function used in template rendering to prettify a given
    pythonic value into to a more human readable form. Depending on the
    datatype the function will call a specialised formatting function
    which takes care of localisation etc.

    The locale is determined from the given request.

    :request: Current request
    :value: Pythonic value
    :returns: Prettified (localized) value

    """
    if not request:
        request = get_current_request()

    # Under some circumstances, get_current_request will return None as
    # the current request. This makes it impossible to retrieve the
    # current locale or getting the translate method.
    if not request:
        locale_name = 'en_EN'
        _ = lambda t: t
    else:
        locale_name = get_locale_name(request)
        _ = request.translate

    if isinstance(value, datetime):
        return format_datetime(get_local_datetime(value),
                               locale_name=locale_name, format="short")
    elif isinstance(value, date):
        return format_date(value,
                           locale_name=locale_name, format="short")
    elif isinstance(value, timedelta):
        return from_timedelta(value)
    elif isinstance(value, list):
        values = []
        is_literal = False
        for v in value:
            r = prettify(request, v)
            if isinstance(r, literal):
                is_literal = True
            values.append(r)
        if is_literal:
            return literal(", ").join(values)
        else:
            return ", ".join(values)
    elif hasattr(value, "render"):
        return value.render()
    elif value is None:
        return ""
    return _(value)
Ejemplo n.º 3
0
 def _from_python(self, value):
     from formbar.converters import from_timedelta
     td = datetime.timedelta(seconds=int(value))
     return from_timedelta(td)
Ejemplo n.º 4
0
 def _from_python(self, value):
     from formbar.converters import from_timedelta
     return from_timedelta(value)
Ejemplo n.º 5
0
 def _from_python(self, value):
     from formbar.converters import from_timedelta
     td = datetime.timedelta(seconds=int(value))
     return from_timedelta(td)
Ejemplo n.º 6
0
 def _from_python(self, value):
     from formbar.converters import from_timedelta
     return from_timedelta(value)