2016-05-10 09:04:22 +01:00
|
|
|
from flask import Blueprint, jsonify, request
|
|
|
|
|
|
|
|
|
|
from app.schemas import provider_details_schema
|
2016-06-14 15:07:23 +01:00
|
|
|
|
2016-05-10 09:04:22 +01:00
|
|
|
from app.dao.provider_details_dao import (
|
|
|
|
|
get_provider_details,
|
|
|
|
|
get_provider_details_by_id,
|
|
|
|
|
dao_update_provider_details
|
|
|
|
|
)
|
|
|
|
|
|
2016-06-14 15:07:23 +01:00
|
|
|
from app.errors import (
|
|
|
|
|
register_errors,
|
|
|
|
|
InvalidRequest
|
|
|
|
|
)
|
|
|
|
|
|
2016-05-10 09:04:22 +01:00
|
|
|
provider_details = Blueprint('provider_details', __name__)
|
2016-06-14 15:07:23 +01:00
|
|
|
register_errors(provider_details)
|
2016-05-10 09:04:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@provider_details.route('', methods=['GET'])
|
|
|
|
|
def get_providers():
|
2016-06-14 15:07:23 +01:00
|
|
|
data = provider_details_schema.dump(get_provider_details(), many=True).data
|
2016-05-10 09:04:22 +01:00
|
|
|
return jsonify(provider_details=data)
|
|
|
|
|
|
|
|
|
|
|
2016-05-10 15:18:41 +01:00
|
|
|
@provider_details.route('/<uuid:provider_details_id>', methods=['GET'])
|
|
|
|
|
def get_provider_by_id(provider_details_id):
|
2016-06-14 15:07:23 +01:00
|
|
|
data = provider_details_schema.dump(get_provider_details_by_id(provider_details_id)).data
|
2016-05-10 15:18:41 +01:00
|
|
|
return jsonify(provider_details=data)
|
|
|
|
|
|
|
|
|
|
|
2016-05-10 09:04:22 +01:00
|
|
|
@provider_details.route('/<uuid:provider_details_id>', methods=['POST'])
|
|
|
|
|
def update_provider_details(provider_details_id):
|
|
|
|
|
fetched_provider_details = get_provider_details_by_id(provider_details_id)
|
|
|
|
|
|
|
|
|
|
current_data = dict(provider_details_schema.dump(fetched_provider_details).data.items())
|
|
|
|
|
current_data.update(request.get_json())
|
2016-06-14 15:07:23 +01:00
|
|
|
update_dict = provider_details_schema.load(current_data).data
|
2016-05-10 15:18:41 +01:00
|
|
|
|
2016-12-19 16:53:23 +00:00
|
|
|
invalid_keys = {'identifier', 'version'} & set(key for key in request.get_json().keys())
|
|
|
|
|
if invalid_keys:
|
2016-06-14 15:07:23 +01:00
|
|
|
message = "Not permitted to be updated"
|
2016-12-19 16:53:23 +00:00
|
|
|
errors = {key: [message] for key in invalid_keys}
|
2016-06-14 15:07:23 +01:00
|
|
|
raise InvalidRequest(errors, status_code=400)
|
2016-05-10 15:18:41 +01:00
|
|
|
|
2016-05-10 09:04:22 +01:00
|
|
|
dao_update_provider_details(update_dict)
|
2016-05-10 15:18:41 +01:00
|
|
|
return jsonify(provider_details=provider_details_schema.dump(fetched_provider_details).data), 200
|