Пример #1
0
def test_query_json():
    q1 = qj.compile(
        {"$and": [{
            "field1": {
                "$gt": 10
            }
        }, {
            "field2": {
                "$eq": "hello"
            }
        }]})
    q2 = Gt("field1", 10) & Eq("field2", "hello")
    assert qtree(q1) == qtree(q2)

    q1 = qj.compile({
        "$not": {
            "$or": [{
                "field1": {
                    "$range": [1, 15]
                }
            }, {
                "field2": {
                    "$text": "hello"
                }
            }]
        }
    })
    q2 = Not(InRange("field1", 1, 15) | Contains("field2", "hello"))
    assert qtree(q1) == qtree(q2)
Пример #2
0
def test_query_json():
    q1 = qj.compile({"$and": [{"field1": {"$gt": 10}}, {"field2": {"$eq": "hello"}}]})
    q2 = Gt("field1", 10) & Eq("field2", "hello")
    assert qtree(q1) == qtree(q2)

    q1 = qj.compile({"$not": {"$or": [{"field1": {"$range": [1, 15]}}, {"field2": {"$text": "hello"}}]}})
    q2 = Not(InRange("field1", 1, 15) | Contains("field2", "hello"))
    assert qtree(q1) == qtree(q2)
Пример #3
0
def remove(table_name):
    db = dbs[session["username"]]
    model = getattr(models, table_name)

    if request.method == "GET":
        req = request.args
    elif request.method == "POST":
        req = request.form
    else:
        return jsonify(ok=0)

    criteria = req.get("criteria")
    ids = req.get("_id")
    if criteria:
        criteria = json.loads(req.get("criteria"))
        if isinstance(criteria, dict) and (len(criteria) == 1) and "_id" in criteria:
            ids = [c["$oid"] for c in criteria["_id"]]
        else:
            ids = None
            criteria = optimize(qj.compile(criteria))
        result = db[model].query(criteria)
    elif ids:
        ids = json.loads(ids)
        result = db[model][ids]
    else:
        return jsonify(ok=0)

    try:
        with transaction.manager:
            count = db.remove(result)
    except Exception as e:
        return jsonify(ok=0, message=str(e), error_type=e.__class__.__name__)

    return jsonify(ok=1, count=count)
Пример #4
0
def find(model_name):
    """Queries the given model.

    Request body should be a JSON object with a 'criteria' member in the
    ZeroDB query format. Optional 'skip', 'limit' and 'sort' members can be
    provided.
    """
    db = session_db_or_403()
    model = model_or_404(model_name)

    query = request.get_json()
    criteria = optimize(query_json.compile(query['criteria']))

    skip = query.get('skip')
    if skip:
        skip = int(skip)

    limit = query.get('limit')
    if limit:
        limit = int(limit)

    sort = query.get('sort')
    if isinstance(sort, six.string_types):
        if sort.startswith('-'):
            sort_index = sort[1:].strip()
            reverse = True
        else:
            sort_index = sort
            reverse = None
    elif isinstance(sort, dict):
        assert len(sort) == 1
        sort_index, direction = sort.popitem()
        reverse = direction >= 0
    else:
        sort_index = None
        reverse = None

    result = db[model].query(criteria, skip=skip, limit=limit,
                             sort_index=sort_index, reverse=reverse)
    objs = [pickler.flatten(o) for o in result]

    return jsonify(objects=objs, count=len(objs))
Пример #5
0
def find(table_name):
    db = dbs[session["username"]]
    model = getattr(models, table_name)

    if request.method == "GET":
        req = request.args
    elif request.method == "POST":
        req = request.form
    else:
        return jsonify(ok=0)

    if req.get("criteria") is None:
        criteria = json.loads(request.data).get("criteria")
    else:
        criteria = json.loads(req.get("criteria"))

    if isinstance(criteria, dict) and (len(criteria)
                                       == 1) and "_id" in criteria:
        ids = [c["$oid"] for c in criteria["_id"]]
    else:
        ids = None
        criteria = optimize(qj.compile(criteria))

    skip = req.get("skip")
    if skip:
        skip = int(skip)

    limit = req.get("limit")
    if limit:
        limit = int(limit)

    sort = req.get("sort")
    if sort:
        try:
            sort = json.loads(sort)
        except ValueError:
            if sort.startswith("-"):
                sort_index = sort[1:].strip()
                reverse = True
            else:
                sort_index = sort
                reverse = None
        if isinstance(sort, dict):
            assert len(sort) == 1  # Only one field at the moment
            sort_index, direction = sort.popitem()
            reverse = (direction >= 0)
        elif isinstance(sort, list):
            sort_index = sort[0]
            reverse = None
    else:
        sort_index = None
        reverse = None

    if ids:
        skip = skip or 0
        end = skip + limit if limit else None
        ids = ids[skip:end]
        result = db[model][ids]
    else:
        result = db[model].query(criteria,
                                 skip=skip,
                                 limit=limit,
                                 sort_index=sort_index,
                                 reverse=reverse)

    return jsonpickle.encode(result, unpicklable=False)
Пример #6
0
def find(table_name):
    db = dbs[session["username"]]
    model = getattr(models, table_name)

    if request.method == "GET":
        req = request.args
    elif request.method == "POST":
        req = request.form
    else:
        return jsonify(ok=0)

    if req.get("criteria") is None:
        criteria = json.loads(request.data).get("criteria")
    else:
        criteria = json.loads(req.get("criteria"))

    if isinstance(criteria, dict) and (len(criteria) == 1) and "_id" in criteria:
        ids = [c["$oid"] for c in criteria["_id"]]
    else:
        ids = None
        criteria = optimize(qj.compile(criteria))

    skip = req.get("skip")
    if skip:
        skip = int(skip)

    limit = req.get("limit")
    if limit:
        limit = int(limit)

    sort = req.get("sort")
    if sort:
        try:
            sort = json.loads(sort)
        except ValueError:
            if sort.startswith("-"):
                sort_index = sort[1:].strip()
                reverse = True
            else:
                sort_index = sort
                reverse = None
        if isinstance(sort, dict):
            assert len(sort) == 1  # Only one field at the moment
            sort_index, direction = sort.popitem()
            reverse = (direction >= 0)
        elif isinstance(sort, list):
            sort_index = sort[0]
            reverse = None
    else:
        sort_index = None
        reverse = None

    if ids:
        skip = skip or 0
        end = skip + limit if limit else None
        ids = ids[skip:end]
        result = db[model][ids]
    else:
        result = db[model].query(criteria, skip=skip, limit=limit, sort_index=sort_index, reverse=reverse)

    return jsonpickle.encode(list(result), unpicklable=False)