fixed bug in ship create

This commit is contained in:
Daniel Schick 2024-09-23 08:39:25 +02:00
parent 27179da2a2
commit 7921a138d4

View File

@ -6,7 +6,7 @@ from marshmallow import ValidationError
from string import ascii_letters, digits from string import ascii_letters, digits
from BreCal.schemas.model import Ship, Shipcall, Berth, User, Participant, ShipcallType from BreCal.schemas.model import Ship, Shipcall, Berth, User, Participant, ShipcallType
from BreCal.database.sql_handler import execute_sql_query_standalone from BreCal.database.sql_handler import execute_sql_query_standalone
from BreCal.database.sql_queries import SQLQuery from BreCal.database.sql_queries import SQLQuery
from BreCal.impl.participant import GetParticipant from BreCal.impl.participant import GetParticipant
from BreCal.impl.ships import GetShips from BreCal.impl.ships import GetShips
@ -27,7 +27,7 @@ class InputValidationShip():
Example: Example:
InputValidationShip.evaluate(user_data, loadedModel, content) InputValidationShip.evaluate(user_data, loadedModel, content)
When the data violates one of the rules, a marshmallow.ValidationError is raised, which details the issues. When the data violates one of the rules, a marshmallow.ValidationError is raised, which details the issues.
""" """
def __init__(self) -> None: def __init__(self) -> None:
pass pass
@ -37,13 +37,13 @@ class InputValidationShip():
# 1.) Only users of type BSMD are allowed to POST # 1.) Only users of type BSMD are allowed to POST
InputValidationShip.check_user_is_bsmd_type(user_data) InputValidationShip.check_user_is_bsmd_type(user_data)
# 2.) The ship IMOs are used as matching keys. They must be unique in the database. # 2.) The ship IMOs are used as matching keys. They must be unique in the database.
InputValidationShip.check_ship_imo_already_exists(loadedModel) InputValidationShip.check_ship_imo_already_exists(loadedModel)
# 3.) Check for reasonable Values (see BreCal.schemas.model.ShipSchema) # 3.) Check for reasonable Values (see BreCal.schemas.model.ShipSchema)
InputValidationShip.optionally_evaluate_bollard_pull_value(content) InputValidationShip.optionally_evaluate_bollard_pull_value(content)
return return
@staticmethod @staticmethod
def evaluate_put_data(user_data:dict, loadedModel:dict, content:dict): def evaluate_put_data(user_data:dict, loadedModel:dict, content:dict):
# 1.) Only users of type BSMD are allowed to PUT # 1.) Only users of type BSMD are allowed to PUT
@ -58,20 +58,20 @@ class InputValidationShip():
# 4.) Check for reasonable Values (see BreCal.schemas.model.ShipSchema) # 4.) Check for reasonable Values (see BreCal.schemas.model.ShipSchema)
InputValidationShip.optionally_evaluate_bollard_pull_value(content) InputValidationShip.optionally_evaluate_bollard_pull_value(content)
return return
@staticmethod @staticmethod
def evaluate_delete_data(user_data:dict, ship_id:typing.Optional[int]): def evaluate_delete_data(user_data:dict, ship_id:typing.Optional[int]):
if ship_id is None: if ship_id is None:
raise ValidationError({"id":f"The ship id must be provided."}) raise ValidationError({"id":f"The ship id must be provided."})
ship_id = int(ship_id) ship_id = int(ship_id)
# 1.) Only users of type BSMD are allowed to PUT # 1.) Only users of type BSMD are allowed to PUT
InputValidationShip.check_user_is_bsmd_type(user_data) InputValidationShip.check_user_is_bsmd_type(user_data)
# 2.) The dataset entry may not be deleted already # 2.) The dataset entry may not be deleted already
InputValidationShip.check_if_entry_is_already_deleted(ship_id) InputValidationShip.check_if_entry_is_already_deleted(ship_id)
return return
@staticmethod @staticmethod
def optionally_evaluate_bollard_pull_value(content:dict): def optionally_evaluate_bollard_pull_value(content:dict):
bollard_pull = content.get("bollard_pull",None) bollard_pull = content.get("bollard_pull",None)
@ -89,7 +89,7 @@ class InputValidationShip():
is_bsmd = check_if_user_is_bsmd_type(user_data) is_bsmd = check_if_user_is_bsmd_type(user_data)
if not is_bsmd: if not is_bsmd:
raise ValidationError({"participant_type":f"current user does not belong to BSMD. Cannot post, put or delete ships. Found user data: {user_data}"}) raise ValidationError({"participant_type":f"current user does not belong to BSMD. Cannot post, put or delete ships. Found user data: {user_data}"})
@staticmethod @staticmethod
def check_ship_imo_already_exists(loadedModel:dict): def check_ship_imo_already_exists(loadedModel:dict):
# get the ships, convert them to a list of JSON dictionaries # get the ships, convert them to a list of JSON dictionaries
@ -97,35 +97,35 @@ class InputValidationShip():
ships = json.loads(response) ships = json.loads(response)
# extract only the 'imo' values # extract only the 'imo' values
ship_imos = [ship.get("imo") for ship in ships if not ship.deleted] ship_imos = [ship.get("imo") for ship in ships if not ship.get("deleted")]
# check, if the imo in the POST-request already exists in the list # check, if the imo in the POST-request already exists in the list
imo_already_exists = loadedModel.get("imo") in ship_imos imo_already_exists = loadedModel.get("imo") in ship_imos
if imo_already_exists: if imo_already_exists:
raise ValidationError({"imo":f"the provided ship IMO {loadedModel.get('imo')} already exists. A ship may only be added, if there is no other ship with the same IMO number."}) raise ValidationError({"imo":f"the provided ship IMO {loadedModel.get('imo')} already exists. A ship may only be added, if there is no other ship with the same IMO number."})
return return
@staticmethod @staticmethod
def put_content_may_not_contain_imo_number(content:dict): def put_content_may_not_contain_imo_number(content:dict):
# IMO is a required field, so it will never be None outside of tests. If so, there is no violation # IMO is a required field, so it will never be None outside of tests. If so, there is no violation
put_data_ship_imo = content.get("imo",None) put_data_ship_imo = content.get("imo",None)
if put_data_ship_imo is None: if put_data_ship_imo is None:
return return
# grab the ship by its ID and compare, whether the IMO is unchanged # grab the ship by its ID and compare, whether the IMO is unchanged
ship = execute_sql_query_standalone(SQLQuery.get_ship_by_id(), param={"id":content.get("id")}, command_type="single", model=Ship) ship = execute_sql_query_standalone(SQLQuery.get_ship_by_id(), param={"id":content.get("id")}, command_type="single", model=Ship)
if put_data_ship_imo != ship.imo: if put_data_ship_imo != ship.imo:
raise ValidationError({"imo":f"The IMO number field may not be changed since it serves the purpose of a primary (matching) key."}) raise ValidationError({"imo":f"The IMO number field may not be changed since it serves the purpose of a primary (matching) key."})
return return
@staticmethod @staticmethod
def content_contains_ship_id(content:dict): def content_contains_ship_id(content:dict):
put_data_ship_id = content.get('id',None) put_data_ship_id = content.get('id',None)
if put_data_ship_id is None: if put_data_ship_id is None:
raise ValidationError({"id":f"The id field is required."}) raise ValidationError({"id":f"The id field is required."})
return return
@staticmethod @staticmethod
def check_if_entry_is_already_deleted(ship_id:typing.Optional[int]): def check_if_entry_is_already_deleted(ship_id:typing.Optional[int]):
""" """
@ -147,4 +147,4 @@ class InputValidationShip():
return return