Exemple #1
0
    def test_6_all_domains(self):
        print ""
        print "get all domains"

        # Force authentication ...
        connection = frontend.login("admin", "admin", force=True)
        assert_true(frontend.authenticated)
        assert_true(frontend.token)

        connection = frontend.connect(username="******")
        assert_true(frontend.authenticated)
        assert_true(frontend.connected)

        # Get all pages
        print "get all elements at once"
        # Filter the templates ...
        for object_type in frontend.backend_available_objets:
            items = frontend.get_objects(object_type["title"], parameters=None, all_elements=False)
            print "Got %d %ss:" % (len(items), object_type)

            items = frontend.get_ui_data_model(object_type["title"])
            print "Got %d %ss:" % (len(items), object_type)
            # assert_true('_items' not in items)

        # Backend connection
        frontend.disconnect()
Exemple #2
0
    def test_6_all_pages(self):
        print ""
        print "get all elements on an endpoint"

        # Force authentication ...
        connection = frontend.login("admin", "admin", force=True)
        assert_true(frontend.authenticated)
        assert_true(frontend.token)

        connection = frontend.connect(username="******")
        assert_true(frontend.authenticated)
        assert_true(frontend.connected)

        # Get all pages
        print "get all elements at once"
        # Filter the templates ...
        parameters = {"where": '{"register":true}'}
        items = frontend.get_objects("host", parameters=parameters, all_elements=True)
        print "Got %s elements:" % len(items)
        assert_true("_items" not in items)
        # assert_true(len(items) > 0)
        for item in items:
            assert_true("host_name" in item)
            print "Host: ", item["host_name"]

        # Get all pages
        print "get all elements at once"
        # Filter the templates ...
        parameters = {"where": '{"register":true}'}
        items = frontend.get_objects("service", parameters=parameters, all_elements=True)
        print "Got %s elements:" % len(items)
        assert_true("_items" not in items)
        # assert_true(len(items) > 0)
        for item in items:
            assert_true("host_name" in item)
            assert_true("service_description" in item)
            print "Service: %s/%s" % (item["host_name"], item["service_description"])
