Exemplo n.º 1
0
    def test_build_mongoengine_query(self):
        query = live_query.LiveQuery(
            fields=['host_name', 'last_check'],
            filters=json.dumps({
                "isnot": {
                    "state": ["0", "1"],
                    "host_state": ["2"]
                },
                "is": {
                    "event_type": ["ALERT"]
                },
                "defined": {
                    "name": True
                }
            }),
            paging=paging.Paging(
                page=3,
                size=100
            )

        )

        fields, query, skip, limit = mongoengine_query.build_mongoengine_query(
            query,
            host.Host
        )

        self.assertEqual(
            fields,
            ['host_name', 'last_check']
        )

        self.assertEqual(skip, 300)
        self.assertEqual(limit, 400)
Exemplo n.º 2
0
    def get_all(self, lq={}):
        """Return all resources."""

        fields, query, skip, limit = mongoengine_query.build_mongoengine_query(
            lq, self.resource_storage)

        if skip is not None and limit is not None:
            objects = (self.resource_storage.objects.filter(query).only(
                *fields).skip(skip).limit(limit))
        else:
            objects = self.resource_storage.objects.filter(query).only(*fields)

        resp = [self.resource_datamodel(**self._get_dict(r)) for r in objects]

        # Mongoengine's 'only()' does not seem to work :(
        filtered_response = []
        if fields:
            for obj in resp:
                obj_with_field = {}
                for field in fields:
                    obj_with_field[field] = getattr(obj, field)
                filtered_response.append(
                    self.resource_datamodel(**obj_with_field))

        return filtered_response or resp
    def get_all(self, lq={}):
        """Return all resources."""

        fields, query, skip, limit = mongoengine_query.build_mongoengine_query(
            lq, self.resource_storage
        )

        if skip is not None and limit is not None:
            objects = (
                self.resource_storage.objects
                .filter(query)
                .only(*fields)
                .skip(skip)
                .limit(limit)
            )
        else:
            objects = self.resource_storage.objects.filter(query).only(*fields)

        resp = [
            self.resource_datamodel(**self._get_dict(r))
            for r
            in objects
        ]

        # Mongoengine's 'only()' does not seem to work :(
        filtered_response = []
        if fields:
            for obj in resp:
                obj_with_field = {}
                for field in fields:
                    obj_with_field[field] = getattr(obj, field)
                filtered_response.append(
                    self.resource_datamodel(**obj_with_field)
                )

        return filtered_response or resp