Ejemplo n.º 1
0
def grid(options, id=None):
    if not id:
        id = 'grid_%s' % get_random_string(5)
    script = grid_script % (id, json.dumps(options))
    container = Html('div').attr('rest-grid', 'luxgrids.%s' % id)
    container.append(script)
    return container.render()
Ejemplo n.º 2
0
    def inner_html(self, request, page, self_comp=''):
        '''Build page html content using json layout.
        :param layout: json (with rows and components) e.g.
            layout = {
                'rows': [
                    {},
                    {cols: ['col-md-6', 'col-md-6']},
                    {cols: ['col-md-6', 'col-md-6']}
                ],
                'components': [
                    {'type': 'text', 'id': 1, 'row': 0, 'col': 0, 'pos': 0},
                    {'type': 'gallery', 'id': 2, 'row': 1, 'col': 1, 'pos': 0}
                ]
            }
        :return: raw html
            <div class="row">
                <div class="col-md-6">
                    <render-component id="1" text></render-component>
                </div>
                <div class="col-md-6"></div>
            </div>
            <div class="row">
                    <div class="col-md-6"></div>
                    <div class="col-md-6">
                        <render-component id="2" gallery></render-component>
                    </div>
            </div>
        '''
        layout = page.layout
        if layout:
            try:
                layout = json.loads(layout)
            except Exception:
                request.app.logger.exception('Could not parse layout')
                layout = None

        if not layout:
            layout = dict(rows=[{}])

        components = layout.get('components') or []
        if not components:
            components.append(dict(type='self'))

        inner = Html(None)

        # Loop over rows
        for row_idx, row in enumerate(layout.get('rows', ())):
            row = AttributeDictionary(row)
            if row.cols:
                html = self._row(row, components, self_comp)
            else:
                html = self._component(components[0], self_comp)
                html = super().inner_html(request, page, html)

            inner.append(html)

        return inner.render(request)
Ejemplo n.º 3
0
 def _inner_form(self, form, request):
     yield form.is_valid()
     html = Html(None)
     for child in self.children:
         html.append(child(form, request))
     for child in form.inputs:
         html.append(child)
     content = yield html.render(request)
     coroutine_return(content)
Ejemplo n.º 4
0
    def inner_html(self, request, page, self_comp=''):
        '''Build page html content using json layout.
        :param layout: json (with rows and components) e.g.
            layout = {
                'rows': [
                    {},
                    {cols: ['col-md-6', 'col-md-6']},
                    {cols: ['col-md-6', 'col-md-6']}
                ],
                'components': [
                    {'type': 'text', 'id': 1, 'row': 0, 'col': 0, 'pos': 0},
                    {'type': 'gallery', 'id': 2, 'row': 1, 'col': 1, 'pos': 0}
                ]
            }
        :return: raw html
            <div class="row">
                <div class="col-md-6">
                    <render-component id="1" text></render-component>
                </div>
                <div class="col-md-6"></div>
            </div>
            <div class="row">
                    <div class="col-md-6"></div>
                    <div class="col-md-6">
                        <render-component id="2" gallery></render-component>
                    </div>
            </div>
        '''
        layout = page.layout
        if layout:
            try:
                layout = json.loads(layout)
            except Exception:
                request.app.logger.exception('Could not parse layout')
                layout = None

        if not layout:
            layout = dict(rows=[{}])

        components = layout.get('components') or []
        if not components:
            components.append(dict(type='self'))

        inner = Html(None)

        # Loop over rows
        for row_idx, row in enumerate(layout.get('rows', ())):
            row = AttributeDictionary(row)
            if row.cols:
                html = self._row(row, components, self_comp)
            else:
                html = self._component(components[0], self_comp)
                html = super().inner_html(request, page, html)

            inner.append(html)

        return inner.render(request)
Ejemplo n.º 5
0
 def _inner_form(self, form, request):
     yield form.is_valid()
     html = Html(None)
     for child in self.children:
         html.append(child(form, request))
     for child in form.inputs:
         html.append(child)
     content = yield html.render(request)
     coroutine_return(content)
Ejemplo n.º 6
0
 def get(self, request):
     user = request.cache.session.user
     if user.is_authenticated():
         yield smart_redirect(request)
     html = Html("div")
     for api in apis.available(request.app.config):
         a = api.html_login_link(request)
         html.append(a)
     response = yield html.http_response(request)
     coroutine_return(response)
