From 3fad50994e7a6e113ac06120d3ba05fe64f59b30 Mon Sep 17 00:00:00 2001 From: Daniel Schick Date: Mon, 8 Sep 2025 14:17:21 +0200 Subject: [PATCH] bugfix enum format fixed required case fixed more default occurrances changed validates signature --- src/server/BreCal/schemas/model.py | 44 +++++++++++++++--------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/server/BreCal/schemas/model.py b/src/server/BreCal/schemas/model.py index b8e80c4..9e1f480 100644 --- a/src/server/BreCal/schemas/model.py +++ b/src/server/BreCal/schemas/model.py @@ -181,7 +181,7 @@ class Participant(Schema): ports: List[int] = field(default_factory=list) @validates("type") - def validate_type(self, value): + def validate_type(self, value, **kwargs): # e.g., when an IntFlag has the values 1,2,4; the maximum valid value is 7 max_int = sum([int(val) for val in list(ParticipantType._value2member_map_.values())]) min_int = 0 @@ -192,7 +192,7 @@ class Participant(Schema): @validates("flags") - def validate_flags(self, value): + def validate_flags(self, value, **kwargs): # e.g., when an IntFlag has the values 1,2,4; the maximum valid value is 7 max_int = sum([int(val) for val in list(ParticipantFlag._value2member_map_.values())]) min_int = 0 @@ -217,7 +217,7 @@ class ShipcallSchema(Schema): id = fields.Integer(required=True) ship_id = fields.Integer(required=True) port_id = fields.Integer(required=True) - type = fields.Enum(ShipcallType, default=ShipcallType.undefined) + type = fields.Enum(ShipcallType, load_default=ShipcallType.undefined, dump_default=ShipcallType.undefined) eta = fields.DateTime(required=False, allow_none=True) voyage = fields.String(allow_none=True, required=False, validate=[validate.Length(max=16)]) etd = fields.DateTime(required=False, allow_none=True) @@ -238,7 +238,7 @@ class ShipcallSchema(Schema): anchored = fields.Bool(required=False, allow_none=True) moored_lock = fields.Bool(required=False, allow_none=True) canceled = fields.Bool(required=False, allow_none=True) - evaluation = fields.Enum(EvaluationType, required=False, allow_none=True, default=EvaluationType.undefined) + evaluation = fields.Enum(EvaluationType, required=False, allow_none=True, load_default=EvaluationType.undefined, dump_default=ShipcallType.undefined) evaluation_message = fields.Str(allow_none=True, required=False) evaluation_time = fields.DateTime(required=False, allow_none=True) evaluation_notifications_sent = fields.Bool(required=False, allow_none=True) @@ -261,7 +261,7 @@ class ShipcallSchema(Schema): return data @validates("type") - def validate_type(self, value): + def validate_type(self, value, **kwargs): valid_shipcall_type = int(value) in [item.value for item in ShipcallType] if not valid_shipcall_type: @@ -398,7 +398,7 @@ class TimesSchema(Schema): berth_info = fields.String(required=False, allow_none=True, validate=[validate.Length(max=512)]) pier_side = fields.Bool(required=False, allow_none = True) shipcall_id = fields.Integer(required=True) - participant_type = fields.Integer(Required = False, allow_none=True)# TODO: could become Enum. # participant_type = fields.Enum(ParticipantType, required=False, allow_none=True, default=ParticipantType.undefined) #fields.Integer(required=False, allow_none=True) + participant_type = fields.Integer(required = False, allow_none=True) # TODO: could become Enum ata = fields.DateTime(required=False, allow_none=True) atd = fields.DateTime(required=False, allow_none=True) eta_interval_end = fields.DateTime(required=False, allow_none=True) @@ -407,7 +407,7 @@ class TimesSchema(Schema): modified = fields.DateTime(required=False, allow_none=True) @validates("participant_type") - def validate_participant_type(self, value): + def validate_participant_type(self, value, **kwargs): # #TODO: it may also make sense to block multi-assignments, whereas a value could be BSMD+AGENCY # while the validation fails when one of those multi-assignments is BSMD, it passes in cases, # such as AGENCY+PILOT @@ -420,56 +420,56 @@ class TimesSchema(Schema): raise ValidationError({"participant_type":f"the participant_type must not be .BSMD"}) @validates("eta_berth") - def validate_eta_berth(self, value): + def validate_eta_berth(self, value, **kwargs): # violation when time is not in the future, but also does not exceed a threshold for the 'reasonable' future # when 'value' is 'None', a ValidationError is not issued. valid_time = validate_time_is_in_not_too_distant_future(raise_validation_error=True, value=value, months=12) return @validates("etd_berth") - def validate_etd_berth(self, value): + def validate_etd_berth(self, value, **kwargs): # violation when time is not in the future, but also does not exceed a threshold for the 'reasonable' future # when 'value' is 'None', a ValidationError is not issued. valid_time = validate_time_is_in_not_too_distant_future(raise_validation_error=True, value=value, months=12) return @validates("lock_time") - def validate_lock_time(self, value): + def validate_lock_time(self, value, **kwargs): # violation when time is not in the future, but also does not exceed a threshold for the 'reasonable' future # when 'value' is 'None', a ValidationError is not issued. valid_time = validate_time_is_in_not_too_distant_future(raise_validation_error=True, value=value, months=12) return @validates("zone_entry") - def validate_zone_entry(self, value): + def validate_zone_entry(self, value, **kwargs): # violation when time is not in the future, but also does not exceed a threshold for the 'reasonable' future # when 'value' is 'None', a ValidationError is not issued. valid_time = validate_time_is_in_not_too_distant_future(raise_validation_error=True, value=value, months=12) return @validates("operations_start") - def validate_operations_start(self, value): + def validate_operations_start(self, value, **kwargs): # violation when time is not in the future, but also does not exceed a threshold for the 'reasonable' future # when 'value' is 'None', a ValidationError is not issued. valid_time = validate_time_is_in_not_too_distant_future(raise_validation_error=True, value=value, months=12) return @validates("operations_end") - def validate_operations_end(self, value): + def validate_operations_end(self, value, **kwargs): # violation when time is not in the future, but also does not exceed a threshold for the 'reasonable' future # when 'value' is 'None', a ValidationError is not issued. valid_time = validate_time_is_in_not_too_distant_future(raise_validation_error=True, value=value, months=12) return @validates("eta_interval_end") - def validate_eta_interval_end(self, value): + def validate_eta_interval_end(self, value, **kwargs): # violation when time is not in the future, but also does not exceed a threshold for the 'reasonable' future # when 'value' is 'None', a ValidationError is not issued. valid_time = validate_time_is_in_not_too_distant_future(raise_validation_error=True, value=value, months=12) return @validates("etd_interval_end") - def validate_etd_interval_end(self, value): + def validate_etd_interval_end(self, value, **kwargs): # violation when time is not in the future, but also does not exceed a threshold for the 'reasonable' future # when 'value' is 'None', a ValidationError is not issued. valid_time = validate_time_is_in_not_too_distant_future(raise_validation_error=True, value=value, months=12) @@ -492,13 +492,13 @@ class UserSchema(Schema): # #TODO: the user schema does not (yet) include the 'notify_' fields @validates("user_phone") - def validate_user_phone(self, value): + def validate_user_phone(self, value, **kwargs): valid_characters = list(map(str,range(0,10)))+["+", " "] if not all([v in valid_characters for v in value]): raise ValidationError({"user_phone":f"one of the phone number values is not valid."}) @validates("user_email") - def validate_user_email(self, value): + def validate_user_email(self, value, **kwargs): if not "@" in value: raise ValidationError({"user_email":f"invalid email address"}) @@ -578,15 +578,15 @@ class ShipSchema(Schema): participant_id = fields.Int(allow_none=True, required=False) length = fields.Float(allow_none=True, required=False, validate=[validate.Range(min=0, max=1000, min_inclusive=False, max_inclusive=False)]) width = fields.Float(allow_none=True, required=False, validate=[validate.Range(min=0, max=100, min_inclusive=False, max_inclusive=False)]) - is_tug = fields.Bool(allow_none=True, required=False, default=False) + is_tug = fields.Bool(allow_none=True, required=False, load_default=False, dump_default=False) bollard_pull = fields.Int(allow_none=True, required=False) eni = fields.Int(allow_none=True, required=False) created = fields.DateTime(allow_none=True, required=False) modified = fields.DateTime(allow_none=True, required=False) - deleted = fields.Bool(allow_none=True, required=False, default=False) + deleted = fields.Bool(allow_none=True, required=False, load_default=False, dump_default=False) @validates("name") - def validate_name(self, value): + def validate_name(self, value, **kwargs): character_length = len(str(value)) if character_length<1: raise ValidationError({"name":f"'name' argument should have at least one character"}) @@ -598,7 +598,7 @@ class ShipSchema(Schema): return @validates("imo") - def validate_imo(self, value): + def validate_imo(self, value, **kwargs): value = str(value).zfill(7) # 1 becomes '0000001' (7 characters). 12345678 becomes '12345678' (8 characters) imo_length = len(value) if imo_length != 7: @@ -606,7 +606,7 @@ class ShipSchema(Schema): return @validates("callsign") - def validate_callsign(self, value): + def validate_callsign(self, value, **kwargs): if value is not None: callsign_length = len(str(value)) if callsign_length>8: