began creating pseudo code for InputValidationTimes for POST, PUT and DELETE requests.

This commit is contained in:
Max Metz 2024-05-27 18:51:51 +02:00
parent 4c1b230de9
commit 4aecb66408

View File

@ -0,0 +1,92 @@
import typing
import json
import datetime
from abc import ABC, abstractmethod
from marshmallow import ValidationError
from string import ascii_letters, digits
from BreCal.schemas.model import Ship, Shipcall, Berth, User, Participant, ShipcallType, Times
from BreCal.impl.participant import GetParticipant
from BreCal.impl.ships import GetShips
from BreCal.impl.berths import GetBerths
from BreCal.impl.times import GetTimes
from BreCal.database.enums import ParticipantType, ParticipantFlag
from BreCal.validators.input_validation_utils import check_if_user_is_bsmd_type, check_if_ship_id_is_valid, check_if_berth_id_is_valid, check_if_participant_ids_are_valid, check_if_participant_ids_and_types_are_valid, get_shipcall_id_dictionary, get_participant_type_from_user_data
from BreCal.database.sql_handler import execute_sql_query_standalone
from BreCal.validators.validation_base_utils import check_if_int_is_valid_flag, check_if_string_has_special_characters
import werkzeug
class InputValidationTimes():
"""
This class combines a complex set of individual input validation functions into a joint object.
It uses static methods, so the object does not need to be instantiated, but functions can be called immediately.
Example:
InputValidationTimes.evaluate(user_data, loadedModel, content)
When the data violates one of the rules, a marshmallow.ValidationError is raised, which details the issues.
"""
def __init__(self) -> None:
pass
@staticmethod
def evaluate_post_data(user_data:dict, loadedModel:dict, content:dict):
raise NotImplementedError("skeleton")
return
@staticmethod
def evaluate_put_data(user_data:dict, loadedModel:dict, content:dict):
raise NotImplementedError("skeleton")
# 1.) Only users of the same participant_id, which the times dataset refers to, can delete the entry
# (same as for .evaluate_delete_data)
# 2.) Reference checking
# (same as for .evaluate_post_data)
# 3.) Value checking
# (same as for .evaluate_post_data)
# participant type should be dynamically checked for POST / PUT. All other values can be validated within the Schema
return
@staticmethod
def evaluate_delete_data(user_data:dict, times_id:int):
raise NotImplementedError("skeleton")
# 1.) Only users of the same participant_id, which the times dataset refers to, can delete the entry
# (same as for .evaluate_put_data)
# 2.) The dataset entry may not be deleted already
shipcall_id is not Defined
InputValidationTimes.check_if_entry_is_already_deleted(times_id, shipcall_id)
return
@staticmethod
def check_if_entry_is_already_deleted(times_id:int, shipcall_id:int):
"""
When calling a delete request for ships, the dataset may not be deleted already. This method
makes sure, that the request contains and ID, has a matching entry in the database, and the
database entry may not have a deletion state already.
"""
raise NotImplementedError("skeleton. fully untested pseudo-code.")
if times_id is None:
raise ValidationError(f"The times_id must be provided.")
# options["shipcall_id"]
assert 'shipcall_id' in options.keys()
response, status_code, header = GetTimes(options)
times = json.loads(response)
existing_database_entries = [time_ for time_ in times if time_.get("id")==times_id]
if len(existing_database_entries)==0:
raise ValidationError(f"Could not find a times entry with the specified ID. Selected: {times_id}")
existing_database_entry = existing_database_entries[0]
deletion_state = existing_database_entry.get("deleted",None)
if deletion_state:
raise ValidationError(f"The selected time entry is already deleted.")
return