Exemple #3
0
    def get(self, name):
        """ Call default datatables function """
        logger.debug("ElementsView, get: %s", name)

        # Update default parameters ...
        self.parameters['where'] = '{"host_name":"%s"}' % name

        resp = frontend.get_objects(self.object_type, parameters=self.parameters)
        logger.debug("ElementsView, response: %s", resp)
        if not '_items' in resp:
            # Not found
            logger.warning("HostsView, host: %s does not exist.", name)

            return render_template(
                'element.html',
                object_type=self.object_type,
                columns=None,
                object=None
            )
        else:
            fields = frontend.get_ui_data_model(self.object_type)
            if not fields:
                raise DatatableException(450, "table, trying to get table data for unmanaged data")

            # Create form dynamically
            class F(Form):
                pass

            table_columns = []
            # Objects are considered in the UI
            for field in fields:
                if field["name"] == "ui":
                    continue
                if not 'ui' in field:
                    continue
                if not 'visible' in field['ui']:
                    continue
                if not field["ui"]["visible"]:
                    continue

                # Ensuring data model is clean will avoid those tests ...
                field_default = ""
                if 'default' in field:
                    field_default = field["default"]
                field_title = field["name"]
                if 'title' in field["ui"]:
                    field_title = field["ui"]["title"]
                table_columns.append({
                    "name": field["name"],
                    "title": field_title,
                    "defaultContent": field_default,
                    "type": field["type"],
                    "format": field["ui"]["format"],
                    "orderable": field["ui"]["orderable"],
                    "searchable": field["ui"]["searchable"],
                    "data": field['name']
                })
                # Update form dynamically
                form_field = None
                valids = []
                if field["required"]:
                    valids.append(validators.InputRequired())

                if field["type"] == 'objectid':
                    logger.debug("ElementsView, field '%s' type: %s", field["name"], field["type"])
                    form_field = StringField(
                        label="Link to "+field_title,
                        description=field_title,
                        default=field_default,
                        validators=valids
                    )
                elif field["type"] == 'string':
                    logger.debug("ElementsView, field '%s' type: %s", field["name"], field["type"])
                    form_field = StringField(
                        label=field_title,
                        description=field_title,
                        default=field_default,
                        validators=valids
                    )
                elif field["type"] == 'boolean':
                    logger.debug("ElementsView, field '%s' type: %s", field["name"], field["type"])
                    form_field = BooleanField(
                        label=field_title,
                        description=field_title,
                        default=field_default,
                        validators=valids
                    )
                elif field["type"] == 'integer':
                    logger.debug("ElementsView, field '%s' type: %s", field["name"], field["type"])
                    form_field = IntegerField(
                        label=field_title,
                        description=field_title,
                        default=field_default,
                        validators=valids
                    )
                elif field["type"] == 'float':
                    logger.debug("ElementsView, field '%s' type: %s", field["name"], field["type"])
                    form_field = FloatField(
                        label=field_title,
                        description=field_title,
                        default=field_default,
                        validators=valids
                    )
                elif field["type"] == 'list':
                    logger.debug("ElementsView, field '%s' type: %s", field["name"], field["type"])
                    choices=[]
                    if "allowed" in field and field["allowed"]:
                        field["ui"]["format"] = {
                            "list_allowed": {
                                u"d": u"Down",
                                u"u": u"Up",
                                u"r": u"Recovery",
                                u"f": u"Flapping",
                                u"s": u"Downtime",
                                u"o": u"Downtime",
                                u"w": u"Warning",
                                u"n": u"None",
                                u"c": u"Critical",
                            }
                        }
                        if "list_allowed" in field["ui"]["format"]:
                            for choice in field["allowed"]:
                                choices.append( (choice, field["ui"]["format"]["list_allowed"][choice]))

                    form_field = SelectField(
                        label=field_title,
                        description=field_title,
                        default=field_default,
                        choices=choices,
                        validators=valids
                    )
                else:
                    logger.debug("ElementsView, field '%s' unspecified type", field["name"])
                    form_field = StringField(
                        label=field_title,
                        description=field_title,
                        default=field_default,
                        validators=valids
                    )

                # Fields may also be Date, DateTime, Decimal, File, Radio, Select, SelectMultiple

                setattr(
                    F,
                    field['name'],
                    form_field
                )

        # Current object
        object=resp['_items'][0]

        self.edit_form = F(request.form, obj=None, **object)
        logger.debug("ElementsView, form: %s", self.edit_form)
        logger.debug("ElementsView, form data: %s", self.edit_form.data)

        return render_template(
            '%s.html' % self.object_type,
            object_type=self.object_type,
            columns=table_columns,
            object=resp['_items'][0],
            form=self.edit_form
        )
Exemple #4
0
    def test_6_page_after_page(self):
        print ""
        print "backend connection with username/password"

        # Force authentication ...
        connection = frontend.login("admin", "admin", force=True)
        assert_true(frontend.authenticated)
        assert_true(frontend.token)

        connection = frontend.connect(username="******")
        assert_true(frontend.authenticated)
        assert_true(frontend.connected)

        # Start with first page ...
        last_page = False
        parameters = {"where": '{"register":false}', "max_results": 10, "page": 1}
        items = []
        while not last_page:
            resp = frontend.get_objects("host", parameters=parameters, all_elements=False)
            assert_true("_items" in resp)
            assert_true("_links" in resp)
            assert_true("_meta" in resp)
            print resp["_meta"]
            page_number = int(resp["_meta"]["page"])
            total = int(resp["_meta"]["total"])
            max_results = int(resp["_meta"]["max_results"])
            print "Got %d elements out of %d (page %d):" % (max_results, total, page_number)
            for item in resp["_items"]:
                assert_true("host_name" in item)
                print "Host: ", item["host_name"]

            if "next" in resp["_links"]:
                # It has pagination, so get items of all pages
                parameters["page"] = page_number + 1
                parameters["max_results"] = max_results
            else:
                last_page = True
            items.extend(resp["_items"])

        print "----------"
        print "Got %s elements:" % len(items)
        assert_true("_items" not in items)
        # assert_true(len(items) > 0)
        for item in items:
            assert_true("host_name" in item)
            print "Host: ", item["host_name"]

        # Start with first page ...
        last_page = False
        parameters = {"where": '{"register":true}', "max_results": 10, "page": 1}
        items = []
        while not last_page:
            resp = frontend.get_objects("service", parameters=parameters, all_elements=False)
            assert_true("_items" in resp)
            assert_true("_links" in resp)
            assert_true("_meta" in resp)
            print resp["_meta"]
            page_number = int(resp["_meta"]["page"])
            total = int(resp["_meta"]["total"])
            max_results = int(resp["_meta"]["max_results"])
            print "Got %d elements out of %d (page %d):" % (max_results, total, page_number)
            for item in resp["_items"]:
                assert_true("host_name" in item)
                assert_true("service_description" in item)
                print "Service: %s/%s" % (item["host_name"], item["service_description"])

            if "next" in resp["_links"]:
                # It has pagination, so get items of all pages
                parameters["page"] = page_number + 1
                parameters["max_results"] = max_results
            else:
                last_page = True
            items.extend(resp["_items"])

        print "----------"
        print "Got %s elements:" % len(items)
        assert_true("_items" not in items)
        # assert_true(len(items) > 0)
        for item in items:
            assert_true("host_name" in item)
            assert_true("service_description" in item)
            print "Service: %s/%s" % (item["host_name"], item["service_description"])