Ejemplo n.º 7
0
 def __call__(self, form, request):
     handler = LAYOUT_HANDLERS.get(self.style, LAYOUT_HANDLERS[''])
     render = getattr(handler, self.name, handler.default)
     html = Html(self.tag)
     if self.legend:
         html.append('<legend>%s</legend>' % self.legend)
     for child in self.children:
         for txt in child(form, request, render):
             html.append(txt)
     return html
Ejemplo n.º 8
0
 def get(self, request):
     user = request.cache.session.user
     if user.is_authenticated():
         yield smart_redirect(request)
     html = Html('div')
     for api in apis.available(request.app.config):
         a = api.html_login_link(request)
         html.append(a)
     response = yield html.http_response(request)
     coroutine_return(response)
Ejemplo n.º 9
0
 def __call__(self, form, request):
     handler = LAYOUT_HANDLERS.get(self.style, LAYOUT_HANDLERS[''])
     render = getattr(handler, self.name, handler.default)
     html = Html(self.tag)
     if self.legend:
         html.append('<legend>%s</legend>' % self.legend)
     for child in self.children:
         for txt in child(form, request, render):
             html.append(txt)
     return html
Ejemplo n.º 10
0
 def __call__(self, form, request=None, tag='form', cn=None, method='post',
              enctype=None, **params):
     enctype = enctype or 'multipart/form-data'
     if request:
         cn = cn or 'ajax'
         request.html_document.head.scripts.require('jquery-form')
     # we need to make sure the form is validated
     html = Html(tag, cn=cn, method=method, enctype=enctype, **params)
     if self.style:
         html.addClass('form-%s' % self.style)
     html.append(self._inner_form(form, request))
     return html
Ejemplo n.º 11
0
 def navigation(self, request, routers, levels=None):
     '''Create an ul with links.'''
     levels = levels or self.config['NAVIGATION_LEVELS']
     ul = Html('ul', cn=self.config['NAVIGATION_CLASSES'])
     for _, router in sorted(self._ordered_nav(routers),
                             key=lambda x: x[0]):
         if router.parameters.navigation:
             try:
                 link = router.link()
             except KeyError:
                 continue
             ul.append(link)
     return ul.render(request)
Ejemplo n.º 12
0
 def navigation(self, request, routers, levels=None):
     '''Create an ul with links.'''
     levels = levels or self.config['NAVIGATION_LEVELS']
     ul = Html('ul', cn=self.config['NAVIGATION_CLASSES'])
     for _, router in sorted(self._ordered_nav(routers),
                             key=lambda x: x[0]):
         if router.parameters.navigation:
             try:
                 link = router.link()
             except KeyError:
                 continue
             ul.append(link)
     return ul.render(request)
Ejemplo n.º 13
0
 def __call__(self,
              form,
              request=None,
              tag='form',
              cn=None,
              method='post',
              enctype=None,
              **params):
     enctype = enctype or 'multipart/form-data'
     if request:
         cn = cn or 'ajax'
         request.html_document.head.scripts.require('jquery-form')
     # we need to make sure the form is validated
     html = Html(tag, cn=cn, method=method, enctype=enctype, **params)
     if self.style:
         html.addClass('form-%s' % self.style)
     html.append(self._inner_form(form, request))
     return html
Ejemplo n.º 14
0
 def _get_content(self, page, context):
     container = Html(None)
     if 'content_ids' not in context:
         context['content_ids'] = {}
     ids = context['content_ids']
     for content in page.content.get(self.key) or ():
         template = content.get('template')
         row = Html('div', template=template)
         for content in content.get('children') or ():
             column = Html('div')
             for bc in content or ():
                 block = Html('div', template=bc.get('template'))
                 for data in bc.get('children') or ():
                     if not data:
                         continue
                     content = data.pop('content', None)
                     if content is None:
                         continue
                     if isinstance(content, dict):
                         elem = Html('div', data=content)
                         # This is THE very special case where the
                         # content in this element is the content
                         # served by the url if there was no cms extension
                         if content.get(CONTENT_URL) == THIS:
                             value = context.get(THIS)
                             elem.append(Html('div', value, field=THIS))
                     else:
                         # id, we need to retrieve the content
                         content = int_or_string(content)
                         elem = Html('div')
                         if content in ids:
                             ids[content].append(elem)
                         else:
                             ids[content] = [elem]
                     elem.data(data)
                     block.append(elem)
                 column.append(block)
             row.append(column)
         container.append(row)
     return container
