serverside fixes for detecting unchanged time values

This commit is contained in:
Daniel Schick 2024-11-10 15:08:41 +01:00
parent 08792c5fa7
commit 2576127b79
2 changed files with 30 additions and 14 deletions

View File

@ -129,11 +129,14 @@ class InputValidationShipcall():
if check_if_int_is_valid_flag(flags_value, enum_object=ParticipantFlag): if check_if_int_is_valid_flag(flags_value, enum_object=ParticipantFlag):
raise ValidationError({"flags":f"incorrect value provided for 'flags'. Must be a valid combination of the flags."}) raise ValidationError({"flags":f"incorrect value provided for 'flags'. Must be a valid combination of the flags."})
existing_shipcall = None
if is_put_data: if is_put_data:
# the type of a shipcall may not be changed. It can only be set with the initial POST-request.
InputValidationShipcall.check_shipcall_type_is_unchanged(loadedModel)
InputValidationShipcall.check_times_are_in_future(loadedModel, content) existing_shipcall = InputValidationShipcall.get_shipcall_by_id(loadedModel.get("id"))
# the type of a shipcall may not be changed. It can only be set with the initial POST-request.
InputValidationShipcall.check_shipcall_type_is_unchanged(loadedModel, existing_shipcall)
InputValidationShipcall.check_times_are_in_future(loadedModel, content, existing_shipcall)
# some arguments must not be provided # some arguments must not be provided
InputValidationShipcall.check_forbidden_arguments(content, forbidden_keys=forbidden_keys) InputValidationShipcall.check_forbidden_arguments(content, forbidden_keys=forbidden_keys)
@ -261,15 +264,17 @@ class InputValidationShipcall():
raise ValidationError({"participants":f"every participant id and type should be listed only once. Found multiple entries for one of the participants."}) raise ValidationError({"participants":f"every participant id and type should be listed only once. Found multiple entries for one of the participants."})
@staticmethod @staticmethod
def check_shipcall_type_is_unchanged(loadedModel:dict): def check_shipcall_type_is_unchanged(loadedModel:dict, existing_shipcall:object):
# the type of a shipcall may only be set on POST requests. Afterwards, shipcall types may not be changed. if int(loadedModel["type"]) != int(existing_shipcall.type):
query = SQLQuery.get_shipcall_by_id()
shipcall = execute_sql_query_standalone(query=query, model=Shipcall, param={"id":loadedModel.get("id")}, command_type="single")
if int(loadedModel["type"]) != int(shipcall.type):
raise ValidationError({"type":f"The shipcall type may only be set in the initial POST-request. Afterwards, changing the shipcall type is not allowed."}) # @pytest.raises raise ValidationError({"type":f"The shipcall type may only be set in the initial POST-request. Afterwards, changing the shipcall type is not allowed."}) # @pytest.raises
return return
@staticmethod
def get_shipcall_by_id(shipcall_id:int):
query = SQLQuery.get_shipcall_by_id()
shipcall = execute_sql_query_standalone(query=query, model=Shipcall, param={"id":shipcall_id}, command_type="single")
return shipcall
@staticmethod @staticmethod
def check_forbidden_arguments(content:dict, forbidden_keys=["evaluation", "evaluation_message"]): def check_forbidden_arguments(content:dict, forbidden_keys=["evaluation", "evaluation_message"]):
""" """
@ -330,7 +335,7 @@ class InputValidationShipcall():
return return
@staticmethod @staticmethod
def check_times_are_in_future(loadedModel:dict, content:dict): def check_times_are_in_future(loadedModel:dict, content:dict, existing_shipcall:object):
""" """
Dates should be in the future. Depending on the ShipcallType, specific values should be checked Dates should be in the future. Depending on the ShipcallType, specific values should be checked
Perfornms datetime checks in the loadedModel (datetime.datetime objects). Perfornms datetime checks in the loadedModel (datetime.datetime objects).
@ -353,11 +358,20 @@ class InputValidationShipcall():
tidal_window_from = loadedModel.get("tidal_window_from", None) tidal_window_from = loadedModel.get("tidal_window_from", None)
tidal_window_to = loadedModel.get("tidal_window_to", None) tidal_window_to = loadedModel.get("tidal_window_to", None)
if existing_shipcall is not None:
existing_eta = existing_shipcall.eta
existing_etd = existing_shipcall.etd
existing_tidal_window_from = existing_shipcall.tidal_window_from
existing_tidal_window_to = existing_shipcall.tidal_window_to
if eta != existing_eta or etd != existing_etd:
# Estimated arrival or departure times # Estimated arrival or departure times
InputValidationShipcall.check_times_in_future_based_on_type(type_, time_ref, eta, etd) InputValidationShipcall.check_times_in_future_based_on_type(type_, time_ref, eta, etd)
if tidal_window_from != existing_tidal_window_from or tidal_window_to != existing_tidal_window_to:
# Tidal Window # Tidal Window
InputValidationShipcall.check_tidal_window_in_future(type_, time_ref, tidal_window_from, tidal_window_to) InputValidationShipcall.check_tidal_window_in_future(type_, time_ref, tidal_window_from, tidal_window_to)
return return
@staticmethod @staticmethod

View File

@ -419,6 +419,8 @@ class InputValidationTimes():
# commonly used in the PUT-request # commonly used in the PUT-request
if loadedModel is not None: if loadedModel is not None:
(shipcall_id, times_assigned_participant) = InputValidationTimes.prepare_authority_check_for_put_request(loadedModel) (shipcall_id, times_assigned_participant) = InputValidationTimes.prepare_authority_check_for_put_request(loadedModel)
else:
loadedModel = get_times_data_for_id(times_id)
# commonly used in the DELETE-request # commonly used in the DELETE-request
if times_id is not None: if times_id is not None: