예제 #1
0
def _get_collection(model, insecure=False, limit=None, marker=None,
                    sort_keys=None, sort_dirs=None, fields=None, **filters):
    columns = (
        tuple([getattr(model, f) for f in fields if hasattr(model, f)])
        if fields else ()
    )

    query = (b.model_query(model, *columns) if insecure
             else _secure_query(model, *columns))
    query = db_filters.apply_filters(query, model, **filters)

    query = _paginate_query(
        model,
        limit,
        marker,
        sort_keys,
        sort_dirs,
        query
    )

    try:
        return query.all()
    except Exception as e:
        raise exc.DBQueryEntryError(
            "Failed when querying database, error type: %s, "
            "error message: %s" % (e.__class__.__name__, str(e))
        )
예제 #2
0
def _get_collection(model,
                    insecure=False,
                    limit=None,
                    marker=None,
                    sort_keys=None,
                    sort_dirs=None,
                    fields=None,
                    **kwargs):
    columns = (tuple([getattr(model, f) for f in fields
                      if hasattr(model, f)]) if fields else ())

    query = (b.model_query(model, *columns) if insecure else _secure_query(
        model, *columns))
    query = query.filter_by(**kwargs)

    tags = kwargs.pop('tags', None)

    # To match the tag list, a resource must contain at least all of the
    # tags present in the filter parameter.
    if tags:
        tag_attr = getattr(model, 'tags')

        if len(tags) == 1:
            expr = tag_attr.contains(tags)
        else:
            expr = sa.and_(*[tag_attr.contains(tag) for tag in tags])

        query = query.filter(expr)

    query = _paginate_query(model, limit, marker, sort_keys, sort_dirs, query)

    try:
        return query.all()
    except Exception as e:
        raise exc.DBQueryEntryError(
            "Failed when querying database, error type: %s, "
            "error message: %s" % (e.__class__.__name__, str(e)))