Exemple #1
0
def fill(tag_start, tag_end, text, callback):

    def replace_tag(match):

        name     = match.groups(1)[0].lower()
        rest     = match.group(0)[len(name)+len(tag_start):-len(tag_end)]
        parts    = tag_parts.findall(rest)
        keywords = dict(a and (a,b) or c and (c,d) or e and (e,f) for (a,b,c,d,e,f,g,h,i) in parts if a or c or e)
        args     = [h or i or "" for (a,b,c,d,e,f,g,h,i) in parts if h or i or g]

        result = callback(name, *args, **keywords)
        if result == None: 
            result = match.group(0)
        return unisafe(result)
    
    expr = """%(tag_start)s([a-z0-9_]+)\s*(.*?)%(tag_end)s""" % dict(tag_start=tag_start, tag_end=tag_end)
    innerre = re.compile(expr,re.IGNORECASE)
    
    result = []
    lastindex = 0
    
    for outermatch in re.finditer("<!--.*?-->",text):
        text_between = text[lastindex:outermatch.start()]
        new_text = innerre.sub(replace_tag, unisafe(text_between))
        result.append(new_text)
        lastindex = outermatch.end()
        result.append(outermatch.group())
    text_after = text[lastindex:]
    result.append(innerre.sub(replace_tag, unisafe(text_after)))
    
    return u''.join(unisafe(x) for x in result)
Exemple #2
0
def render(content='', items=None):
    """Render some content"""
    if items != None:
        content = unisafe(content)
        if hasattr(items, 'keys'):
            t = content % items
        else:
            t = ''.join(content % item for item in items)
    else:
        t = content
    return unisafe(t)
Exemple #3
0
def render(content='', items=None):
    """Render some content"""
    if items != None:
        content = unisafe(content)
        if hasattr(items,'keys'):
            t = content % items
        else:
            t = ''.join(content % item for item in items)
    else:
        t = content
    return unisafe(t)
Exemple #4
0
    def render_header(self):
        def render_search(value):
            CLEAR_LAYOUT = '<span class="clear"><a href="%s"><img src="/static/dz/images/remove_filter.png"></a></span>'
            SEARCH_FIELDS = '<input type="text" class="text_field" id="search_text" name="q" value="%s">%s<input class="search_button" type=submit value="Search">'
            SEARCH_BUTTON = '<input class="search_button" type=submit value="Search">'
            SEARCH_LAYOUT = '<div class="search">%s</div>'

            if value == None:
                return ''
            elif value == '':
                clear = '<span class="clear"></span>'
            else:
                clear = CLEAR_LAYOUT % ('/'.join([''] + route + ['clear']))
            return SEARCH_LAYOUT % form_for(
                SEARCH_FIELDS % (websafe(value), clear), method='GET')

        def render_actions(items):
            return as_actions(items)

        header_required = self.title or self.subtitle or self.search != None or self.actions
        if not header_required: return ''
        return unisafe(HEADER_LAYOUT % dict(title=self.title,
                                            subtitle=self.subtitle,
                                            search=render_search(self.search),
                                            actions=as_actions(self.actions)))
Exemple #5
0
    def render_header(self):

        def render_search(value):
            CLEAR_LAYOUT  = '<span class="clear"><a href="%s"><img src="/static/dz/images/remove_filter.png"></a></span>'
            SEARCH_FIELDS = '<input type="text" class="text_field" id="search_text" name="q" value="%s">%s<input class="search_button" type=submit value="Search">'
            SEARCH_BUTTON = '<input class="search_button" type=submit value="Search">'
            SEARCH_LAYOUT = '<div class="search">%s</div>'

            if value == None:
                return ''
            elif value == '':
                clear = '<span class="clear"></span>'
            else:
                clear = CLEAR_LAYOUT % ('/'.join([''] + route + ['clear']))
            return  SEARCH_LAYOUT % form_for(SEARCH_FIELDS % (websafe(value),clear), method='GET')

        def render_actions(items):
            return as_actions(items)

        header_required = self.title or self.subtitle or self.search!=None or self.actions
        if not header_required: return ''
        return unisafe(HEADER_LAYOUT % dict(
                title=self.title,
                subtitle=self.subtitle,
                search=render_search(self.search),
                actions=as_actions(self.actions)))
