Exemplo n.º 1
0
    def _get_pagination_query(session, query, pagination, api_model, model):
        if not pagination.get('sort'):
            pagination['sort'] = api_model.DEFAULT_SORT
        marker = None
        if pagination.get('marker'):
            key_attr = getattr(model, api_model.PRIMARY_KEY)
            marker_query = copy.copy(query)
            marker_query = marker_query.filter(
                key_attr == pagination['marker'])
            try:
                marker = marker_query.limit(1).one()
            except exc.NoResultFound:
                raise storage.InvalidMarker(
                    'Marker %s not found.' % pagination['marker'])
        limit = pagination.get('limit')
        # we sort by "severity" by its semantic than its alphabetical
        # order when "severity" specified in sorts.
        for sort_key, sort_dir in pagination['sort'][::-1]:
            if sort_key == 'severity':
                engine = session.connection()
                if engine.dialect.name != "mysql":
                    raise aodh.NotImplementedError
                sort_dir_func = {'asc': asc, 'desc': desc}[sort_dir]
                query = query.order_by(sort_dir_func(
                    func.field(getattr(model, sort_key), 'low',
                               'moderate', 'critical')))
                pagination['sort'].remove((sort_key, sort_dir))

        sort_keys = [s[0] for s in pagination['sort']]
        sort_dirs = [s[1] for s in pagination['sort']]
        return oslo_sql_utils.paginate_query(
            query, model, limit, sort_keys, sort_dirs=sort_dirs, marker=marker)
Exemplo n.º 2
0
 def _get_pagination_query(query, pagination, api_model, model):
     if not pagination.get('sort'):
         pagination['sort'] = api_model.DEFAULT_SORT
     marker = None
     if pagination.get('marker'):
         key_attr = getattr(model, api_model.PRIMARY_KEY)
         marker_query = copy.copy(query)
         marker_query = marker_query.filter(
             key_attr == pagination['marker'])
         try:
             marker = marker_query.limit(1).one()
         except exc.NoResultFound:
             raise storage.InvalidMarker('Marker %s not found.' %
                                         pagination['marker'])
     limit = pagination.get('limit')
     sort_keys = [s[0] for s in pagination['sort']]
     sort_dirs = [s[1] for s in pagination['sort']]
     return oslo_sql_utils.paginate_query(query,
                                          model,
                                          limit,
                                          sort_keys,
                                          sort_dirs=sort_dirs,
                                          marker=marker)