def filter_stream(self, req, method, filename, stream, data):
        crumbs = self._get_crumbs(req.session)
        if not crumbs:
            return stream

        add_stylesheet(req, 'breadcrumbs/css/breadcrumbs.css')
        ul = []

        path = req.path_info
        if path.count('/') >= 2:
            realm, resource_id = path.split('/', 2)[1:]
            if '&' in resource_id:
                resource_id = resource_id[0:resource_id.index('&')]
            current = '/'.join((realm, resource_id))
        else:
            current = None

        offset = 0
        if crumbs and crumbs[0] == current:
            offset = 1
        for crumb in crumbs[offset: self.max_crumbs + offset]:
            realm, resource_id = crumb.split('/', 1)
            resource = Resource(realm, resource_id)

            name = get_resource_shortname(self.env, resource)

            if not resource_exists(self.env, resource):
                continue

            title = get_resource_summary(self.env, resource)
            link = req.href(realm, resource_id)

            first = ul == []
            li = tag.li(tag.a(title=title, href=link)(name))
            if first:
                li(class_="first")
            ul.append(li)

        if ul:
            last = ul.pop()
            ul.append(last(class_="last"))
            label = self.label if self.label else "Breadcrumbs:"
            insert = tag.ul(class_="nav", id="breadcrumbs")(tag.li(label), ul)
        else:
            insert = ''

        return stream | Transformer('//div[@id="metanav"]/ul').after(insert)
示例#2
0
 def filter_stream(self, req, method, filename, stream, data):
     if filename != 'wiki_view.html':
         return stream
     items = WikiNegotiator(self.env).make_menu_bar(req)
     if not items or len(items) <= 1:
         return stream
     li = []
     for content, cls in items:
         li.append(tag.li(content, class_=cls))
     insert = tag.div(tag.ul(li), id='langmenu')
     return stream | Transformer(self._menu_location).before(insert)
示例#3
0
 def filter_stream(self, req, method, filename, stream, data):
     if filename != 'wiki_view.html':
         return stream
     items = WikiNegotiator(self.env).make_menu_bar(req)
     if not items or len(items) <= 1:
         return stream
     li = []
     for content, cls in items:
         li.append(tag.li(content, class_=cls))
     insert = tag.div(tag.ul(li), id='langmenu')
     return stream | Transformer(self._menu_location).before(insert)
示例#4
0
    def expand_macro(self, formatter, name, content):
        attachment_type = ""
        if content:
            argv = [arg.strip() for arg in content.split(',')]
            if len(argv) > 0:
                attachment_type = argv[0]

        with self.env.db_transaction as db:
            if attachment_type is None or attachment_type == "":
                attachments = db("""
                   SELECT type,id,filename,size,time,
                    description,author,ipnr FROM attachment
                   """)
            else:
                attachments = db(
                    """
                   SELECT type,id,filename,size,time,
                    description,author,ipnr FROM attachment
                   WHERE type=%s
                   """, (attachment_type, ))

        formatters = {
            'wiki': formatter.href.wiki,
            'ticket': formatter.href.ticket,
            'milestone': formatter.href.milestone,
        }
        types = {
            'wiki': '',
            'ticket': 'ticket ',
            'milestone': 'milestone ',
        }

        return html.ul([
            html.li(
                html.a(filename,
                       href=formatter.href.attachment(type + '/' + id + '/' +
                                                      filename)), " (",
                html.span(pretty_size(size), title=size), ") - added by ",
                html.em(author), " to ",
                html.a(types[type] + ' ' + id, href=formatters[type](id)), ' ')
            for type, id, filename, size, time, description, author, ipnr in
            attachments
            if self._has_perm(type, id, filename, formatter.context)
        ])
示例#5
0
    def expand_macro(self, formatter, name, content):
        attachment_type = ""
        if content:
            argv = [arg.strip() for arg in content.split(',')]
            if len(argv) > 0:
                attachment_type = argv[0]

        with self.env.db_transaction as db:
            if attachment_type is None or attachment_type == "":
                attachments = db("""
                   SELECT type,id,filename,size,time,
                    description,author,ipnr FROM attachment
                   """)
            else:
                attachments = db("""
                   SELECT type,id,filename,size,time,
                    description,author,ipnr FROM attachment
                   WHERE type=%s
                   """, (attachment_type, ))

        formatters = {
            'wiki': formatter.href.wiki,
            'ticket': formatter.href.ticket,
            'milestone': formatter.href.milestone,
        }
        types = {
            'wiki': '',
            'ticket': 'ticket ',
            'milestone': 'milestone ',
        }

        return html.ul(
            [html.li(
                html.a(filename, href=formatter.href.attachment(type + '/' +
                                                                id + '/' +
                                                                filename)),
                " (", html.span(pretty_size(size), title=size), ") - added by ",
                html.em(author), " to ",
                html.a(types[type] + ' ' + id, href=formatters[type](id)), ' ')
             for type, id, filename, size, time, description, author, ipnr
             in attachments
             if self._has_perm(type, id, filename, formatter.context)])
