Exemplo n.º 1
0
def _to_html(val, key=None, level=0, timeago=False):
    """
    Recursively convert a value to its HTML representation using <dl>s for
    dictionaries and <ul>s for lists.
    """
    recurse = lambda k, v: _to_html(v, key=k, level=level + 1, timeago=timeago)

    def _key_format(k, v):
        if not _is_list_like(v):
            return _format_slug_string_for_display(k)
        else:
            return ""

    if isinstance(val, dict):
        ret = format_html(
            "<dl {}>{}</dl>",
            mark_safe("class='well'")
            if level == 0 else '',  # nosec: no user input
            format_html_join("", "<dt>{}</dt><dd>{}</dd>",
                             [(_key_format(k, v), recurse(k, v))
                              for k, v in val.items()]))

    elif _is_list_like(val):
        ret = format_html(
            "<dl>{}</dl>",
            format_html_join("", "<dt>{}</dt><dd>{}</dd>",
                             [(key, recurse(None, v)) for v in val]))

    elif isinstance(val, datetime.date):
        if isinstance(val, datetime.datetime):
            fmt = USER_DATETIME_FORMAT
        else:
            fmt = USER_DATE_FORMAT

        iso = val.isoformat()
        ret = format_html(
            "<time{timeago} title='{title}' datetime='{iso}'>{display}</time>".
            format(
                timeago=mark_safe(" class='timeago'")
                if timeago else "",  # nosec: no user input
                title=iso,
                iso=iso,
                display=safe_strftime(val, fmt)))
    else:
        if val is None:
            val = '---'

        ret = escape(val)

    return ret
def _to_html(val, key=None, level=0, timeago=False):
    """
    Recursively convert a value to its HTML representation using <dl>s for
    dictionaries and <ul>s for lists.
    """
    recurse = lambda k, v: _to_html(v, key=k, level=level + 1, timeago=timeago)

    def _key_format(k, v):
        if not _is_list_like(v):
            return _format_slug_string_for_display(k)
        else:
            return ""

    if isinstance(val, dict):
        ret = "".join(["<dl %s>" % ("class='well'" if level == 0 else '')] + [
            "<dt>%s</dt><dd>%s</dd>" % (_key_format(k, v), recurse(k, v))
            for k, v in val.items()
        ] + ["</dl>"])

    elif _is_list_like(val):
        ret = "".join(
            ["<dl>"] +
            ["<dt>%s</dt><dd>%s</dd>" % (key, recurse(None, v))
             for v in val] + ["</dl>"])

    elif isinstance(val, datetime.date):
        if isinstance(val, datetime.datetime):
            fmt = USER_DATETIME_FORMAT
        else:
            fmt = USER_DATE_FORMAT

        iso = val.isoformat()
        ret = mark_safe("<time %s title='%s' datetime='%s'>%s</time>" %
                        ("class='timeago'" if timeago else "", iso, iso,
                         safe_strftime(val, fmt)))
    else:
        if val is None:
            val = '---'

        ret = escape(val)

    return mark_safe(ret)
Exemplo n.º 3
0
def _to_html(val, key=None, level=0, timeago=False):
    """
    Recursively convert a value to its HTML representation using <dl>s for
    dictionaries and <ul>s for lists.
    """
    recurse = lambda k, v: _to_html(v, key=k, level=level + 1, timeago=timeago)

    def _key_format(k, v):
        if not _is_list_like(v):
            return _format_slug_string_for_display(k)
        else:
            return ""

    if isinstance(val, types.DictionaryType):
        ret = "".join(
            ["<dl %s>" % ("class='well'" if level == 0 else '')] + 
            ["<dt>%s</dt><dd>%s</dd>" % (_key_format(k, v), recurse(k, v))
             for k, v in val.items()] +
            ["</dl>"])

    elif _is_list_like(val):
        ret = "".join(
            ["<dl>"] +
            ["<dt>%s</dt><dd>%s</dd>" % (key, recurse(None, v)) for v in val] +
            ["</dl>"])

    elif isinstance(val, datetime.date):
        if isinstance(val, datetime.datetime):
            fmt = USER_DATETIME_FORMAT
        else:
            fmt = USER_DATE_FORMAT

        iso = val.isoformat()
        ret = mark_safe("<time %s title='%s' datetime='%s'>%s</time>" % (
            "class='timeago'" if timeago else "", iso, iso, safe_strftime(val, fmt)))
    else:
        if val is None or val == '':
            val = '---'

        ret = escape(val)

    return mark_safe(ret)
Exemplo n.º 4
0
 def _fmt_timestamp(timestamp):
     if timestamp is not None and timestamp >= 0:
         return safe_strftime(date.fromtimestamp(timestamp),
                              USER_DATE_FORMAT)
     return SCALAR_NEVER_WAS
 def test_safe_strftime_abbr_year(self):
     dt = datetime.datetime(2015, 1, 1, 12, 24, 48)
     self.assertEqual(safe_strftime(dt, '%y-%m-%dT%H:%M:%SZ'),
                      '15-01-01T12:24:48Z')
 def test_safe_strftime_abbr_year(self):
     dt = datetime.datetime(2015, 1, 1, 12, 24, 48)
     self.assertEqual(safe_strftime(dt, '%y-%m-%dT%H:%M:%SZ'),
                      '15-01-01T12:24:48Z')
Exemplo n.º 7
0
 def ui_string(self, fmt=USER_DATETIME_FORMAT):
     return safe_strftime(self._datetime, fmt)
Exemplo n.º 8
0
 def ui_string(self, fmt=SERVER_DATETIME_FORMAT):
     return safe_strftime(self._datetime.replace(tzinfo=pytz.utc), fmt)
Exemplo n.º 9
0
 def _unwrap(self, value):
     _assert(value.tzinfo is None,
             "Can't set a USecDateTimeProperty to an offset-aware datetime")
     return value, safe_strftime(value, '%Y-%m-%dT%H:%M:%S.%fZ')
Exemplo n.º 10
0
 def _unwrap(self, value):
     _assert(
         value.tzinfo is None,
         "Can't set a USecDateTimeProperty to an offset-aware datetime")
     return value, safe_strftime(value, '%Y-%m-%dT%H:%M:%S.%fZ')
Exemplo n.º 11
0
 def _fmt_timestamp(timestamp):
     if timestamp is not None and timestamp >= 0:
         return safe_strftime(date.fromtimestamp(timestamp), USER_DATE_FORMAT)
     return SCALAR_NEVER_WAS
Exemplo n.º 12
0
 def ui_string(self, fmt=USER_DATETIME_FORMAT):
     return safe_strftime(self._datetime, fmt)
Exemplo n.º 13
0
 def ui_string(self, fmt=SERVER_DATETIME_FORMAT):
     return safe_strftime(self._datetime.replace(tzinfo=pytz.utc), fmt)
Exemplo n.º 14
0
 def _fmt_ordinal(ordinal):
     if ordinal is not None and ordinal >= 0:
         return safe_strftime(date.fromordinal(ordinal), USER_DATE_FORMAT)
     return SCALAR_NEVER_WAS
Exemplo n.º 15
0
 def _fmt_ordinal(ordinal):
     if ordinal is not None and ordinal >= 0:
         return safe_strftime(date.fromordinal(ordinal),
                              USER_DATE_FORMAT)
     return SCALAR_NEVER_WAS