Beispiel #1
0
def deleteData():
    if enc_key is None:
        abort(403, "Cannot store data without a key.\n")
    database = request.json['database']
    collection = request.json['collection']
    # query is the filter
    query = request.json['query']
    convert_string_to_objectid(query)
    # Indices is a json dict mapping keys to [data_type, is_sparse]
    # Each index is of the form itemA.itemB.....itemZ,
    indices = request.json['indices']
    is_many = request.json['is_many']
    # Get the database
    db = get_collection(database, collection, indices)
    collation = request.json['collation']
    if is_many:
        result = db.delete_many(query, collation)
    else:
        result = db.delete_one(query, collation)
    result_dict = dict()
    result_dict['acknowledged'] = result.acknowledged
    result_dict['deleted_count'] = result.deleted_count
    result_dict['raw_result'] = result.raw_result
    convert_objectid_to_string(result_dict)
    return result_dict
Beispiel #2
0
def replaceOneData():
    if enc_key is None:
        abort(403, "Cannot store data without a key.\n")
    database = request.json['database']
    collection = request.json['collection']
    # query is the filter
    query = request.json['query']
    convert_string_to_objectid(query)
    # Data is the data transferred
    data = request.json['data']
    convert_string_to_objectid(data)
    # Indices is a json dict mapping keys to [data_type, is_sparse]
    # Each index is of the form itemA.itemB.....itemZ,
    indices = request.json['indices']
    upsert = request.json['upsert']
    bypass_document_validation = request.json['bypass_document_validation']
    collation = request.json['collation']
    # Get the database
    db = get_collection(database, collection, indices)
    result = db.replace_one(
        query,
        data,
        upsert=upsert,
        bypass_document_validation=bypass_document_validation,
        collation=collation)

    result_dict = dict()
    result_dict['acknowledged'] = result.acknowledged
    result_dict['matched_count'] = result.matched_count
    result_dict['modified_count'] = result.modified_count
    result_dict['raw_result'] = result.raw_result
    result_dict['upserted_id'] = result.upserted_id
    convert_objectid_to_string(result_dict)
    return result_dict
Beispiel #3
0
def insertDepricatedData():
    if enc_key is None:
        abort(403, "Cannot store data without a key.\n")
    database = request.json['database']
    collection = request.json['collection']
    # Indices is a json dict mapping keys to [data_type, is_sparse]
    # Each index is of the form itemA.itemB.....itemZ,
    indices = request.json['indices']
    # Get the database
    db = get_collection(database, collection, indices)

    # Get fields
    doc_or_docs = request.json['doc_or_docs']
    convert_string_to_objectid(doc_or_docs)
    manipulate = request.json['manipulate']
    check_keys = request.json['check_keys']
    continue_on_error = request.json['continue_on_error']
    # kwargs
    kwargs_dict = dict()
    if 'w' in request.json:
        kwargs_dict['w'] = request.json['w']
    if 'wtimeout' in request.json:
        kwargs_dict['wtimeout'] = request.json['wtimeout']
    if 'j' in request.json:
        kwargs_dict['j'] = request.json['j']
    if 'fsync' in request.json:
        kwargs_dict['fsync'] = request.json['fsync']

    result = db.insert(doc_or_docs, manipulate, check_keys, continue_on_error,
                       **kwargs_dict)

    result_dict = {'resp': result}
    convert_objectid_to_string(result_dict)
    return result_dict
Beispiel #4
0
def insertData():
    if enc_key is None:
        abort(403, "Cannot store data without a key.\n")
    database = request.json['database']
    collection = request.json['collection']
    # Data is the data transferred
    data = request.json['data']
    convert_string_to_objectid(data)
    # Indices is a json dict mapping keys to [data_type, is_sparse]
    # Each index is of the form itemA.itemB.....itemZ,
    indices = request.json['indices']

    is_many = request.json['is_many']
    # Get the database
    db = get_collection(database, collection, indices)
    bypass_document_validation = request.json['bypass_document_validation']
    result_dict = dict()
    if is_many:
        ordered = request.json['ordered']
        result = db.insert_many(data, ordered, bypass_document_validation)
        result_dict['inserted_ids'] = result.inserted_ids
    else:
        result = db.insert_one(data, bypass_document_validation)
        result_dict['inserted_id'] = result.inserted_id
    result_dict['acknowledged'] = result.acknowledged
    convert_objectid_to_string(result_dict)
    return result_dict
