fixed enum serialization by using custom optional to_json() method
This commit is contained in:
parent
e88f3fa1de
commit
63b815c274
@ -17,7 +17,7 @@ def GetNotifications(options):
|
|||||||
pooledConnection = local_db.getPoolConnection()
|
pooledConnection = local_db.getPoolConnection()
|
||||||
commands = pydapper.using(pooledConnection)
|
commands = pydapper.using(pooledConnection)
|
||||||
data = commands.query("SELECT id, shipcall_id, level, type, message, created, modified FROM notification " +
|
data = commands.query("SELECT id, shipcall_id, level, type, message, created, modified FROM notification " +
|
||||||
"WHERE shipcall_id = ?scid?", model=model.Notification, param={"scid" : options["shipcall_id"]})
|
"WHERE shipcall_id = ?scid?", model=model.Notification.from_query_row, param={"scid" : options["shipcall_id"]})
|
||||||
pooledConnection.close()
|
pooledConnection.close()
|
||||||
|
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
|
|||||||
@ -33,7 +33,7 @@ def GetShipcalls(options):
|
|||||||
"(etd >= DATE(NOW() - INTERVAL %d DAY)))) " +
|
"(etd >= DATE(NOW() - INTERVAL %d DAY)))) " +
|
||||||
"ORDER BY eta") % (options["past_days"], options["past_days"], options["past_days"], options["past_days"])
|
"ORDER BY eta") % (options["past_days"], options["past_days"], options["past_days"], options["past_days"])
|
||||||
|
|
||||||
data = commands.query(query, model=model.Shipcall)
|
data = commands.query(query, model=model.Shipcall.from_query_row, buffered=True)
|
||||||
for shipcall in data:
|
for shipcall in data:
|
||||||
participant_query = "SELECT participant_id, type FROM shipcall_participant_map WHERE shipcall_id=?shipcall_id?";
|
participant_query = "SELECT participant_id, type FROM shipcall_participant_map WHERE shipcall_id=?shipcall_id?";
|
||||||
for record in commands.query(participant_query, model=dict, param={"shipcall_id" : shipcall.id}, buffered=False):
|
for record in commands.query(participant_query, model=dict, param={"shipcall_id" : shipcall.id}, buffered=False):
|
||||||
|
|||||||
@ -13,6 +13,8 @@ import datetime
|
|||||||
def obj_dict(obj):
|
def obj_dict(obj):
|
||||||
if isinstance(obj, datetime.datetime):
|
if isinstance(obj, datetime.datetime):
|
||||||
return obj.isoformat()
|
return obj.isoformat()
|
||||||
|
if hasattr(obj, 'to_json'):
|
||||||
|
return obj.to_json()
|
||||||
return obj.__dict__
|
return obj.__dict__
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
@ -26,7 +28,7 @@ class Berth(Schema):
|
|||||||
modified: datetime
|
modified: datetime
|
||||||
deleted: bool
|
deleted: bool
|
||||||
|
|
||||||
class Operation(IntEnum):
|
class OperationType(IntEnum):
|
||||||
undefined = 0
|
undefined = 0
|
||||||
insert = 1
|
insert = 1
|
||||||
update = 2
|
update = 2
|
||||||
@ -54,6 +56,7 @@ class ShipcallType(IntEnum):
|
|||||||
departure = 2
|
departure = 2
|
||||||
shifting = 3
|
shifting = 3
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class History:
|
class History:
|
||||||
def __init__(self, id, participant_id, shipcall_id, timestamp, eta, type, operation):
|
def __init__(self, id, participant_id, shipcall_id, timestamp, eta, type, operation):
|
||||||
@ -72,10 +75,22 @@ class History:
|
|||||||
timestamp: datetime
|
timestamp: datetime
|
||||||
eta: datetime
|
eta: datetime
|
||||||
type: ObjectType
|
type: ObjectType
|
||||||
operation: Operation
|
operation: OperationType
|
||||||
|
|
||||||
|
def to_json(self):
|
||||||
|
return {
|
||||||
|
"id": self.id,
|
||||||
|
"participant_id": self.participant_id,
|
||||||
|
"shipcall_id": self.shipcall_id,
|
||||||
|
"timestamp": self.timestamp.isoformat() if self.timestamp else "",
|
||||||
|
"eta": self.eta.isoformat() if self.eta else "",
|
||||||
|
"type": self.type.name,
|
||||||
|
"operation": self.operation.name
|
||||||
|
}
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_query_row(self, id, participant_id, shipcall_id, timestamp, eta, type, operation):
|
def from_query_row(self, id, participant_id, shipcall_id, timestamp, eta, type, operation):
|
||||||
return self(id, participant_id, shipcall_id, timestamp, eta, ObjectType(type), Operation(operation))
|
return self(id, participant_id, shipcall_id, timestamp, eta, ObjectType(type), OperationType(operation))
|
||||||
|
|
||||||
class Error(Schema):
|
class Error(Schema):
|
||||||
message = fields.String(required=True)
|
message = fields.String(required=True)
|
||||||
@ -94,6 +109,21 @@ class Notification:
|
|||||||
created: datetime
|
created: datetime
|
||||||
modified: datetime
|
modified: datetime
|
||||||
|
|
||||||
|
def to_json(self):
|
||||||
|
return {
|
||||||
|
"id": self.id,
|
||||||
|
"shipcall_id": self.shipcall_id,
|
||||||
|
"level": self.level,
|
||||||
|
"type": self.type.name,
|
||||||
|
"message": self.message,
|
||||||
|
"created": self.created.isoformat() if self.created else "",
|
||||||
|
"modified": self.modified.isoformat() if self.modified else ""
|
||||||
|
}
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_query_row(self, id, shipcall_id, level, type, message, created, modified):
|
||||||
|
return self(id, shipcall_id, level, NotificationType(type), message, created, modified)
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Participant(Schema):
|
class Participant(Schema):
|
||||||
id: int
|
id: int
|
||||||
@ -166,7 +196,7 @@ class Shipcall:
|
|||||||
|
|
||||||
id: int
|
id: int
|
||||||
ship_id: int
|
ship_id: int
|
||||||
type: str
|
type: ShipcallType
|
||||||
eta: datetime
|
eta: datetime
|
||||||
voyage: str
|
voyage: str
|
||||||
etd: datetime
|
etd: datetime
|
||||||
@ -187,7 +217,7 @@ class Shipcall:
|
|||||||
anchored: bool
|
anchored: bool
|
||||||
moored_lock: bool
|
moored_lock: bool
|
||||||
canceled: bool
|
canceled: bool
|
||||||
evaluation: str
|
evaluation: EvaluationType
|
||||||
evaluation_message: str
|
evaluation_message: str
|
||||||
evaluation_time: datetime
|
evaluation_time: datetime
|
||||||
evaluation_notifications_sent: bool
|
evaluation_notifications_sent: bool
|
||||||
@ -195,6 +225,46 @@ class Shipcall:
|
|||||||
modified: datetime
|
modified: datetime
|
||||||
participants: List[Participant_Assignment] = field(default_factory=list)
|
participants: List[Participant_Assignment] = field(default_factory=list)
|
||||||
|
|
||||||
|
def to_json(self):
|
||||||
|
return {
|
||||||
|
"id": self.id,
|
||||||
|
"ship_id": self.ship_id,
|
||||||
|
"type": self.type.name,
|
||||||
|
"eta": self.eta.isoformat() if self.eta else "",
|
||||||
|
"voyage": self.voyage,
|
||||||
|
"etd": self.etd.isoformat() if self.etd else "",
|
||||||
|
"arrival_berth_id": self.arrival_berth_id,
|
||||||
|
"departure_berth_id": self.departure_berth_id,
|
||||||
|
"tug_required": self.tug_required,
|
||||||
|
"pilot_required": self.pilot_required,
|
||||||
|
"flags": self.flags,
|
||||||
|
"pier_side": self.pier_side,
|
||||||
|
"bunkering": self.bunkering,
|
||||||
|
"replenishing_terminal": self.replenishing_terminal,
|
||||||
|
"replenishing_lock": self.replenishing_lock,
|
||||||
|
"draft": self.draft,
|
||||||
|
"tidal_window_from": self.tidal_window_from.isoformat() if self.tidal_window_from else "",
|
||||||
|
"tidal_window_to": self.tidal_window_to.isoformat() if self.tidal_window_to else "",
|
||||||
|
"rain_sensitive_cargo": self.rain_sensitive_cargo,
|
||||||
|
"recommended_tugs": self.recommended_tugs,
|
||||||
|
"anchored": self.anchored,
|
||||||
|
"moored_lock": self.moored_lock,
|
||||||
|
"canceled": self.canceled,
|
||||||
|
"evaluation": self.evaluation.name,
|
||||||
|
"evaluation_message": self.evaluation_message,
|
||||||
|
"evaluation_time": self.evaluation_time.isoformat() if self.evaluation_time else "",
|
||||||
|
"evaluation_notifications_sent": self.evaluation_notifications_sent,
|
||||||
|
"created": self.created.isoformat() if self.created else "",
|
||||||
|
"modified": self.modified.isoformat() if self.modified else "",
|
||||||
|
"participants": [participant.__dict__ for participant in self.participants]
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_query_row(self, id, ship_id, type, eta, voyage, etd, arrival_berth_id, departure_berth_id, tug_required, pilot_required, flags, pier_side, bunkering, replenishing_terminal, replenishing_lock, draft, tidal_window_from, tidal_window_to, rain_sensitive_cargo, recommended_tugs, anchored, moored_lock, canceled, evaluation, evaluation_message, evaluation_time, evaluation_notifications_sent, created, modified):
|
||||||
|
return self(id, ship_id, ShipcallType(type), eta, voyage, etd, arrival_berth_id, departure_berth_id, tug_required, pilot_required, flags, pier_side, bunkering, replenishing_terminal, replenishing_lock, draft, tidal_window_from, tidal_window_to, rain_sensitive_cargo, recommended_tugs, anchored, moored_lock, canceled, EvaluationType(evaluation), evaluation_message, evaluation_time, evaluation_notifications_sent, created, modified)
|
||||||
|
|
||||||
class ShipcallId(Schema):
|
class ShipcallId(Schema):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user