Exemplo n.º 1
0
def register_driver():
    try:
        request_json = request.get_json()
    except Exception as e:
        return make_response(
            jsonify({'return_message': 'JSON Object not found'}), 400)
    # If to validate by test_register_driver_json_not_exist, need to be improved
    if request.get_json() is None:
        return make_response(
            jsonify({'return_message': 'JSON Object not found'}), 400)

    json__ = request.get_json()
    errors = driver_schema.validate(request.get_json())
    if errors:
        return make_response(
            jsonify({
                'return_message':
                'There is some erros in your request see errors_field',
                'errors_field': errors
            }), 400)

    request_json = request.get_json()
    driver = Driver(request_json.get('name', None),
                    request_json.get('last_name', None),
                    request_json.get('date_of_birth', None),
                    request_json.get('gender_id', None),
                    request_json.get('cnh_type_id', None),
                    request_json.get('own_vehicle', None))

    driver = driver_dao.save_driver(driver)
    return jsonify({
        'driver': driver.to_json(),
        'return_message': 'Driver Registered'
    }), 201
Exemplo n.º 2
0
def update_driver(driver_id):
    try:
        json_to_validade = request.get_json()
    except Exception as e:
        return make_response(
            jsonify({'return_message': 'JSON Object not found'}), 400)
    # If to validate by test_register_driver_json_not_exist, need to be improved
    if request.get_json() is None:
        return make_response(
            jsonify({'return_message': 'JSON Object not found'}), 400)

    json_to_validade['driver_id'] = driver_id
    errors = driver_schema.validate(json_to_validade)
    if errors:
        return make_response(
            jsonify({
                'return_message':
                'There is some erros in your request see errors_field',
                'errors_field': errors
            }), 400)

    driver = driver_dao.get_driver_by_id(driver_id)
    if driver:
        request_json = request.get_json()
        driver = Driver(request_json.get('name', None),
                        request_json.get('last_name', None),
                        request_json.get('date_of_birth', None),
                        request_json.get('gender_id', None),
                        request_json.get('cnh_type_id', None),
                        request_json.get('own_vehicle', None), driver_id)

        driver = driver_dao.save_driver(driver)
        return jsonify({
            'driver': driver.to_json(),
            'return_message': 'Driver Updated'
        }), 200
    else:
        abort(404, {'message': 'Driver does not exist'})