Beispiel #5
0
def distinctData():
    if enc_key is None:
        abort(403, "Cannot load data without a key.\n")
    database = request.json['database']
    collection = request.json['collection']
    # Indices is a json dict mapping keys to [data_type, is_sparse]
    # Each index is of the form itemA.itemB.....itemZ,
    indices = request.json['indices']

    db = get_collection(database, collection, indices)

    cursor = getCursor(db.find)
    distinct_key = request.json['distinct_key']
    return {'distinct': cursor.distinct(distinct_key)}
Beispiel #6
0
def countData():
    if enc_key is None:
        abort(403, "Cannot load data without a key.\n")
    database = request.json['database']
    collection = request.json['collection']
    # Indices is a json dict mapping keys to [data_type, is_sparse]
    # Each index is of the form itemA.itemB.....itemZ,
    indices = request.json['indices']

    db = get_collection(database, collection, indices)

    cursor = getCursor(db.find)
    with_limit_and_skip = request.json['with_limit_and_skip']
    return {'count': cursor.count(with_limit_and_skip)}
Beispiel #7
0
def findOneData():
    if enc_key is None:
        abort(403, "Cannot load data without a key.\n")
    database = request.json['database']
    collection = request.json['collection']
    # Indices is a json dict mapping keys to [data_type, is_sparse]
    # Each index is of the form itemA.itemB.....itemZ,
    indices = request.json['indices']

    db = get_collection(database, collection, indices)
    data = getCursor(db.find_one)
    result_dict = {'data': data}
    convert_objectid_to_string(result_dict)
    return result_dict
Beispiel #8
0
def add_encrypt_key():
    global enc_key
    if enc_key:
        abort(403, "Key already given\n")
    else:
        enc_key = request.json['key']
        bytes_str = bytes(enc_key, 'utf-8')
        if len(bytes_str) < 32:
            bytes_str = bytes_str + bytes(
                "".join(['0'] * (32 - len(bytes_str))), "utf-8")
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            s.connect((url, 27018))
            # Send the first 32 bytes
            s.sendall(bytes_str[:32])
            s.recv(1024)
Beispiel #9
0
def updateDepricatedData():
    if enc_key is None:
        abort(403, "Cannot store data without a key.\n")
    database = request.json['database']
    collection = request.json['collection']
    # Indices is a json dict mapping keys to [data_type, is_sparse]
    # Each index is of the form itemA.itemB.....itemZ,
    indices = request.json['indices']
    # Get the database
    db = get_collection(database, collection, indices)

    # Get fields
    spec = request.json['spec']
    document = request.json['document']
    convert_string_to_objectid(document)
    upsert = request.json['upsert']
    manipulate = request.json['manipulate']
    check_keys = request.json['check_keys']
    # kwargs
    kwargs_dict = dict()

    if 'multi' in request.json:
        kwargs_dict['multi'] = request.json['multi']
    if 'w' in request.json:
        kwargs_dict['w'] = request.json['w']
    if 'wtimeout' in request.json:
        kwargs_dict['wtimeout'] = request.json['wtimeout']
    if 'j' in request.json:
        kwargs_dict['j'] = request.json['j']
    if 'fsync' in request.json:
        kwargs_dict['fsync'] = request.json['fsync']

    result = db.update(spec, document, upsert, manipulate, check_keys,
                       **kwargs_dict)

    result_dict = {'resp': result}

    convert_objectid_to_string(result_dict)
    return result_dict