示例#6
0
    def expand_macro(self, formatter, name, content):

        # Create request context.
        context = Context.from_request(formatter.req)('screenshots-wiki')

        # Get database access.
        db = self.env.get_db_cnx()
        context.cursor = db.cursor()

        # Get API component.
        api = self.env[ScreenshotsApi]

        if name == 'Screenshot':
            # Check permission.
            if not formatter.req.perm.has_permission('SCREENSHOTS_VIEW'):
               return html.div('No permissions to see screenshots.',
               class_ = 'system-message')

            # Get macro arguments.
            arguments = content.split(',')

            # Get screenshot ID.
            try:
               screenshot_id = int(arguments[0])
            except:
                 raise TracError("Missing screenshot ID in macro arguments.")

            # Try to get screenshots of that ID.
            screenshot = api.get_screenshot(context, screenshot_id)

            # Build and return macro content.
            if screenshot:
                # Set default values of image attributes.
                attributes = {'align' : 'none',
                              'border' : '1',
                              'format' : 'raw',
                              'alt' : screenshot['description'],
                              'description' : self.default_description}

                # Fill attributes from macro arguments.
                for argument in arguments[1:]:
                    argument = argument.strip()
                    match = self.attributes_re.match(argument)
                    if match:
                        attributes[str(match.group(1))] = match.group(2)

                # Zero width or height means keep original.
                if attributes.has_key('width'):
                    if attributes['width'] == 0:
                        attributes['width'] = screenshot['width']
                if attributes.has_key('height'):
                    if attributes['height'] == 0:
                        attributes['height'] = screenshot['height']

                # If one dimension is missing compute second to keep aspect.
                if not attributes.has_key('width') and \
                  attributes.has_key('height'):
                    attributes['width'] = int(int(attributes['height']) * (
                      float(screenshot['width']) / float(screenshot['height']))
                      + 0.5)
                if not attributes.has_key('height') and \
                  attributes.has_key('width'):
                    attributes['height'] = int(int(attributes['width']) * (
                      float(screenshot['height']) / float(screenshot['width']))
                      + 0.5)

                # If both dimensions are missing keep original.
                if not attributes.has_key('width') and not \
                  attributes.has_key('height'):
                    attributes['width'] = screenshot['width']
                    attributes['height'] = screenshot['height']

                self.log.debug('attributes: %s' % (attributes,))

                # Format screenshot description from template.
                attributes['description'] = self._format_description(context,
                  attributes['description'], screenshot)

                # Make copy of attributes for image tag.
                img_attributes = {}
                for attribute in attributes.keys():
                    if attribute not in ('align', 'border', 'description',
                      'format', 'width', 'height'):
                        img_attributes[attribute] = attributes[attribute]

                # Add CSS for image.
                add_stylesheet(formatter.req, 'screenshots/css/screenshots.css')

                # Build screenshot image and/or screenshot link.
                image = html.img(src = formatter.req.href.screenshots(
                  screenshot['id'], format = 'raw', width = attributes['width'],
                  height = attributes['height']), **img_attributes)
                link = html.a(image, href = formatter.req.href.screenshots(
                  screenshot['id'], format = attributes['format']), title =
                  screenshot['description'], style = 'border-width: %spx;' % (
                  attributes['border'],))
                width_and_border = int(attributes['width']) + 2 * \
                  int(attributes['border'])
                description = html.span(attributes['description'], class_ =
                  'description', style = "width: %spx;" % (width_and_border,))
                auxilary = html.span(link, description, class_ = 'aux',
                  style = "width: %spx;" % (width_and_border,))
                thumbnail_class = 'thumbnail' + ((attributes['align'] == 'left')
                  and '-left' or (attributes['align'] == 'right') and '-right'
                  or '')
                thumbnail = html.span(auxilary, class_ = thumbnail_class)
                return thumbnail
            else:
                return html.a(screenshot_id, href =
                  formatter.req.href.screenshots(), title = content,
                  class_ = 'missing')

        elif name == 'ScreenshotsList':
            # Check permission.
            if not formatter.req.perm.has_permission('SCREENSHOTS_VIEW'):
               return html.div('No permissions to see screenshots.',
               class_ = 'system-message')

            # Get desired list item description
            list_item_description = content or self.default_list_item

            # Get all screenshots.
            screenshots = api.get_screenshots_complete(context)

            # Create and return HTML list of screenshots.
            list_items = []
            for screenshot in screenshots:
                list_item = self._format_description(context,
                  list_item_description, screenshot)
                list_items.append(html.li(html.a(list_item, href =
                  formatter.req.href.screenshots(screenshot['id']))))
            return html.ul(list_items)