Exemple #5
0
    def table(self):
        """
        Request for elements list

        Returns an HTML page containing filtered elements for an object_type (displays host,
        service, contact, ...) formatted in a jQuery datatable.

        1/ request for data model objects definition
        2/ get main information for objects:
            - list page title,
            - ...
        3/ filter to retain only fields that are managed in the UI

        """
        logger.debug("request for %s list ...", self.object_type)

        # Data model ...
        table_columns = []
        table_links = {}
        ui_dm = {"title": "All %s (XXX items)" % self.object_type}
        fields = frontend.get_ui_data_model(self.object_type)
        if not fields:
            raise DatatableException(450, "table, trying to get table data for unmanaged data")

        # Objects are considered in the UI
        for field in fields:
            logger.debug("%s field: %s", self.object_type, field)
            if field["name"] == "ui":
                ui_dm["title"] = field["ui"]["title"]
                continue

            if 'ui' in field and 'visible' in field['ui'] and field["ui"]["visible"]:
                # Ensuring data model is clean will avoid those tests ...
                field_default = ""
                if 'default' in field:
                    field_default = field["default"]
                field_title = field["name"]
                if 'title' in field['ui']:
                    field_title = field["ui"]["title"]
                if 'schema' in field and 'data_relation' in field['schema']:
                    table_links.update({
                        field["name"]: field['schema']['data_relation']['resource']
                    })

                table_columns.append({
                    "name": field["name"],
                    "title": field_title,
                    "defaultContent": field_default,
                    "type": field['type'],
                    "format": field["ui"]["format"],
                    "orderable": field["ui"]["orderable"],
                    "searchable": field["ui"]["searchable"],
                    # "render": 'function () { console.log("Render %s"); }' % field["name"],
                    "data": field['name']
                })

        table_columns.sort(key=lambda field: field['name'])

        resp = frontend.get_objects(self.object_type)

        # Update title with number of elements
        if '_items' in resp and '%d' in ui_dm["title"]:
            ui_dm["title"] = ui_dm["title"] % len(resp['_items'])

        return render_template(
            'list.html',
            object_type=self.object_type,
            title=ui_dm["title"],
            columns=table_columns,
            links=table_links,
            list=resp['_items'] if '_items' in resp else resp
        )
