Exemplo n.º 1
0
    def get_list(self, page, sort_column, sort_desc, search, filters,
                 execute=True):
        
        query = {u'model_form_id':self.model_form_id}

        # Filters
        if self._filters:
            data = []

            for flt, value in filters:
                f = self._filters[flt]
                data = f.apply(data, value)

            if data:
                if len(data) == 1:
                    query = data[0]
                else:
                    query['$AND'] = data

        # Search
        if self._search_supported and search:
            values = search.split(' ')

            queries = []

            # Construct inner querie
            for value in values:
                if not value:
                    continue

                regex = parse_like_term(value)

                stmt = []
                for field in self._search_fields:
                    stmt.append({field: {'$regex': regex}})

                if stmt:
                    if len(stmt) == 1:
                        queries.append(stmt[0])
                    else:
                        queries.append({'$or': stmt})

            # Construct final query
            if queries:
                if len(queries) == 1:
                    final = queries[0]
                else:
                    final = {'$and': queries}

                if query:
                    query = {'$and': [query, final]}
                else:
                    query = final

        # Get count
        
        count = db.model_form_values.find(query).count()

        # Sorting
        sort_by = None

        if sort_column:
            sort_by = [(sort_column, pymongo.DESCENDING if sort_desc else pymongo.ASCENDING)]

        # Pagination
        skip = None

        if page is not None:
            skip = page * self.page_size

        results = db.model_form_values.find(query, sort=sort_by, skip=skip, limit=self.page_size)

        if execute:
            results = list(results)

        return count, results
Exemplo n.º 2
0
    def get_list(self, page, sort_column, sort_desc, search, filters,
                 execute=True):
        """
            Get list of objects from MongoEngine

            :param page:
                Page number
            :param sort_column:
                Sort column
            :param sort_desc:
                Sort descending
            :param search:
                Search criteria
            :param filters:
                List of applied fiters
            :param execute:
                Run query immediately or not
        """
        query = {}

        # Filters
        if self._filters:
            data = []

            for flt, value in filters:
                f = self._filters[flt]
                data = f.apply(data, value)

            if data:
                if len(data) == 1:
                    query = data[0]
                else:
                    query['$AND'] = data

        # Search
        if self._search_supported and search:
            values = search.split(' ')

            queries = []

            # Construct inner querie
            for value in values:
                if not value:
                    continue

                regex = parse_like_term(value)

                stmt = []
                for field in self._search_fields:
                    stmt.append({field: {'$regex': regex}})

                if stmt:
                    if len(stmt) == 1:
                        queries.append(stmt[0])
                    else:
                        queries.append({'$or': stmt})

            # Construct final query
            if queries:
                if len(queries) == 1:
                    final = queries[0]
                else:
                    final = {'$and': queries}

                if query:
                    query = {'$and': [query, final]}
                else:
                    query = final

        # Get count
        count = self.coll.find(query).count()

        # Sorting
        sort_by = None

        if sort_column:
            sort_by = [(sort_column, pymongo.DESCENDING if sort_desc else pymongo.ASCENDING)]

        # Pagination
        skip = None

        if page is not None:
            skip = page * self.page_size

        results = self.coll.find(query, sort=sort_by, skip=skip, limit=self.page_size)

        if execute:
            results = list(results)

        return count, results