From c32160811d637cb6cab560d1396cda3dc12b2791 Mon Sep 17 00:00:00 2001 From: scopesorting Date: Mon, 20 Nov 2023 17:44:28 +0100 Subject: [PATCH] enumerators for Shipcall (type, evaluation), Participant (type, flags) --- misc/BreCalApi.yaml | 10 ++++---- src/server/BreCal/database/enums.py | 9 +++++-- src/server/BreCal/schemas/model.py | 24 ++++++++++--------- src/server/BreCal/stubs/participant.py | 6 +++-- src/server/BreCal/stubs/shipcall.py | 5 ++-- .../BreCal/validators/validation_rules.py | 2 +- .../validators/test_validation_rule_state.py | 2 +- 7 files changed, 35 insertions(+), 23 deletions(-) diff --git a/misc/BreCalApi.yaml b/misc/BreCalApi.yaml index f3c845c..96850dc 100644 --- a/misc/BreCalApi.yaml +++ b/misc/BreCalApi.yaml @@ -355,8 +355,8 @@ components: ship_id: type: integer type: - type: integer - # TODO: use an enum + type: string + enum: [UNDEFINED, INCOMING, OUTGOING, SHIFTING] eta: type: string format: date-time @@ -424,8 +424,9 @@ components: type: boolean nullable: true evaluation: - type: integer + type: string nullable: true + enum: [NONE, GREEN, YELLOW, RED] evaluation_message: type: string nullable: true @@ -660,7 +661,8 @@ components: type: string maxLength: 64 type: - type: integer + type: string + enum: [NONE, BSMD, TERMINAL, PILOT, AGENCY, MOORING, PORT_ADMINISTRATION, TUG] flags: type: integer nullable: true diff --git a/src/server/BreCal/database/enums.py b/src/server/BreCal/database/enums.py index e038643..0fa7cbd 100644 --- a/src/server/BreCal/database/enums.py +++ b/src/server/BreCal/database/enums.py @@ -2,7 +2,7 @@ from enum import Enum class ParticipantType(Enum): """determines the type of a participant""" - NONE = 0 + UNDEFINED = 0 BSMD = 1 TERMINAL = 2 PILOT = 4 @@ -11,8 +11,13 @@ class ParticipantType(Enum): PORT_ADMINISTRATION = 32 TUG = 64 +class ParticipantFlag(Enum): + UNDEFINED = 0 + ALLOW_BSMD = 1 + class ShipcallType(Enum): """determines the type of a shipcall, as this changes the applicable validation rules""" + UNDEFINED = 0 INCOMING = 1 OUTGOING = 2 SHIFTING = 3 @@ -31,7 +36,7 @@ class StatusFlags(Enum): these enumerators ensure that each traffic light validation rule state corresponds to a value, which will be used in the ValidationRules object to identify the necessity of notifications. """ - NONE = 0 + UNDEFINED = 0 GREEN = 1 YELLOW = 2 RED = 3 diff --git a/src/server/BreCal/schemas/model.py b/src/server/BreCal/schemas/model.py index 193c3f4..0ac9b76 100644 --- a/src/server/BreCal/schemas/model.py +++ b/src/server/BreCal/schemas/model.py @@ -7,6 +7,8 @@ from typing import List import json import datetime +from BreCal.database.enums import ParticipantFlag, ParticipantType, ShipcallType, StatusFlags + def obj_dict(obj): if isinstance(obj, datetime.datetime): return obj.isoformat() @@ -36,8 +38,8 @@ class Notification(Schema): id: int times_id: int acknowledged: bool - level: int - type: int + level: int # note: could become an enum + type: int # note: could become an enum message: str created: datetime modified: datetime @@ -49,8 +51,8 @@ class Participant(Schema): street: str postal_code: str city: str - type: int - flags: int + type: ParticipantType + flags: ParticipantFlag created: datetime modified: datetime deleted: bool @@ -60,7 +62,7 @@ class ParticipantList(Participant): class ParticipantAssignmentSchema(Schema): participant_id = fields.Int() - type = fields.Int() + type : ParticipantType = fields.Enum(enum=ParticipantType) class ShipcallSchema(Schema): def __init__(self): @@ -69,7 +71,7 @@ class ShipcallSchema(Schema): id = fields.Int() ship_id = fields.Int() - type = fields.Int() + type : ShipcallType = fields.Enum(enum=ShipcallType) eta = fields.DateTime(Required = False, allow_none=True) voyage = fields.Str(Required = False, allow_none=True) etd = fields.DateTime(Required = False, allow_none=True) @@ -77,7 +79,7 @@ class ShipcallSchema(Schema): departure_berth_id = fields.Int(Required = False, allow_none=True) tug_required = fields.Bool(Required = False, allow_none=True) pilot_required = fields.Bool(Required = False, allow_none=True) - flags = fields.Int(Required = False, allow_none=True) + flags = fields.Int(Required = False, allow_none=True) # note: could become an enum pier_side = fields.Bool(Required = False, allow_none=True) bunkering = fields.Bool(Required = False, allow_none=True) replenishing_terminal = fields.Bool(Required = False, allow_none=True) @@ -90,7 +92,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.Int(Required = False, allow_none=True) + evaluation = fields.Enum(Required = False, allow_none=True, enum=StatusFlags) evaluation_message = fields.Str(Required = False, allow_none=True) participants = fields.List(fields.Nested(ParticipantAssignmentSchema)) created = fields.DateTime(Required = False, allow_none=True) @@ -104,14 +106,14 @@ class Participant_Assignment: pass participant_id: int - type: int + type: ParticipantType @dataclass class Shipcall: id: int ship_id: int - type: str + type: ShipcallType eta: datetime voyage: str etd: datetime @@ -132,7 +134,7 @@ class Shipcall: anchored: bool moored_lock: bool canceled: bool - evaluation: int + evaluation: StatusFlags evaluation_message: str created: datetime modified: datetime diff --git a/src/server/BreCal/stubs/participant.py b/src/server/BreCal/stubs/participant.py index 38f303c..f08ab45 100644 --- a/src/server/BreCal/stubs/participant.py +++ b/src/server/BreCal/stubs/participant.py @@ -1,6 +1,8 @@ import datetime from BreCal.stubs import generate_uuid1_int from BreCal.schemas.model import Participant +from BreCal.database.enums import ParticipantType, ParticipantFlag + def get_participant_simple(): participant_id = generate_uuid1_int() @@ -10,8 +12,8 @@ def get_participant_simple(): street = "Musterstrasse 1" postal_code = "12345" city = "Bremen" - role_type = 1 # integer - flags = 0 # integer. unclear + role_type = ParticipantType.BSMD + flags = ParticipantFlag.UNDEFINED # integer. unclear created = datetime.datetime.now() modified = created+datetime.timedelta(seconds=10) diff --git a/src/server/BreCal/stubs/shipcall.py b/src/server/BreCal/stubs/shipcall.py index c76a8e9..faba781 100644 --- a/src/server/BreCal/stubs/shipcall.py +++ b/src/server/BreCal/stubs/shipcall.py @@ -2,6 +2,7 @@ import datetime from BreCal.stubs import generate_uuid1_int from BreCal.schemas.model import Shipcall from dataclasses import field +from BreCal.database.enums import ShipcallType, StatusFlags def get_shipcall_simple(): # only used for the stub @@ -11,7 +12,7 @@ def get_shipcall_simple(): ship_id = generate_uuid1_int() eta = base_time+datetime.timedelta(hours=3, minutes=12) - role_type = 1 + role_type = ShipcallType.INCOMING voyage = "987654321" etd = base_time+datetime.timedelta(hours=6, minutes=12) # should never be before eta @@ -39,7 +40,7 @@ def get_shipcall_simple(): moored_lock = False # de: 'Festmacherschleuse', en: 'moored lock' canceled = False - evaluation = None + evaluation = StatusFlags.UNDEFINED evaluation_message = "" created = datetime.datetime.now() modified = created+datetime.timedelta(seconds=10) diff --git a/src/server/BreCal/validators/validation_rules.py b/src/server/BreCal/validators/validation_rules.py index 2753446..a3bb07b 100644 --- a/src/server/BreCal/validators/validation_rules.py +++ b/src/server/BreCal/validators/validation_rules.py @@ -122,7 +122,7 @@ class ValidationRules(ValidationRuleFunctions): returns bool, whether a notification should be triggered """ # state_old is always considered at least 'Green' (1) - state_old = max(copy.copy(self.notification_state) if "notification_state" in list(self.__dict__.keys()) else StatusFlags.NONE, StatusFlags.GREEN.value) + state_old = max(copy.copy(self.notification_state) if "notification_state" in list(self.__dict__.keys()) else StatusFlags.UNDEFINED, StatusFlags.GREEN.value) return state_new.value > state_old.value def undefined_method(self) -> str: diff --git a/src/server/tests/validators/test_validation_rule_state.py b/src/server/tests/validators/test_validation_rule_state.py index 4ed9b2c..70c4db7 100644 --- a/src/server/tests/validators/test_validation_rule_state.py +++ b/src/server/tests/validators/test_validation_rule_state.py @@ -18,7 +18,7 @@ def test_validation_rule_state_order(): # red>yellow>green>none assert StatusFlags.RED.value>StatusFlags.YELLOW.value assert StatusFlags.YELLOW.value>StatusFlags.GREEN.value - assert StatusFlags.GREEN.value>StatusFlags.NONE.value + assert StatusFlags.GREEN.value>StatusFlags.UNDEFINED.value return if __name__=="__main__":