Exemple #6
0
    def table_data(self):
        """
        Return elements data in json format as of Datatables SSP protocol
        More info: https://datatables.net/manual/server-side

        Example URL:
        GET /?
        draw=1&
        columns[0][data]=alias&
        columns[0][name]=&
        columns[0][searchable]=true&
        columns[0][orderable]=true&
        columns[0][search][value]=&
        columns[0][search][regex]=false&
         ...
        order[0][column]=0&
        order[0][dir]=asc&
        start=0&
        length=10&
        search[value]=&
        search[regex]=false&

        Request parameters are Json formatted


        Request Parameters:
        - draw, index parameter to be returned in the response

            Pagination:
            - start / length, for pagination

            Searching:
            - search (value or regexp)
                search[value]: Global search value. To be applied to all columns which are
                searchable
                => not implemented.
                search[regex]: true if searh[value] is a regex
                => not implemented.

            Sorting:
            - order[i][column] / order[i][dir]
                index of the columns to order and sort direction (asc/desc)

            Columns:
            - columns[i][data]: Column's data source, as defined by columns.data.
            - columns[i][name]: Column's name, as defined by columns.name.
            - columns[i][searchable]: Flag to indicate if this column is searchable (true).
            - columns[i][orderable]: Flag to indicate if this column is orderable (true).
            - columns[i][search][value]: Search value to apply to this specific column.
            - columns[i][search][regex]: Flag to indicate if the search term for this column is a
            regex.

        Response data:
        - draw

        - recordsTotal: total records, before filtering
            (i.e. total number of records in the database)

        - recordsFiltered: Total records, after filtering
            (i.e. total number of records after filtering has been applied -
            not just the number of records being returned for this page of data).
            !!! NOT YET IMPLEMENTED !!!

        - data: The data to be displayed in the table.
            an array of data source objects, one for each row, which will be used by DataTables.

        - error (optional): Error message if an error occurs
            Not included if there is no error.
        """
        logger.info("request for backend data ...")

        # Manage request parameters ...
        # Because of specific datatables parameters name (eg. columns[0] ...)
        # ... some parameters have been json.stringify on client side !
        params = {}
        for key in request.args.keys():
            if key == 'columns' or key == 'order' or key == 'search':
                params[key] = json.loads(request.args[key])
            else:
                params[key] = request.args[key]
        logger.debug("backend request parameters: %s", params)
        # params now contains 'valid' query parameters as Bottle should have found them ...

        parameters = {}
        # Manage page length ...
        parameters['max_results'] = int(params.get('length', 25))

        # Manage page number ...
        parameters['page'] = int(params.get('start', 0))

        # Columns ordering
        # order:[{"column":2,"dir":"desc"}]
        if 'order' in params and params['order']:
            sorted_columns = []
            for order in params['order']:
                idx = int(order['column'])
                logger.info(
                    "sort by column %d (%s), order: %s ",
                    idx, params['columns'][idx]['data'], order['dir']
                )
                if order['dir'] == 'desc':
                    sorted_columns.append('-' + params['columns'][idx]['data'])
                else:
                    sorted_columns.append(params['columns'][idx]['data'])
            if sorted_columns:
                parameters['sort'] = ','.join(sorted_columns)

        # Columns filtering
        # earch:{"value":"test","regex":false}
        if 'search' in params and params['search'] and params['search']['value']:
            logger.info("search all searchable columns containing '%s'", params['search']['value'])
            searched_columns = []
            for column in params['columns']:
                if 'searchable' in column and column['searchable']:
                    searched_columns.append(
                        '{ "%s": { "$regex": ".*%s.*" } }' % (
                            column['data'], params['search']['value']
                        )
                    )
            if searched_columns:
                parameters['where'] = '{"$or": [' + ','.join(searched_columns) + '] }'

        # Request objects from the backend ...
        logger.info("backend parameters: %s", parameters)
        resp = frontend.get_objects(self.object_type, parameters=parameters)
        # page_number = int(resp['_meta']['page'])
        total = 0
        if '_meta' in resp:
            total = int(resp['_meta']['total'])
        # max_results = int(resp['_meta']['max_results'])
        # recordsFiltered = 0
        # if total > max_results:
        # recordsFiltered =

        # Prepare response
        draw = 0
        if request.args.get('draw'):
            draw = int(request.args.get('draw'))
        rsp = {
            "draw": draw,
            "recordsTotal": total,
            "recordsFiltered": total,
            "data": resp['_items'] if '_items' in resp else resp
        }
        # response.content_type = 'application/json'
        logger.info("response: %s", rsp)
        return json.dumps(rsp)