示例#7
0
    def _get_menu(self, req, menu_name, nav_orig):
        config_menu, config_options = self._get_config_menus(req, menu_name)
        menu_orig = nav_orig.get(menu_name, [])
        hide_if_no_children = []
        menu_result = []

        if 'inherit' in config_options:
            menu_orig += nav_orig.get(config_options['inherit'], [])

        tree_menu = {}
        for option in sorted(
                menu_orig + [{
                    'name': key
                } for key in config_menu.keys()],
                key=lambda x: int(
                    config_menu.get(x['name'], {}).get('order', 999))):
            name = option['name']
            if 'visited' in tree_menu.get(name, []) \
                    or (config_menu.get(name, {}).get('enabled', True)==False and not 'active' in option)\
                    or config_menu.get(name, {}).get('if_path_info', True)==False \
                    or False in [req.perm.has_permission(perm) for perm in config_menu.get(name, {}).get('perm', [])]:
                continue

            tree_node = tree_menu.setdefault(name, {})
            tree_node.update(option.copy())

            if 'label' in option and 'label' in config_menu.get(name, []):
                del config_menu[name]['label']
            tree_node.update(
                config_menu.get(name, {'parent_name': 'unassigned'}))

            if tree_node.get('hide_if_no_children'):
                hide_if_no_children.append(tree_node)

            tree_node['label'] = html(
                tree_node.setdefault('label', html.a(name)))
            tree_node['visited'] = True
            if tree_node.get('href'):
                tree_node_href = urlsplit(tree_node['href'])
                tree_node.setdefault(
                    'active', tree_node_href[2] == req.path_info
                    and tree_node_href[3] in req.environ['QUERY_STRING'])

            if '_tmp_children' in tree_node:
                tree_node['children'] = html.ul()
                tree_node['label'].append(tree_node['children'])
                tree_node['children'].children.extend(
                    tree_node['_tmp_children'])
                del tree_node['_tmp_children']

            if (tree_node['parent_name']=='unassigned' and not 'unassigned' in config_menu) \
                    or tree_node['parent_name']=='top':
                menu_result.append(tree_node)
                continue

            tree_node['parent'] = tree_menu.setdefault(
                tree_node['parent_name'], {})

            child_node = html.li(
                class_=tree_node.get('active') == True and 'active' or None)
            tree_node['outter_html'] = child_node
            child_node.children = [tree_node['label']]
            if 'label' in tree_node['parent']:
                if not 'children' in tree_node['parent']:
                    tree_node['parent']['children'] = html.ul()
                    tree_node['parent']['label'].append(
                        tree_node['parent']['children'])
                tree_node['parent']['children'].append(child_node)
            else:
                tree_node['parent'].setdefault('_tmp_children',
                                               []).append(child_node)

        for hide_node in hide_if_no_children:
            if not hide_node.get('children'):
                if hide_node['parent_name'] == 'top':
                    pos = menu_result.index(hide_node)
                    del menu_result[pos]
                else:
                    pos = hide_node['parent']['children'].children.index(
                        hide_node['outter_html'])
                    del hide_node['parent']['children'].children[pos]
        return menu_result