Ejemplo n.º 15
0
 def _get_content(self, page, context):
     container = Html(None)
     if 'content_ids' not in context:
         context['content_ids'] = {}
     ids = context['content_ids']
     for content in page.content.get(self.key) or ():
         template = content.get('template')
         row = Html('div', template=template)
         for content in content.get('children') or ():
             column = Html('div')
             for bc in content or ():
                 block = Html('div', template=bc.get('template'))
                 for data in bc.get('children') or ():
                     if not data:
                         continue
                     content = data.pop('content', None)
                     if content is None:
                         continue
                     if isinstance(content, dict):
                         elem = Html('div', data=content)
                         # This is THE very special case where the
                         # content in this element is the content
                         # served by the url if there was no cms extension
                         if content.get(CONTENT_URL) == THIS:
                             value = context.get(THIS)
                             elem.append(Html('div', value, field=THIS))
                     else:
                         # id, we need to retrieve the content
                         content = int_or_string(content)
                         elem = Html('div')
                         if content in ids:
                             ids[content].append(elem)
                         else:
                             ids[content] = [elem]
                     elem.data(data)
                     block.append(elem)
                 column.append(block)
             row.append(column)
         container.append(row)
     return container
Ejemplo n.º 16
0
 def fieldset(self, html, bfield, container):
     control = Html('div', cn='control-group')
     if html.attr('type') == 'checkbox':
         control.append(Html('label', html, bfield.label, cn='checkbox'))
     else:
         label = Html('label', bfield.label, cn='control-label')
         label.attr('for', bfield.id)
         control.append(label)
         control.append(Html('div', html, cn='controls'))
     yield control
Ejemplo n.º 17
0
 def fieldset(self, html, bfield, container):
     control = Html('div', cn='control-group')
     if html.attr('type') == 'checkbox':
         control.append(Html('label', html, bfield.label, cn='checkbox'))
     else:
         label = Html('label', bfield.label, cn='control-label')
         label.attr('for', bfield.id)
         control.append(label)
         control.append(Html('div', html, cn='controls'))
     yield control
Ejemplo n.º 18
0
    def _row(self, row, components, self_comp):
        fluid = '-fluid' if row.fluid else ''
        container = Html('div', cn='container%s' % fluid)
        htmlRow = Html('div', cn='row')
        container.append(htmlRow)

        for col_idx, col in enumerate(row.cols):
            htmlCol = Html('div', cn=col)
            htmlRow.append(htmlCol)

            for comp in sorted(components, key=lambda c: c.get('pos', 0)):
                html = self._component(comp, self_comp)
                htmlCol.append(Html('div', html, cn='block'))

        return container
Ejemplo n.º 19
0
    def _row(self, row, components, self_comp):
        fluid = '-fluid' if row.fluid else ''
        container = Html('div', cn='container%s' % fluid)
        htmlRow = Html('div', cn='row')
        container.append(htmlRow)

        for col_idx, col in enumerate(row.cols):
            htmlCol = Html('div', cn=col)
            htmlRow.append(htmlCol)

            for comp in sorted(components, key=lambda c: c.get('pos', 0)):
                html = self._component(comp, self_comp)
                htmlCol.append(Html('div', html, cn='block'))

        return container
Ejemplo n.º 20
0
 def main(self, request, data):
     ul = Html('ul', cn='nav')
     for link in data:
         ul.append(link)
     return ul
Ejemplo n.º 21
0
 def secondary(self, request, data):
     ul = Html('ul', cn='nav secondary')
     for link in data:
         ul.append(link)
     return ul
Ejemplo n.º 22
0
 def user(self, request, data):
     ul = Html('ul', cn='nav user')
     for link in data:
         ul.append(link)
     return ul
Ejemplo n.º 23
0
 def secondary(self, request, data):
     ul = Html('ul', cn='nav secondary')
     for link in data:
         ul.append(link)
     return ul
Ejemplo n.º 24
0
 def main(self, request, data):
     ul = Html('ul', cn='nav')
     for link in data:
         ul.append(link)
     return ul
Ejemplo n.º 25
0
 def user(self, request, data):
     ul = Html('ul', cn='nav user')
     for link in data:
         ul.append(link)
     return ul