Ejemplo n.º 1
0
def load_view_into_dashlet(dashlet, nr, view_name, add_context=None, load_from_all_views=False):
    import views

    views.load_views()

    permitted_views = views.permitted_views()

    # it is random which user is first accessing
    # an apache python process, initializing the dashboard loading and conversion of
    # old dashboards. In case of the conversion we really try hard to make the conversion
    # work in all cases. So we need all views instead of the views of the user.
    if load_from_all_views and view_name not in permitted_views:
        # This is not really 100% correct according to the logic of visuals.available(),
        # but we do this for the rare edge case during legacy dashboard conversion, so
        # this should be sufficient
        view = None
        for (u, n), this_view in views.all_views().items():
            # take the first view with a matching name
            if view_name == n:
                view = this_view
                break

        if not view:
            raise MKGeneralException(
                _(
                    "Failed to convert a builtin dashboard which is referencing "
                    'the view "%s". You will have to migrate it to the new '
                    "dashboard format on your own to work properly." % view_name
                )
            )
    else:
        view = permitted_views[view_name]

    view = copy.deepcopy(view)  # Clone the view
    dashlet.update(view)
    if add_context:
        dashlet["context"].update(add_context)

    # Overwrite the views default title with the context specific title
    dashlet["title"] = visuals.visual_title("view", view)
    dashlet["title_url"] = html.makeuri_contextless(
        [("view_name", view_name)] + visuals.get_singlecontext_vars(view).items(), filename="view.py"
    )

    dashlet["type"] = "view"
    dashlet["name"] = "dashlet_%d" % nr
    dashlet["show_title"] = True
Ejemplo n.º 2
0
def load_view_into_dashlet(dashlet, nr, view_name, add_context=None):
    import views
    views.load_views()
    views = views.permitted_views()
    if view_name in views:
        view = copy.deepcopy(views[view_name])
        dashlet.update(view)
        if add_context:
            dashlet['context'].update(add_context)

        # Overwrite the views default title with the context specific title
        dashlet['title'] = visuals.visual_title('view', view)
        dashlet['title_url'] = html.makeuri_contextless(
                [('view_name', view_name)] + visuals.get_singlecontext_vars(view).items(),
                filename='view.py')

    dashlet['type']       = 'view'
    dashlet['name']       = 'dashlet_%d' % nr
    dashlet['show_title'] = True