示例#8
0
    def _get_menu(self, req, menu_name, nav_orig):
        config_menu, config_options = self._get_config_menus(req, menu_name)
        menu_orig = nav_orig.get(menu_name, [])
        hide_if_no_children = []
        menu_result = []

        if 'inherit' in config_options:
            menu_orig += nav_orig.get(config_options['inherit'], [])

        order = 900
        menu_items = config_menu
        for item in menu_orig:
            order += 1
            name = item['name']
            item.update({
                'has_original': True,
                'order': order,
            })
            if name in menu_items:
                # update original with configured
                item.update(menu_items[name])
            menu_items[name] = item

        tree_menu={}
        active_subitem = None
        active_top = None
        href_path = req.href(req.path_info)
        for name in sorted(menu_items.keys(),
                           key=lambda n: int(menu_items[n].get('order', 999))):
            item = menu_items[name]
            if (item.get('enabled', True)==False and not 'active' in item)\
                    or item.get('if_path_info', True)==False \
                    or ( item.get('hide_if_no_original', False) and not item.get('has_original') )\
                    or False in [req.perm.has_permission(perm) for perm in item.get('perm', [])]:
                continue

            tree_node = tree_menu.setdefault(name, {})
            tree_node.update(item.copy())
            tree_node.setdefault('parent_name', 'unassigned')

            if tree_node.get('hide_if_no_children'):
                hide_if_no_children.append(tree_node)

            if tree_node.get('href'):
                tree_node_href = urlsplit(tree_node['href'])
                tree_node.setdefault('active',
                                     tree_node_href[2].rstrip('/') == href_path and \
                                     tree_node_href[3] == req.query_string)

            if not tree_node.get('has_original'):
                if tree_node.get('href'):
                    label_href = tree_node['href']
                else:
                    label_href = '#'
                label_text = tree_node.get('label_text', name)
                tree_node['label'] = html.a(label_text, href=label_href)
            tree_node['label'] = html(tree_node['label'])

            if '_tmp_children' in tree_node:
                tree_node['children'] = html.ul()
                tree_node['label'].append(tree_node['children'])
                tree_node['children'].children.extend(tree_node['_tmp_children'])
                del tree_node['_tmp_children']

            if (tree_node['parent_name']=='unassigned' and not 'unassigned' in config_menu) \
                    or tree_node['parent_name']=='top':
                if not active_top and tree_node.get('active'):
                    active_top = tree_node
                else:
                    tree_node['active'] = False
                menu_result.append(tree_node)
                continue
            # else working with subitems

            tree_node['parent'] = tree_menu.setdefault(tree_node['parent_name'], {})

            if not active_subitem and tree_node.get('active'):
                active_subitem = tree_node
                active_top     = tree_node['parent']

            child_node = html.li()
            tree_node['outter_html'] = child_node
            child_node.children=[tree_node['label']]
            if 'label' in tree_node['parent']:
                if not 'children' in tree_node['parent']:
                    tree_node['parent']['children'] = html.ul()
                    tree_node['parent']['label'].append(tree_node['parent']['children'])
                tree_node['parent']['children'].append(child_node)
            else:
                tree_node['parent'].setdefault('_tmp_children',[]).append(child_node)

        for hide_node in hide_if_no_children:
            if not hide_node.get('children'):
                if hide_node['parent_name']=='top':
                    pos = menu_result.index(hide_node)
                    del menu_result[pos]
                else:
                    pos = hide_node['parent']['children'].children.index(hide_node['outter_html'])
                    del hide_node['parent']['children'].children[pos]

        if active_top:
            active_name = active_top['name']
            for item in menu_result:
                if item['name'] == active_name:
                    item['active'] = True
                    break

        return menu_result
 def expand_macro(self, formatter, name, content):
     env = formatter.env
     req = formatter.req
     if not content:
         args = []
         kw = {}
     else:
         args, kw = parse_args(content)
     if name == 'ProjectStats':
         if 'wiki' in kw.keys():
             prefix = 'prefix' in kw.keys() and kw['prefix'] or None
             wiki = WikiSystem(env)
             if kw['wiki'] == 'count' or 'count' in args:
                 return tag(len(list(wiki.get_pages(prefix))))
     elif name == 'UserQuery':
         msg_no_perm = tag.p(tag_("(required %(perm)s missing)",
                                  perm=tag.strong('USER_VIEW')),
                             class_='hint')
         if 'perm' in kw.keys():
             perm_sys = PermissionSystem(self.env)
             users = perm_sys.get_users_with_permission(kw['perm'].upper())
         else:
             acct_mgr = AccountManager(env)
             users = list(set(acct_mgr.get_users()))
         if 'locked' in kw.keys() or 'locked' in args:
             guard = AccountGuard(env)
             locked = []
             for user in users:
                 if guard.user_locked(user):
                     locked.append(user)
             if kw.get('locked', 'True').lower() in ('true', 'yes', '1'):
                 users = locked
             else:
                 users = list(set(users) - set(locked))
         elif 'visit' in kw.keys() or 'visit' in args:
             if 'USER_VIEW' not in req.perm:
                 return msg_no_perm
             cols = []
             data = {'accounts': fetch_user_data(env, req), 'cls': 'wiki'}
             for col in ('email', 'name'):
                 if col in args:
                     cols.append(col)
             data['cols'] = cols
             return Chrome(env).render_template(req, 'user_table.html',
                                                data, 'text/html', True)
         if kw.get('format') == 'count' or 'count' in args:
             return tag(len(users))
         if 'USER_VIEW' not in req.perm:
             return msg_no_perm
         if 'email' in args or 'name' in args:
             # Replace username with full name, add email if available.
             for username, name, email in self.env.get_known_users():
                 if username in users:
                     if 'name' not in args or name is None:
                         name = username
                     if 'email' in args and email is not None:
                         email = ''.join(['<', email, '>'])
                         name = ' '.join([name, email])
                     if not username == name:
                         users.pop(users.index(username))
                         users.append(name)
         if not users and 'nomatch' in kw.keys():
             return format_to_oneliner(env, formatter.context,
                                       kw['nomatch'])
         users = sorted(users)
         if kw.get('format') == 'list':
             return tag.ul([
                 tag.li(Chrome(env).format_author(req, user))
                 for user in users
             ])
         else:
             # Default output format: comma-separated list.
             return tag(', '.join(
                 [Chrome(env).format_author(req, user) for user in users]))