Exemple #6
0
def page(content='', template=None, callback=None, css=None, js=None, title=None, search=None, actions=None, items=None, head=''):

    def render_search(value):
        if value == None: 
            return ''
        elif value == '':
            clear = '<span class="clear"></span>'
        else:
            clear = '<span class="clear"><a href="%s"><img src="/static/images/remove_filter.png"></a></span>' % ('/'.join([''] + route + ['clear']))
        return '<div class="search">%s</div>' % form_for('<input type="text" class="text_field" id="search_text" name="q" value="%s">%s<input class="search_button" type=submit value="Search">' % (value,clear), method='GET')


    header_layout = """
<table id="title_bar"><tr>
<td id="title_bar_left">
<H1>%(title)s</H1>
</td>
<td id="title_bar_right">
%(actions)s
%(search)s
</td>
</tr></table>

"""
    if len(route)>1:
        breadcrumb = link_to(system.app.title,'/'+system.app.name)
    else:
        breadcrumb = ''

    system.result = page = Page()
    if title or actions:
        page_header = header_layout % dict(title=title or '', search=render_search(search), actions=as_actions(actions))
    else:
        page_header = ''

    page.content = unisafe(page_header) + unisafe(render(unisafe(content), items))
    page.css = css or ''
    page.js = js or ''
    page.callback = callback
    page.template = template or system.default_template
    page.head = head
    return page
Exemple #7
0
    def replace_tag(match):

        name     = match.groups(1)[0].lower()
        rest     = match.group(0)[len(name)+len(tag_start):-len(tag_end)]
        parts    = tag_parts.findall(rest)
        keywords = dict(a and (a,b) or c and (c,d) or e and (e,f) for (a,b,c,d,e,f,g,h,i) in parts if a or c or e)
        args     = [h or i or "" for (a,b,c,d,e,f,g,h,i) in parts if h or i or g]

        result = callback(name, *args, **keywords)
        if result == None: 
            result = match.group(0)
        return unisafe(result)
Exemple #8
0
def fill(tag_start, tag_end, text, callback):
    def replace_tag(match):

        name = match.groups(1)[0].lower()
        rest = match.group(0)[len(name) + len(tag_start):-len(tag_end)]
        parts = tag_parts.findall(rest)
        keywords = dict(a and (a, b) or c and (c, d) or e and (e, f)
                        for (a, b, c, d, e, f, g, h, i) in parts
                        if a or c or e)
        args = [
            h or i or "" for (a, b, c, d, e, f, g, h, i) in parts
            if h or i or g
        ]

        result = callback(name, *args, **keywords)
        if result == None:
            result = match.group(0)
        return unisafe(result)

    tags = (tag_start, tag_end)
    if not tags in patterns:
        patterns[tags] = re.compile(pattern_tpl % (tag_start, tag_end),
                                    re.IGNORECASE)
    innerre = patterns[tags]

    result = []
    lastindex = 0

    for outermatch in re.finditer("<!--.*?-->", text):
        text_between = text[lastindex:outermatch.start()]
        new_text = innerre.sub(replace_tag, unisafe(text_between))
        result.append(new_text)
        lastindex = outermatch.end()
        result.append(outermatch.group())
    text_after = text[lastindex:]
    result.append(innerre.sub(replace_tag, unisafe(text_after)))

    return u''.join(unisafe(x) for x in result)
Exemple #9
0
    def replace_tag(match):

        name = match.groups(1)[0].lower()
        rest = match.group(0)[len(name) + len(tag_start):-len(tag_end)]
        parts = tag_parts.findall(rest)
        keywords = dict(a and (a, b) or c and (c, d) or e and (e, f)
                        for (a, b, c, d, e, f, g, h, i) in parts
                        if a or c or e)
        args = [
            h or i or "" for (a, b, c, d, e, f, g, h, i) in parts
            if h or i or g
        ]

        result = callback(name, *args, **keywords)
        if result == None:
            result = match.group(0)
        return unisafe(result)
Exemple #10
0
        columns = ['_id'] + columns
        t = []
        for i in alist:
            t.append([i[0]] + i)
        alist = t

    head = labels[:]
    if head and on_delete: head.append(' ')
    if head and on_remove: head.append(' ')

    body = []
    for count, row in enumerate(alist, start=1):
        body.append('<tr class="row-{}">'.format(count))

        for colnum, item in enumerate(row):
            wrapping = (nowrap and len(unisafe(item)) < nowrap
                        ) and '<td class="nowrap">{}</td>' or '<td>{}</td>'
            if (on_click or on_delete or on_remove) and colnum == 0:
                key = item
            if on_click and (colnum == 1 or colnum == 0 and len(row) == 1):
                url = '/'.join(route[1:] + [str(key)])
                body.append(
                    wrapping.format(link_to(item, url, *args, **keywords)))
            elif colnum > 0 or not (on_click or on_remove or on_delete):
                body.append(wrapping.format(unisafe(item)))

        if on_delete:
            body.append(trash_can(key, 'delete', on_delete))

        if on_remove:
            body.append(trash_can(key, 'remove', on_remove))
