示例#1
0
def nicerepr(val,
             nd=None,
             none_value=NONE_VALUE,
             dateformat=None,
             timeformat=None,
             settings=None,
             **kwargs):
    '''Prettify a value to be displayed in html.

:parameter val: value to prettify.
:parameter nd: numerical accuracy for floating point numbers.
'''
    if val is None:
        return NONE_VALUE
    elif is_safe(val):
        return val
    elif isinstance(val, date):
        return smart_time(val, dateformat, timeformat, settings)
    elif isinstance(val, bool):
        if val:
            return with_icon('true')
        else:
            return with_icon('false')
    elif hasattr(val, '_meta'):
        return val._meta.name
    else:
        val = to_string(val)
        try:
            return significant_format(val, n=nd)
        except ValueError:
            if val.startswith('http://') or val.startswith('https://'):
                val = mark_safe('<a href="{0}">{0}</a>'.format(val))
            return val
示例#2
0
def dump_data_value(v):
    if not is_string(v):
        if isinstance(v,bytes):
            v = v.decode()
        else:
            v = json.dumps(v)
    return mark_safe(v)
示例#3
0
def dump_data_value(v):
    if not is_string(v):
        if isinstance(v, bytes):
            v = v.decode()
        else:
            v = json.dumps(v)
    return mark_safe(v)
示例#4
0
 def render_css(self):
     prefix = self.settings.get("MEDIA_URL", "")
     absolute = self.absolute_path
     done = set()
     for medium in sorted(self._css):
         paths = self._css[medium]
         medium = "" if medium == "all" else " media='%s'" % medium
         for path in paths:
             url = path[0]
             if url in done:
                 continue
             done.add(url)
             link = "<link href='%s' type='text/css'%s rel='stylesheet'/>" % (absolute(url, prefix), medium)
             if len(path) == 2:
                 link = "<!--[if %s]>%s<![endif]-->" % (path[1], link)
             yield mark_safe(link)
示例#5
0
 def render_css(self):
     prefix = self.settings.get('MEDIA_URL','')
     absolute = self.absolute_path
     done = set()
     for medium in sorted(self._css):
         paths = self._css[medium]
         medium = '' if medium == 'all' else " media='%s'" % medium
         for path in paths:
             url = path[0]
             if url in done:
                 continue
             done.add(url)
             link = "<link href='%s' type='text/css'%s rel='stylesheet'/>"\
                     % (absolute(url, prefix), medium)
             if len(path) == 2:
                 link = '<!--[if %s]>%s<![endif]-->' % (path[1],link)
             yield mark_safe(link)
示例#6
0
def action_checkbox(val, id):
    '''Return html for the action checkbox'''
    if val:
        chk = Widget('input:checkbox', name='action-item', value=id)
        val = divchk.format(chk.render(), val)
    return mark_safe(val)
示例#7
0
__all__ = ['flatatt',
           'render',
           'html_trace',
           'StreamRenderer',
           'WidgetMaker',
           'Widget',
           'Div',
           'Anchor',
           'Img',
           'NON_BREACKING_SPACE']


default_widgets_makers = {}

NON_BREACKING_SPACE = mark_safe('&nbsp;')

def attrsiter(attrs):
    for k, v in iteritems(attrs):
        if v is not None:
            yield " %s='%s'" % (k, escape(v, force=True))


def flatatt(attrs):
    return ''.join(attrsiter(attrs))

def render(request, data):
    if isinstance(data, Widget):
        return data.render(request)
    else:
        return data
示例#8
0
from djpcms.utils.httpurl import ispy3k, is_string, to_string, iteritems,\
                                 is_string_or_native_string, itervalues

if ispy3k:
    from itertools import zip_longest
else:  # pragma nocover
    from itertools import izip_longest as zip_longest

__all__ = [
    'flatatt', 'render', 'html_trace', 'StreamRenderer', 'WidgetMaker',
    'Widget', 'Div', 'Anchor', 'Img', 'NON_BREACKING_SPACE'
]

default_widgets_makers = {}

NON_BREACKING_SPACE = mark_safe('&nbsp;')


def attrsiter(attrs):
    for k, v in iteritems(attrs):
        if v is not None:
            yield " %s='%s'" % (k, escape(v, force=True))


def flatatt(attrs):
    return ''.join(attrsiter(attrs))


def render(request, data):
    if isinstance(data, Widget):
        return data.render(request)