示例#10
0
    def render_cloud(self,
                     req,
                     cloud,
                     renderer=None,
                     caseless_sort=False,
                     mincount=None,
                     realms=()):
        """Render a tag cloud.

        :cloud: Dictionary of {object: count} representing the cloud.
        :param renderer: A callable with signature (tag, count, percent)
                         used to render the cloud objects.
        :param caseless_sort: Boolean, whether tag cloud should be sorted
                              case-sensitive.
        :param mincount: Integer threshold to hide tags with smaller count.
        """
        min_px = 10.0
        max_px = 30.0
        scale = 1.0

        if renderer is None:

            def default_renderer(tag, count, percent):
                href = self.get_href(req, realms, tag=Resource('tag', tag))
                return builder.a(tag,
                                 rel='tag',
                                 title='%i' % count,
                                 href=href,
                                 style='font-size: %ipx' %
                                 int(min_px + percent * (max_px - min_px)))

            renderer = default_renderer

        # A LUT from count to n/len(cloud)
        size_lut = dict([
            (c, float(i))
            for i, c in enumerate(sorted(set([r for r in cloud.values()])))
        ])
        if size_lut:
            scale = 1.0 / len(size_lut)

        if caseless_sort:
            # Preserve upper-case precedence within similar tags.
            items = reversed(
                sorted(cloud.iteritems(),
                       key=lambda t: t[0].lower(),
                       reverse=True))
        else:
            items = sorted(cloud.iteritems())
        ul = li = None
        for i, (tag, count) in enumerate(items):
            percent = size_lut[count] * scale
            if mincount and count < as_int(mincount, 1):
                # Tag count is too low.
                continue
            if ul:
                # Found new tag for cloud; now add previously prepared one.
                ul('\n', li)
            else:
                # Found first tag for cloud; now create the list.
                ul = builder.ul(class_='tagcloud')
            # Prepare current tag entry.
            li = builder.li(renderer(tag, count, percent))
        if li:
            # All tags checked; mark latest tag as last one (no tailing colon).
            li(class_='last')
            ul('\n', li, '\n')
        return ul and ul or _("No tags found")
 def expand_macro(self, formatter, name, content):
     env = formatter.env
     req = formatter.req
     if not content:
         args = []
         kw = {}
     else:
         args, kw = parse_args(content)
     if name == 'ProjectStats':
         if 'wiki' in kw.keys():
             prefix = 'prefix' in kw.keys() and kw['prefix'] or None
             wiki = WikiSystem(env)
             if kw['wiki'] == 'count' or 'count' in args:
                 return tag(len(list(wiki.get_pages(prefix))))
     elif name == 'UserQuery':
         msg_no_perm = tag.p(tag_("(required %(perm)s missing)",
                                  perm=tag.strong('USER_VIEW')),
                             class_='hint')
         if 'perm' in kw.keys():
             perm_sys = PermissionSystem(self.env)
             users = perm_sys.get_users_with_permission(kw['perm'].upper())
         else:
             acct_mgr = AccountManager(env)
             users = list(set(acct_mgr.get_users()))
         if 'locked' in kw.keys() or 'locked' in args:
             guard = AccountGuard(env)
             locked = []
             for user in users:
                 if guard.user_locked(user):
                     locked.append(user)
             if kw.get('locked', 'True').lower() in ('true', 'yes', '1'):
                 users = locked
             else:
                 users = list(set(users) - set(locked))
         elif 'visit' in kw.keys() or 'visit' in args:
             if 'USER_VIEW' not in req.perm:
                 return msg_no_perm
             cols = []
             data = {'accounts': fetch_user_data(env, req), 'cls': 'wiki'}
             for col in ('email', 'name'):
                 if col in args:
                     cols.append(col)
             data['cols'] = cols
             return Chrome(env).render_template(
                 req, 'user_table.html', data, 'text/html', True)
         if kw.get('format') == 'count' or 'count' in args:
             return tag(len(users))
         if 'USER_VIEW' not in req.perm:
             return msg_no_perm
         if 'email' in args or 'name' in args:
             # Replace username with full name, add email if available.
             for username, name, email in self.env.get_known_users():
                 if username in users:
                     if 'name' not in args or name is None:
                         name = username
                     if 'email' in args and email is not None:
                         email = ''.join(['<', email, '>'])
                         name = ' '.join([name, email])
                     if not username == name:
                         users.pop(users.index(username))
                         users.append(name)
         if not users and 'nomatch' in kw.keys():
             return format_to_oneliner(env, formatter.context,
                                       kw['nomatch'])
         users = sorted(users)
         if kw.get('format') == 'list':
             return tag.ul([tag.li(Chrome(env).format_author(req, user))
                            for user in users])
         else:
             # Default output format: comma-separated list.
             return tag(', '.join([Chrome(env).format_author(req, user)
                                   for user in users]))