Exemple #11
0
def browse(items, labels=None, columns=None, fields=None, footer=None, title=None, actions=None, header=None, on_click=None, on_delete=None, on_remove=None, *args, **keywords):

    def trash_can(key,kind,action,**args):
        link  = url_for(str(key),action,**args)
        tpl = '<td width=1px><a href="%(link)s"><img src="/themes/%(theme)s/images/%(kind)s.png" border=0 height=13px width=13px alt="%(kind)s"></a></td>'
        return tpl % dict(link=link, kind=kind, theme=system.theme)

    def getcol(item, name):
        try:
            return item[name]
        except:
            return ''

    if labels:
        if not columns:
            if len(items) and hasattr(items[0], 'get'):
                columns = [name_for(label).lower() for label in labels]

            elif len(items) and hasattr(items[0], '__len__') and len(items[0]):
                columns = range(len(labels))

            else:
                columns = [name_for(label).lower() for label in labels]

    else:
        if columns:
            labels = columns
        else:
            if len(items) and hasattr(items[0], 'keys') and callable(getattr(items[0],'keys')):
                labels = columns = items[0].keys()

            elif len(items) and hasattr(items[0], '__dict__'):
                labels = columns = [items[0].__dict__.keys()]

            elif len(items) and hasattr(items[0], 'keys') and callable(getattr(items[0],'keys')):
                labels = columns = [items[0].keys()]

            elif len(items) and hasattr(items[0], '__len__') and len(items[0]):
                labels = items[0]
                columns = range(len(items[0]))
                items = items[1:]

            else:
                if len(items):
                    raise Exception('%s' % hasattr(items[0],'__len__'))
                raise Exception('Unable to infer columns')

    columns = list(columns)
    labels = list(labels)

    if fields:
        #labels = []
        lookup = fields.as_dict()
        for col in columns[len(labels):]:
            if col in lookup:
                label = lookup[col].label
            else:
                label = col
            labels.append(label)

        alist = []
        for item in items:
            fields.initialize(item)
            flookup = fields.display_value()
            row = [flookup.get(col.upper(),getcol(item,col)) for col in columns]
            alist.append(row)
    else:
        alist = [[getcol(item,col) for col in columns] for item in items]

    if (on_click or on_delete or on_remove) and columns[0] <> '_id':
        columns = ['_id'] + columns
        t = []
        for i in alist:
            t.append([i[0]] + i)
        alist = t

    odd_rowclass = 'dark'
    evn_rowclass = 'light'

    t = []
    if labels:
        t.append( '<thead><tr>' )

        colnum = 0
        for label in labels:
            colnum += 1
            t.append('<th>%s</th>' % label)

        if on_delete:
            t.append('<th colwidth=1px>%s</th>' % '')
        if on_remove:
            t.append('<th colwidth=1px>%s</th>' % '')
        t.append( '</tr></thead>' )
        odd_rowclass = 'light' # if there is a header start line shading on 2nd line
        evn_rowclass = 'dark'

    t.append( '<tbody>' )

    count = 0
    for row in alist:
        count += 1
        if count % 2:
            rowclass = odd_rowclass
        else:
            rowclass = evn_rowclass
        t.append( '<tr id="row-%s" class="%s">' % (count, rowclass) )

        colnum = 0
        for item in row:
            #wrapping = len(unicode(item)) < 40 and 'nowrap' or ''
            wrapping = len(unisafe(item)) < 80 and '<td nowrap>%s</td>' or '<td>%s</td>'
            if (on_click or on_delete or on_remove) and colnum == 0:
                key = item
            if on_click and (colnum == 1 or colnum==0 and len(row)==1):
                url = '/'.join(route[1:]+[str(key)])
                #t.append('<td>%s</td>' % link_to(item, url, *args, **keywords))
                t.append(wrapping % link_to(item, url, *args, **keywords))
            elif colnum>0 or not (on_click or on_remove or on_delete):
                #t.append('<td %s>%s</td>' % (wrapping, item))
                t.append(wrapping % unisafe(item))
            colnum += 1

        if on_delete:
            t.append(trash_can(key,'delete',on_delete))

        if on_remove:
            t.append(trash_can(key,'remove',on_remove))

        t.append( '</tr>' )

    t.append( '</tbody>' )

    if not count:
        t.append('<tr><td colspan=%s>None</td></tr>' % len(labels))

    if not header:
        if title:
            header = '<div class="title">%s</div>' %  title
        if actions:
            header += as_actions(actions)

    header_body = header and ('<div class="header">%s</div>' % header) or ''
    footer_body = footer and ('<div class="footer">%s</div>' % footer) or ''

    result = '\n'.join(['<div class="baselist">'] + [header_body] + ['<table>'] + t + ['</table>'] + [footer_body] + ['</div>'])
    return result
