from flask import Blueprint, request from ..schemas import model from .. import impl from ..services.auth_guard import auth_guard, check_jwt import json import logging from marshmallow import ValidationError from BreCal.validators.input_validation_times import InputValidationTimes from . import verify_if_request_is_json from BreCal.validators.validation_error import create_validation_error_response, create_werkzeug_error_response bp = Blueprint('times', __name__) @bp.route('/times', methods=['get']) @auth_guard() # no restriction by role def GetTimes(): options = {} options["shipcall_id"] = request.args.get("shipcall_id") return impl.times.GetTimes(options) @bp.route('/times', methods=['post']) @auth_guard() # no restriction by role def PostTimes(): try: verify_if_request_is_json(request) # print (request.is_json) content = request.get_json(force=True) # force gets us json even if the content-type was wrong # print (content) # body = parser.parse(schema, request, location='json') loadedModel = model.TimesSchema().load(data=content, many=False, partial=True) # read the user data from the JWT token (set when login is performed) user_data = check_jwt() # validate the request InputValidationTimes.evaluate_post_data(user_data, loadedModel, content) except ValidationError as ex: logging.error(ex) print(ex) return create_validation_error_response(ex=ex, status_code=400) except Exception as ex: logging.error(ex) print(ex) return json.dumps("bad format"), 400 return impl.times.PostTimes(loadedModel) @bp.route('/times', methods=['put']) @auth_guard() # no restriction by role def PutTimes(): try: verify_if_request_is_json(request) content = request.get_json(force=True) loadedModel = model.TimesSchema().load(data=content, many=False, partial=True) # read the user data from the JWT token (set when login is performed) user_data = check_jwt() # validate the request InputValidationTimes.evaluate_put_data(user_data, loadedModel, content) except ValidationError as ex: logging.error(ex) print(ex) return create_validation_error_response(ex=ex, status_code=400) except Exception as ex: logging.error(ex) print(ex) return json.dumps("bad format"), 400 return impl.times.PutTimes(loadedModel) @bp.route('/times', methods=['delete']) @auth_guard() # no restriction by role def DeleteTimes(): try: if 'id' in request.args: options = {} options["id"] = request.args.get("id") # read the user data from the JWT token (set when login is performed) user_data = check_jwt() # validate the request InputValidationTimes.evaluate_delete_data(user_data, times_id = request.args.get("id")) return impl.times.DeleteTimes(options) else: logging.warning("Times delete missing id argument") return json.dumps("missing argument"), 400 except ValidationError as ex: logging.error(ex) print(ex) return create_validation_error_response(ex=ex, status_code=400) except Exception as ex: logging.error(ex) print(ex) return json.dumps("bad format"), 400