示例#12
0
    def _get_menu(self, req, menu_name, nav_orig):
        config_menu, config_options = self._get_config_menus(req, menu_name)
        menu_orig = nav_orig.get(menu_name, [])
        hide_if_no_children = []
        menu_result = []

        if "inherit" in config_options:
            menu_orig += nav_orig.get(config_options["inherit"], [])

        tree_menu = {}
        for option in sorted(
            menu_orig + [{"name": key} for key in config_menu.keys()],
            key=lambda x: int(config_menu.get(x["name"], {}).get("order", 999)),
        ):
            name = option["name"]
            if (
                "visited" in tree_menu.get(name, [])
                or (config_menu.get(name, {}).get("enabled", True) == False and not "active" in option)
                or config_menu.get(name, {}).get("if_path_info", True) == False
                or False in [req.perm.has_permission(perm) for perm in config_menu.get(name, {}).get("perm", [])]
            ):
                continue

            tree_node = tree_menu.setdefault(name, {})
            tree_node.update(option.copy())

            if "label" in option and "label" in config_menu.get(name, []):
                del config_menu[name]["label"]
            tree_node.update(config_menu.get(name, {"parent_name": "unassigned"}))

            if tree_node.get("hide_if_no_children"):
                hide_if_no_children.append(tree_node)

            tree_node["label"] = html(tree_node.setdefault("label", html.a(name)))
            tree_node["visited"] = True
            if tree_node.get("href"):
                tree_node_href = urlsplit(tree_node["href"])
                tree_node.setdefault(
                    "active", tree_node_href[2] == req.path_info and tree_node_href[3] in req.environ["QUERY_STRING"]
                )

            if "_tmp_children" in tree_node:
                tree_node["children"] = html.ul()
                tree_node["label"].append(tree_node["children"])
                tree_node["children"].children.extend(tree_node["_tmp_children"])
                del tree_node["_tmp_children"]

            if (tree_node["parent_name"] == "unassigned" and not "unassigned" in config_menu) or tree_node[
                "parent_name"
            ] == "top":
                menu_result.append(tree_node)
                continue

            tree_node["parent"] = tree_menu.setdefault(tree_node["parent_name"], {})

            child_node = html.li(class_=tree_node.get("active") == True and "active" or None)
            tree_node["outter_html"] = child_node
            child_node.children = [tree_node["label"]]
            if "label" in tree_node["parent"]:
                if not "children" in tree_node["parent"]:
                    tree_node["parent"]["children"] = html.ul()
                    tree_node["parent"]["label"].append(tree_node["parent"]["children"])
                tree_node["parent"]["children"].append(child_node)
            else:
                tree_node["parent"].setdefault("_tmp_children", []).append(child_node)

        for hide_node in hide_if_no_children:
            if not hide_node.get("children"):
                if hide_node["parent_name"] == "top":
                    pos = menu_result.index(hide_node)
                    del menu_result[pos]
                else:
                    pos = hide_node["parent"]["children"].children.index(hide_node["outter_html"])
                    del hide_node["parent"]["children"].children[pos]
        return menu_result
示例#13
0
 def render_groups(groups):
     return tag.ul(
         [tag.li(isinstance(elt, tuple) and 
                 tag(tag.strong(elt[0]), render_groups(elt[1])) or
                 render_one(elt, pagelangs.get(elt)))
          for elt in groups])
示例#14
0
    def render_macro(self, req, name, content):

        # Get database access.
        db = self.env.get_db_cnx()
        cursor = db.cursor()

        # Get API component.
        api = self.env[ScreenshotsApi]

        if name == 'Screenshot':
            # Check permission.
            if not req.perm.has_permission('SCREENSHOTS_VIEW'):
                return html.div('No permissions to see screenshots.',
                                class_='system-message')

            # Get macro arguments.
            arguments = content.split(',')

            # Get screenshot ID.
            try:
                screenshot_id = int(arguments[0])
            except:
                raise TracError("Missing screenshot ID in macro arguments.")

            # Try to get screenshots of that ID.
            screenshot = api.get_screenshot(cursor, screenshot_id)

            # Build and return macro content.
            if screenshot:
                # Set default values of image attributes.
                attributes = {
                    'align': 'none',
                    'border': '1',
                    'format': 'raw',
                    'width': screenshot['width'],
                    'height': screenshot['height'],
                    'alt': screenshot['description'],
                    'description': self.default_description
                }

                # Fill attributes from macro arguments.
                for argument in arguments[1:]:
                    argument = argument.strip()
                    match = self.attributes_re.match(argument)
                    if match:
                        attributes[str(match.group(1))] = match.group(2)
                self.log.debug('attributes: %s' % (attributes, ))

                # Format screenshot description from template.
                attributes['description'] = self._format_description(
                    attributes['description'], screenshot)

                # Make copy of attributes for image tag.
                img_attributes = {
                    'align': 'center',
                    'style': 'border-width: %spx;' % (attributes['border'], )
                }
                for attribute in attributes.keys():
                    if attribute not in ('align', 'border', 'description',
                                         'format'):
                        img_attributes[attribute] = attributes[attribute]

                # Add CSS for image.
                add_stylesheet(req, 'screenshots/css/screenshots.css')

                # Build screenshot image and/or screenshot link.
                image = html.img(src=req.href.screenshots(
                    screenshot['id'],
                    width=attributes['width'],
                    height=attributes['height'],
                    format='raw'),
                                 **img_attributes)
                link = html.a(image,
                              href=req.href.screenshots(
                                  screenshot['id'],
                                  format=attributes['format']),
                              title=screenshot['description'])
                description = html.span(attributes['description'],
                                        class_='description')
                thumbnail_class = 'thumbnail' + (
                    (attributes['align'] == 'left') and '-left' or
                    (attributes['align'] == 'right') and '-right' or '')
                thumbnail = html.span(link,
                                      ' ',
                                      description,
                                      class_=thumbnail_class,
                                      style="width: %spx;" %
                                      (int(attributes['width']) +
                                       2 * int(attributes['border'], )))
                return thumbnail
            else:
                return html.a(screenshot_id,
                              href=req.href.screenshots(),
                              title=content,
                              class_='missing')

        elif name == 'ScreenshotsList':
            # Check permission.
            if not req.perm.has_permission('SCREENSHOTS_VIEW'):
                return html.div('No permissions to see screenshots.',
                                class_='system-message')

            # Get desired list item description
            list_item_description = content or self.default_list_item

            # Get all screenshots.
            screenshots = api.get_screenshots_complete(cursor)

            # Create and return HTML list of screenshots.
            list_items = []
            for screenshot in screenshots:
                list_item = self._format_description(list_item_description,
                                                     screenshot)
                list_items.append(
                    html.li(
                        html.a(list_item,
                               href=req.href.screenshots(screenshot['id']))))
            return html.ul(list_items)