Ejemplo n.º 3
0
def render_dashboard(name):
    mode = "display"
    if html.var("edit") == "1":
        mode = "edit"

    if mode == "edit" and not config.may("general.edit_dashboards"):
        raise MKAuthException(_("You are not allowed to edit dashboards."))

    board = load_dashboard_with_cloning(name, edit=mode == "edit")

    # The dashboard may be called with "wato_folder" set. In that case
    # the dashboard is assumed to restrict the shown data to a specific
    # WATO subfolder or file. This could be a configurable feature in
    # future, but currently we assume, that *all* dashboards are filename
    # sensitive.

    wato_folder = html.var("wato_folder")

    title = visuals.visual_title("dashboard", board)

    # Distance from top of the screen to the lower border of the heading
    header_height = 55

    # The title of the dashboard needs to be prefixed with the WATO path,
    # in order to make it clear to the user, that he is seeing only partial
    # data.
    if not board.get("show_title"):
        # Remove the whole header line
        html.set_render_headfoot(False)
        header_height = 0

    elif wato_folder is not None:
        title = wato.get_folder_title(wato_folder) + " - " + title

    html.header(title, javascripts=["dashboard"], stylesheets=["pages", "dashboard", "status", "views"])

    html.write('<div id=dashboard class="dashboard_%s">\n' % name)  # Container of all dashlets

    used_types = list(set([d["type"] for d in board["dashlets"]]))

    # Render dashlet custom scripts
    scripts = "\n".join([dashlet_types[ty]["script"] for ty in used_types if dashlet_types[ty].get("script")])
    if scripts:
        html.javascript(scripts)

    # Render dashlet custom styles
    styles = "\n".join([dashlet_types[ty]["styles"] for ty in used_types if dashlet_types[ty].get("styles")])
    if styles:
        html.write("<style>\n%s\n</style>\n" % styles)

    refresh_dashlets = []  # Dashlets with automatic refresh, for Javascript
    dashlets_js = []
    on_resize = []  # javascript function to execute after ressizing the dashlet
    for nr, dashlet in enumerate(board["dashlets"]):
        # dashlets using the 'urlfunc' method will dynamically compute
        # an url (using HTML context variables at their wish).
        if "urlfunc" in dashlet:
            urlfunc = dashlet["urlfunc"]
            # We need to support function pointers to be compatible to old dashboard plugin
            # based definitions. The new dashboards use strings to reference functions within
            # the global context or functions of a module. An example would be:
            #
            # urlfunc: "my_custom_url_rendering_function"
            #
            # or within a module:
            #
            # urlfunc: "my_module.render_my_url"
            if type(urlfunc) == type(lambda x: x):
                dashlet["url"] = urlfunc()
            else:
                if "." in urlfunc:
                    module_name, func_name = urlfunc.split(".", 1)
                    module = __import__(module_name)
                    fp = module.__dict__[func_name]
                else:
                    fp = globals()[urlfunc]
                dashlet["url"] = fp()

        dashlet_type = dashlet_types[dashlet["type"]]

        # dashlets using the 'url' method will be refreshed by us. Those
        # dashlets using static content (such as an iframe) will not be
        # refreshed by us but need to do that themselves.
        if "url" in dashlet or ("render" in dashlet_type and dashlet_type.get("refresh")):
            url = dashlet.get("url", "dashboard_dashlet.py?name=" + name + "&id=" + str(nr))
            refresh = dashlet.get("refresh", dashlet_type.get("refresh"))
            if refresh:
                action = None
                if "on_refresh" in dashlet_type:
                    try:
                        action = "function() {%s}" % dashlet_type["on_refresh"](nr, dashlet)
                    except Exception, e:
                        # Ignore the exceptions in non debug mode, assuming the exception also occures
                        # while dashlet rendering, which is then shown in the dashlet itselfs.
                        if config.debug:
                            raise
                else:
                    # FIXME: remove add_wato_folder_to_url
                    action = '"%s"' % add_wato_folder_to_url(url, wato_folder)  # url to dashboard_dashlet.py

                if action:
                    refresh_dashlets.append("[%d, %d, %s]" % (nr, refresh, action))

        # Update the dashlets context with the dashboard global context when there are
        # useful information
        add_url_vars = apply_global_context(board, dashlet)

        # Paint the dashlet's HTML code
        render_dashlet(name, board, nr, dashlet, wato_folder, add_url_vars)

        if "on_resize" in dashlet_type:
            try:
                on_resize.append("%d: function() {%s}" % (nr, dashlet_type["on_resize"](nr, dashlet)))
            except Exception, e:
                # Ignore the exceptions in non debug mode, assuming the exception also occures
                # while dashlet rendering, which is then shown in the dashlet itselfs.
                if config.debug:
                    raise
