示例#1
0
def get_data_for_sending(query: ModelSelect, require_where=False):
    page, limit, where_count = 1, Config.DEFAULT_API_LIST_LIMIT, 0
    for arg_name, arg_value in request.args.items():
        if len(arg_value) == 0:
            continue
        if arg_name in ("order_by", "order_by_desc"):
            fields = get_fields_by_names(query.model, [arg_value])
            if len(fields) == 0:
                abort(400)
            field = fields[0] if "order_by" == arg_name else fields[0].desc()
            query = query.order_by(field)
        elif arg_name == "page":
            page = int(arg_value)
        elif arg_name == "limit":
            limit = int(arg_value)
            limit = limit if limit <= Config.MAX_API_LIST_LIMIT else Config.MAX_API_LIST_LIMIT
        elif arg_name.startswith("where:") or arg_name.startswith("or_where:"):
            operations = arg_name.split(":")
            is_or_where = operations[0] == "or_where"
            if len(operations) == 2:
                operator = "equal"
            else:
                operator = operations[1]
            fields = get_fields_by_names(query.model, [operations[-1]])
            if len(fields) == 0:
                abort(400)
            else:
                field = fields[0]
            expression = get_expression_by_operator(field, arg_value, operator)
            if expression is None:
                abort(400)
            if is_or_where:
                query = query.orwhere(expression)
            else:
                query = query.where(expression)
            where_count += 1
    if require_where and where_count == 0:
        abort(400)
    count = query.count()
    if count == 0:
        abort(404)
    pages_count = math.ceil(count / limit)
    data = [get_model_dict(sm) for sm in query.paginate(page, limit)]
    return {
        "data": data,
        "page": page,
        "pages": pages_count,
        "count": count,
        "limit": limit
    }
示例#2
0
def _create_access_type(query: peewee.ModelSelect):
    if query.count() == 0:
        return AccessType.READ_WRITE
    else:
        return AccessType(query[0].access_type)