示例#15
0
    def expand_macro(self, formatter, name, content):

        # Create request context.
        context = Context.from_request(formatter.req)('screenshots-wiki')

        # Get database access.
        db = self.env.get_db_cnx()
        context.cursor = db.cursor()

        # Get API component.
        api = self.env[ScreenshotsApi]

        if name == 'Screenshot':
            # Check permission.
            if not formatter.req.perm.has_permission('SCREENSHOTS_VIEW'):
                return html.div(_("No permissions to see screenshots."),
                                class_='system-message')

            # Get macro arguments.
            arguments = content.split(',')

            # Get screenshot ID.
            try:
                screenshot_id = int(arguments[0])
            except:
                raise TracError(_("Missing screenshot ID in macro arguments."))

            # Try to get screenshots of that ID.
            screenshot = api.get_screenshot(context, screenshot_id)

            # Build and return macro content.
            if screenshot:
                # Set default values of image attributes.
                attributes = {
                    'align': 'none',
                    'border': '1',
                    'format': 'raw',
                    'alt': screenshot['description'],
                    'description': self.default_description
                }

                # Fill attributes from macro arguments.
                for argument in arguments[1:]:
                    argument = argument.strip()
                    match = self.attributes_re.match(argument)
                    if match:
                        attributes[str(match.group(1))] = match.group(2)

                # Zero width or height means keep original.
                if attributes.has_key('width'):
                    if attributes['width'] == 0:
                        attributes['width'] = screenshot['width']
                if attributes.has_key('height'):
                    if attributes['height'] == 0:
                        attributes['height'] = screenshot['height']

                # If one dimension is missing compute second to keep aspect.
                if not attributes.has_key('width') and \
                  attributes.has_key('height'):
                    attributes['width'] = int(
                        int(attributes['height']) *
                        (float(screenshot['width']) /
                         float(screenshot['height'])) + 0.5)
                if not attributes.has_key('height') and \
                  attributes.has_key('width'):
                    attributes['height'] = int(
                        int(attributes['width']) *
                        (float(screenshot['height']) /
                         float(screenshot['width'])) + 0.5)

                # If both dimensions are missing keep original.
                if not attributes.has_key('width') and not \
                  attributes.has_key('height'):
                    attributes['width'] = screenshot['width']
                    attributes['height'] = screenshot['height']

                self.log.debug('attributes: %s' % (attributes, ))

                # Format screenshot description from template.
                attributes['description'] = self._format_description(
                    context, attributes['description'], screenshot)

                # Make copy of attributes for image tag.
                img_attributes = {}
                for attribute in attributes.keys():
                    if attribute not in ('align', 'border', 'description',
                                         'format', 'width', 'height'):
                        img_attributes[attribute] = attributes[attribute]

                # Add CSS for image.
                add_stylesheet(formatter.req,
                               'screenshots/css/screenshots.css')

                # Build screenshot image and/or screenshot link.
                image = html.img(src=formatter.req.href.screenshots(
                    screenshot['id'],
                    format='raw',
                    width=attributes['width'],
                    height=attributes['height']),
                                 **img_attributes)
                link = html.a(image,
                              href=formatter.req.href.screenshots(
                                  screenshot['id'],
                                  format=attributes['format']),
                              title=screenshot['description'],
                              style='border-width: %spx;' %
                              (attributes['border'], ))
                width_and_border = int(attributes['width']) + 2 * \
                  int(attributes['border'])
                description = html.span(attributes['description'],
                                        class_='description',
                                        style="width: %spx;" %
                                        (width_and_border, ))
                auxilary = html.span(link,
                                     description,
                                     class_='aux',
                                     style="width: %spx;" %
                                     (width_and_border, ))
                thumbnail_class = 'thumbnail' + (
                    (attributes['align'] == 'left') and '-left' or
                    (attributes['align'] == 'right') and '-right' or '')
                thumbnail = html.span(auxilary, class_=thumbnail_class)
                return thumbnail
            else:
                return html.a(screenshot_id,
                              href=formatter.req.href.screenshots(),
                              title=content,
                              class_='missing')

        elif name == 'ScreenshotsList':
            # Check permission.
            if not formatter.req.perm.has_permission('SCREENSHOTS_VIEW'):
                return html.div(_("No permissions to see screenshots."),
                                class_='system-message')

            # Get desired list item description
            list_item_description = content or self.default_list_item

            # Get all screenshots.
            screenshots = api.get_screenshots_complete(context)

            # Create and return HTML list of screenshots.
            list_items = []
            for screenshot in screenshots:
                list_item = self._format_description(context,
                                                     list_item_description,
                                                     screenshot)
                list_items.append(
                    html.li(
                        html.a(list_item,
                               href=formatter.req.href.screenshots(
                                   screenshot['id']))))
            return html.ul(list_items)