Ejemplo n.º 4
0
def render_dashboard(name):
    mode = 'display'
    if html.var('edit') == '1':
        mode = 'edit'

    if mode == 'edit' and not config.may("general.edit_dashboards"):
        raise MKAuthException(_("You are not allowed to edit dashboards."))

    board = load_dashboard_with_cloning(name, edit = mode == 'edit')

    # The dashboard may be called with "wato_folder" set. In that case
    # the dashboard is assumed to restrict the shown data to a specific
    # WATO subfolder or file. This could be a configurable feature in
    # future, but currently we assume, that *all* dashboards are filename
    # sensitive.

    wato_folder = html.var("wato_folder")

    title = visuals.visual_title('dashboard', board)

    # Distance from top of the screen to the lower border of the heading
    header_height = 55

    # The title of the dashboard needs to be prefixed with the WATO path,
    # in order to make it clear to the user, that he is seeing only partial
    # data.
    if not board.get('show_title'):
        # Remove the whole header line
        html.set_render_headfoot(False)
        header_height = 0

    elif wato_folder is not None:
        title = wato.get_folder_title(wato_folder) + " - " + title

    html.header(title, javascripts=["dashboard"], stylesheets=["pages", "dashboard", "status", "views"])

    html.write("<div id=dashboard class=\"dashboard_%s\">\n" % name) # Container of all dashlets

    used_types = list(set([ d['type'] for d in board['dashlets'] ]))

    # Render dashlet custom scripts
    scripts = '\n'.join([ dashlet_types[ty]['script'] for ty in used_types if dashlet_types[ty].get('script') ])
    if scripts:
        html.javascript(scripts)

    # Render dashlet custom styles
    styles = '\n'.join([ dashlet_types[ty]['styles'] for ty in used_types if dashlet_types[ty].get('styles') ])
    if styles:
        html.write("<style>\n%s\n</style>\n" % styles)

    refresh_dashlets = [] # Dashlets with automatic refresh, for Javascript
    dashlets_js      = []
    on_resize        = [] # javascript function to execute after ressizing the dashlet
    for nr, dashlet in enumerate(board["dashlets"]):
        # dashlets using the 'urlfunc' method will dynamically compute
        # an url (using HTML context variables at their wish).
        if "urlfunc" in dashlet:
            urlfunc = dashlet['urlfunc']
            # We need to support function pointers to be compatible to old dashboard plugin
            # based definitions. The new dashboards use strings to reference functions within
            # the global context or functions of a module. An example would be:
            #
            # urlfunc: "my_custom_url_rendering_function"
            #
            # or within a module:
            #
            # urlfunc: "my_module.render_my_url"
            if type(urlfunc) == type(lambda x: x):
                dashlet["url"] = urlfunc()
            else:
                if '.' in urlfunc:
                    module_name, func_name = urlfunc.split('.', 1)
                    module = __import__(module_name)
                    fp = module.__dict__[func_name]
                else:
                    fp = globals()[urlfunc]
                dashlet["url"] = fp()

        dashlet_type = dashlet_types[dashlet['type']]

        # dashlets using the 'url' method will be refreshed by us. Those
        # dashlets using static content (such as an iframe) will not be
        # refreshed by us but need to do that themselves.
        if "url" in dashlet or ('render' in dashlet_type and dashlet_type.get('refresh')):
            url = dashlet.get("url", "dashboard_dashlet.py?name="+name+"&id="+ str(nr))
            refresh = dashlet.get("refresh", dashlet_type.get("refresh"))
            if refresh:
                action = None
                if 'on_refresh' in dashlet_type:
                    try:
                        action = 'function() {%s}' % dashlet_type['on_refresh'](nr, dashlet)
                    except Exception, e:
                        # Ignore the exceptions in non debug mode, assuming the exception also occures
                        # while dashlet rendering, which is then shown in the dashlet itselfs.
                        if config.debug:
                            raise
                else:
                    # FIXME: remove add_wato_folder_to_url
                    action = '"%s"' % add_wato_folder_to_url(url, wato_folder) # url to dashboard_dashlet.py

                if action:
                    refresh_dashlets.append('[%d, %d, %s]' % (nr, refresh, action))


        # Update the dashlets context with the dashboard global context when there are
        # useful information
        add_url_vars = apply_global_context(board, dashlet)

        # Paint the dashlet's HTML code
        render_dashlet(name, board, nr, dashlet, wato_folder, add_url_vars)

        if 'on_resize' in dashlet_type:
            try:
                on_resize.append('%d: function() {%s}' % (nr, dashlet_type['on_resize'](nr, dashlet)))
            except Exception, e:
                # Ignore the exceptions in non debug mode, assuming the exception also occures
                # while dashlet rendering, which is then shown in the dashlet itselfs.
                if config.debug:
                    raise