112 lines
3.8 KiB
Python
112 lines
3.8 KiB
Python
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, create_dynamic_exception_response
|
|
|
|
bp = Blueprint('times', __name__)
|
|
|
|
|
|
@bp.route('/times', methods=['get'])
|
|
@auth_guard() # no restriction by role
|
|
def GetTimes():
|
|
|
|
try:
|
|
options = {}
|
|
options["shipcall_id"] = request.args.get("shipcall_id")
|
|
return impl.times.GetTimes(options)
|
|
|
|
except Exception as ex:
|
|
return create_dynamic_exception_response(ex=ex, status_code=400)
|
|
|
|
|
|
@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)
|
|
return impl.times.PostTimes(loadedModel)
|
|
|
|
except ValidationError as ex:
|
|
return create_validation_error_response(ex=ex, status_code=400)
|
|
|
|
except Exception as ex:
|
|
return create_dynamic_exception_response(ex=ex, status_code=400, message="bad format")
|
|
|
|
|
|
|
|
@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()
|
|
|
|
if not InputValidationTimes.exists_times_by_id(loadedModel.get("id")):
|
|
return create_dynamic_exception_response(ex=None, status_code=404, message="no times found with the provided id")
|
|
|
|
# validate the request
|
|
InputValidationTimes.evaluate_put_data(user_data, loadedModel, content)
|
|
return impl.times.PutTimes(loadedModel)
|
|
|
|
except ValidationError as ex:
|
|
return create_validation_error_response(ex=ex, status_code=400)
|
|
|
|
except Exception as ex:
|
|
return create_dynamic_exception_response(ex=ex, status_code=400, message="bad format")
|
|
|
|
|
|
|
|
@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()
|
|
|
|
if not InputValidationTimes.exists_times_by_id(options["id"]):
|
|
return create_dynamic_exception_response(ex=None, status_code=404, message="no times found with the provided id")
|
|
|
|
# validate the request
|
|
InputValidationTimes.evaluate_delete_data(user_data, times_id = request.args.get("id"))
|
|
|
|
return impl.times.DeleteTimes(options)
|
|
else:
|
|
return create_dynamic_exception_response(ex=None, status_code=400, message="Times delete missing argument: id")
|
|
|
|
except ValidationError as ex:
|
|
return create_validation_error_response(ex=ex, status_code=400)
|
|
|
|
except Exception as ex:
|
|
return create_dynamic_exception_response(ex=ex, status_code=400, message="bad format")
|