示例#16
0
    def render_macro(self, req, name, content):

        # Get database access.
        db = self.env.get_db_cnx()
        cursor = db.cursor()

        # Get API component.
        api = self.env[ScreenshotsApi]

        if name == 'Screenshot':
            # Check permission.
            if not req.perm.has_permission('SCREENSHOTS_VIEW'):
               return html.div('No permissions to see screenshots.',
               class_ = 'system-message')

            # Get macro arguments.
            arguments = content.split(',')

            # Get screenshot ID.
            try:
               screenshot_id = int(arguments[0])
            except:
                 raise TracError("Missing screenshot ID in macro arguments.")

            # Try to get screenshots of that ID.
            screenshot = api.get_screenshot(cursor, screenshot_id)

            # Build and return macro content.
            if screenshot:
                # Set default values of image attributes.
                attributes = {'align' : 'none',
                              'border' : '1',
                              'format' : 'raw',
                              'width' : screenshot['width'],
                              'height' : screenshot['height'],
                              'alt' : screenshot['description'],
                              'description' : self.default_description}

                # Fill attributes from macro arguments.
                for argument in arguments[1:]:
                    argument = argument.strip()
                    match = self.attributes_re.match(argument)
                    if match:
                        attributes[str(match.group(1))] = match.group(2)
                self.log.debug('attributes: %s' % (attributes,))

                # Format screenshot description from template.
                attributes['description'] = self._format_description(
                  attributes['description'], screenshot)

                # Make copy of attributes for image tag.
                img_attributes = {'align' : 'center',
                                  'style' : 'border-width: %spx;' % (
                                    attributes['border'],)}
                for attribute in attributes.keys():
                    if attribute not in ('align', 'border', 'description',
                      'format'):
                        img_attributes[attribute] = attributes[attribute]

                # Add CSS for image.
                add_stylesheet(req, 'screenshots/css/screenshots.css')

                # Build screenshot image and/or screenshot link.
                image = html.img(src = req.href.screenshots(screenshot['id'],
                  width = attributes['width'], height = attributes['height'],
                  format = 'raw'), **img_attributes)
                link = html.a(image, href = req.href.screenshots(screenshot['id'],
                  format = attributes['format']), title =
                  screenshot['description'])
                description = html.span(attributes['description'], class_ =
                  'description')
                thumbnail_class = 'thumbnail' + ((attributes['align'] == 'left')
                  and '-left' or (attributes['align'] == 'right') and '-right'
                  or '')
                thumbnail = html.span(link, ' ', description, class_ =
                  thumbnail_class, style = "width: %spx;" % (
                  int(attributes['width']) + 2 * int(attributes['border'],)))
                return thumbnail
            else:
                return html.a(screenshot_id, href = req.href.screenshots(),
                  title = content, class_ = 'missing')

        elif name == 'ScreenshotsList':
            # Check permission.
            if not req.perm.has_permission('SCREENSHOTS_VIEW'):
               return html.div('No permissions to see screenshots.',
               class_ = 'system-message')

            # Get desired list item description
            list_item_description = content or self.default_list_item

            # Get all screenshots.
            screenshots = api.get_screenshots_complete(cursor)

            # Create and return HTML list of screenshots.
            list_items = []
            for screenshot in screenshots:
                list_item = self._format_description(list_item_description,
                  screenshot)
                list_items.append(html.li(html.a(list_item, href =
                  req.href.screenshots(screenshot['id']))))
            return html.ul(list_items)