Beispiel #1
0
def update_object(collection, id):
    id = int(id)

    if collection not in CRUD_COLLECTIONS:
        return respond({'error': 'Page not found'}, 404)

    if id in NON_MODIFIABLE[collection]:
        return respond({'error': 'Cannot be modified'}, 400)

    data = request.json
    if data is None or data == {}:
        return respond({'error': 'Please provide the data to update'}, 400)

    db = MongoAPI(DATABASE, collection)
    if not db.exists({'_id': id}):
        return respond({'error': 'The collection has no record with the provided ID'}, 400)

    content_to_update = {}
    for field_name in UPDATEABLE_FIELDS[collection]:
        if field_name in data:
            content_to_update[field_name] = int(data[field_name]) if type(data[field_name]) == str and data[field_name].isdigit() else data[field_name]

    if len(content_to_update) > 0:
        response = db.update({'_id': id}, content_to_update)
        return respond(response, 200)
    else:
        return respond({'error': 'You can only update {} for {}'.format(', '.join(UPDATEABLE_FIELDS[collection]), collection)}, 400)
Beispiel #2
0
def delete_object(collection, id):
    id = int(id)

    if collection not in CRUD_COLLECTIONS:
        return respond({'error': 'Page not found'}, 404)

    if id in NON_MODIFIABLE[collection]:
        return respond({'error': 'Cannot be modified'}, 400)

    db = MongoAPI(DATABASE, collection)
    if not db.exists({'_id': id}):
        return respond({'error': 'The collection has no record with the provided ID'}, 400)

    for check in EXISTENCE_CHECKS_BEFORE_DELETE[collection]:
        child_db = MongoAPI(DATABASE, check.collection)
        if child_db.exists({check.field_name: id}):
            return respond({'error': 'Please first remove all the {} for this entity'.format(check.collection)}, 400)

    response = db.delete({'_id': id})
    return respond(response, 200)