Ejemplo n.º 1
0
def link_to(label, *args, **kwargs):
    """produce a link

    >>> ctx.site = lambda: None
    >>> ctx.site.url = ''

    >>> link_to('Company', 'http://company.com')
    '<a href="http://company.com">Company</a>'

    >>> link_to('http://company.com')
    '<a href="http://company.com">http://company.com</a>'

    >>> link_to('http://company.com', q='test')
    '<a href="http://company.com?q=test">http://company.com</a>'
    """
    nargs = args or [label]
    return html.tag('a', label, href=url_for(*nargs, **kwargs))
Ejemplo n.º 2
0
def form_for(*args, **kwargs):
    """returns a form with optional hidden values

    >>> print(form_for('test'))
    <form action="<dz:request_path>" class="clearfix" enctype="application/x-www-form-urlencoded" id="zoom_form" method="POST" name="zoom_form">
    <input name="csrf_token" type="hidden" value="<dz:csrf_token>" />
    test
    </form>


    """

    params = kwargs.copy()
    name = params.pop('form_name', 'zoom_form')
    id = params.pop('id', 'zoom_form')
    method = params.pop('method', 'POST')
    action = params.pop('action', '<dz:request_path>')
    enctype = params.pop('enctype', 'application/x-www-form-urlencoded')

    t = []
    if method == 'POST':
        request = zoom.system.request
        if hasattr(request, 'session'):
            request.session.csrf_token = uuid.uuid4().hex
        t.append(html.hidden(name='csrf_token', value='<dz:csrf_token>'))

    content = []
    for arg in args:
        if arg:
            content.append(type(arg) == str and arg or arg.edit())
            if hasattr(arg, 'requires_multipart_form'
                       ) and arg.requires_multipart_form():
                enctype = "multipart/form-data"

    for key, value in params.items():
        t.append(html.hidden(name=key, value=value))

    return html.tag('form',
                    '\n' + '\n'.join(t + content) + '\n',
                    action=action,
                    name=name,
                    id=name,
                    method=method,
                    enctype=enctype,
                    classed='clearfix')
Ejemplo n.º 3
0
def link_to(label, *args, **kwargs):
    """produce a link

    >>> zoom.system.site = lambda: None
    >>> zoom.system.site.url = ''

    >>> link_to('Company', 'http://company.com')
    '<a href="http://company.com" name="link-to-company">Company</a>'

    >>> link_to('http://company.com')
    '<a href="http://company.com" name="link-to-http-company-com">http://company.com</a>'

    >>> link_to('http://company.com', q='test')
    '<a href="http://company.com?q=test" name="link-to-http-company-com">http://company.com</a>'
    """
    nargs = args or [label]
    return html.tag(
        'a',
        label,
        href=url_for(*nargs, **kwargs),
        name='link-to-' +
        zoom.utils.id_for(str(label).replace('.', '-').replace(':', '-')),
    )
Ejemplo n.º 4
0
def link_to_page(label, *args, **kwargs):
    nargs = args or [label.lower().replace(' ', '_')]
    return html.tag('a', label, href=url_for_page(*nargs, **kwargs))
Ejemplo n.º 5
0
 def access_link(self):
     return html.tag('a', self.filename, href=self.access_url)
Ejemplo n.º 6
0
def as_links(items, select=None, filter=None):
    """generate an unordered list of links

    >>> as_links(['one', 'two'])
    '<ul><li><a href="<dz:app_url>">one</a></li><li><a href="<dz:app_url>/two">two</a></li></ul>'

    >>> as_links([('one', '/1'), 'two'])
    '<ul><li><a href="/1">one</a></li><li><a href="<dz:app_url>/two">two</a></li></ul>'

    >>> as_links([('uno', 'one', '/1'), 'two'], select=lambda a: a.name=='uno')
    '<ul><li class="active"><a class="active" href="/1">one</a></li><li><a href="<dz:app_url>/two">two</a></li></ul>'

    >>> as_links(['one', 'two'], select=lambda a: a.name=='two')
    '<ul><li><a href="<dz:app_url>">one</a></li><li class="active"><a class="active" href="<dz:app_url>/two">two</a></li></ul>'
    """
    logger = logging.getLogger(__name__)

    def as_link_item(n, item):

        if type(item) == str:
            # only the label was provided
            name = n and id_for(item) or ''
            url = tag_for('app_url') + (n and '/' + name or '')
            return Link(name=name or 'index', label=item, url=url)

        elif len(item) == 2:
            # the label and the URL were provided
            name = n and id_for(item[0]) or ''
            url = tag_for('app_url') + (n and '/' + name or '')
            return Link(name=name or 'index', label=item[0], url=item[1])

        elif len(item) == 3:
            # the name, label and URL were provided
            return Link(name=item[0], label=item[1], url=item[2])

        else:
            raise Exception('unkown menu item {}'.format(repr(item),))

    def as_link_items(items):
        for n, item in enumerate(items):
            link_item = as_link_item(n, item)
            if not filter or filter(link_item):
                yield link_item

    if items is None:
        return ''

    links = []

    for link_item in as_link_items(items):
        selected = select and select(link_item)
        attributes = {}
        if selected:
            attributes['class'] = 'active'
        links.append(
            tag(
                'li',
                a(
                    link_item.label,
                    href=link_item.url,
                    **attributes
                ),
                **attributes
            )
        )
    return tag('ul', ''.join(links))
Ejemplo n.º 7
0
def link_to_page(label, *args, **kwargs):
    nargs = args or [label]
    return html.tag('a', label, href=url_for_page(*nargs, **kwargs))
Ejemplo n.º 8
0
def use_common_package(message):
    zoom.requires('common_package_test')
    return zoom.Component(h.tag('div', message, id='common_package_test'))