Ejemplo n.º 1
0
    def paginate(self, request, query, parameters):
        '''Refine ``query`` from input ``parameters`` and perform
        pagination.

        Return a pagination info object
        '''
        config = request.app.config
        columns = self.columns(request, query.model)
        #
        search = parameters.pop(config['QUERY_SEARCH_KEY'], None)
        pretty = parameters.pop('pretty', False)
        if search:
            query = query.search(search)
        # pick up the fields
        field_key = config['QUERY_FIELD_KEY']
        if field_key not in parameters:
            field_key = '%s[]' % field_key
        fields = parameters.pop(field_key, None)
        if fields and not isinstance(fields, list):
            fields = [fields]
        if fields:
            cd = dict(((c.code, c) for c in columns))
            columns = []
            load_only = set()
            for field in fields:
                column = cd.get(field)
                if column:
                    load_only.update(column.fields)
                else:
                    column = Column.get(field)
                columns.append(column)
            if self.required_fields:
                load_only.update(self.required_fields)
            load_only = tuple(load_only)
            query = query.load_only(*load_only)

        query = self.safe_filter(query, parameters)
        #
        total_size = yield query.count()
        start = safe_int(parameters.get(config['QUERY_START_KEY'], 0))
        length = safe_int(parameters.get(config['QUERY_LENGTH_KEY']))
        max_length = config['QUERY_MAX_LENGTH']

        if start or length:
            if not length:
                end = -1
            else:
                end = start + length
            data = yield query[start:end]
        else:
            length = total_size
            data = yield query.all()
        info = self.pag_info(data, start, length, total_size, columns, pretty)
        coroutine_return(info)
Ejemplo n.º 2
0
 def _setup(self):
     # Override _setup
     meta = self._router._meta
     if meta:
         columns = self._columns
         if not columns:
             new_columns = [self.column(meta.pk)]
             for field in meta.scalarfields:
                 new_columns.append(self.column(field))
         else:
             new_columns = []
             for col in columns:
                 if not isinstance(col, Column):
                     if col in meta.dfields:
                         col = self.column(meta.dfields[col])
                     else:
                         col = Column.get(col)
                 new_columns.append(col)
         self._columns = new_columns
Ejemplo n.º 3
0
 def column(self, field):
     return Column.get(field.attname, type=get_field_type(field))