Exemple #12
0
    t.append( '<tbody>' )

    count = 0
    for row in alist:
        count += 1
        if count % 2:
            rowclass = odd_rowclass
        else:
            rowclass = evn_rowclass
        t.append( '<tr id="row-%s" class="%s">' % (count, rowclass) )

        colnum = 0
        for item in row:
            #wrapping = len(unicode(item)) < 40 and 'nowrap' or ''
            wrapping = len(unisafe(item)) < 80 and '<td nowrap>%s</td>' or '<td>%s</td>'
            if (on_click or on_delete or on_remove) and colnum == 0:
                key = item
            if on_click and (colnum == 1 or colnum==0 and len(row)==1):
                url = '/'.join(route[1:]+[str(key)])
                #t.append('<td>%s</td>' % link_to(item, url, *args, **keywords))
                t.append(wrapping % link_to(item, url, *args, **keywords))
            elif colnum>0 or not (on_click or on_remove or on_delete):
                #t.append('<td %s>%s</td>' % (wrapping, item))
                t.append(wrapping % unisafe(item))
            colnum += 1

        if on_delete:
            t.append(trash_can(key,'delete',on_delete))

        if on_remove:
Exemple #13
0
        columns = ['_id'] + columns
        t = []
        for i in alist:
            t.append([i[0]] + i)
        alist = t

    head = labels[:]
    if head and on_delete: head.append(' ')
    if head and on_remove: head.append(' ')

    body = []
    for count, row in enumerate(alist, start=1):
        body.append( '<tr class="row-{}">'.format(count) )

        for colnum, item in enumerate(row):
            wrapping = (nowrap and len(unisafe(item)) < nowrap) and '<td class="nowrap">{}</td>' or '<td>{}</td>'
            if (on_click or on_delete or on_remove) and colnum == 0:
                key = item
            if on_click and (colnum == 1 or colnum==0 and len(row)==1):
                url = '/'.join(route[1:]+[str(key)])
                body.append(wrapping.format(link_to(item, url, *args, **keywords)))
            elif colnum>0 or not (on_click or on_remove or on_delete):
                body.append(wrapping.format(unisafe(item)))

        if on_delete:
            body.append(trash_can(key,'delete',on_delete))

        if on_remove:
            body.append(trash_can(key,'remove',on_remove))

        body.append( '</tr>' )
Exemple #14
0
    t.append('<tbody>')

    count = 0
    for row in alist:
        count += 1
        if count % 2:
            rowclass = odd_rowclass
        else:
            rowclass = evn_rowclass
        t.append('<tr id="row-%s" class="%s">' % (count, rowclass))

        colnum = 0
        for item in row:
            #wrapping = len(unicode(item)) < 40 and 'nowrap' or ''
            wrapping = len(
                unisafe(item)) < 80 and '<td nowrap>%s</td>' or '<td>%s</td>'
            if (on_click or on_delete or on_remove) and colnum == 0:
                key = item
            if on_click and (colnum == 1 or colnum == 0 and len(row) == 1):
                url = '/'.join(route[1:] + [str(key)])
                #t.append('<td>%s</td>' % link_to(item, url, *args, **keywords))
                t.append(wrapping % link_to(item, url, *args, **keywords))
            elif colnum > 0 or not (on_click or on_remove or on_delete):
                #t.append('<td %s>%s</td>' % (wrapping, item))
                t.append(wrapping % unisafe(item))
            colnum += 1

        if on_delete:
            t.append(trash_can(key, 'delete', on_delete))

        if on_remove: