diff --git a/thermal_events/schemas.py b/thermal_events/schemas.py index 5b69459..62a9b4b 100644 --- a/thermal_events/schemas.py +++ b/thermal_events/schemas.py @@ -3,6 +3,7 @@ from marshmallow import fields from .thermal_event_instance import ThermalEventInstance from .thermal_event import ThermalEvent +from .strike_line_descriptor import StrikeLineDescriptor class ThermalEventInstanceSchema(SQLAlchemyAutoSchema): @@ -31,3 +32,14 @@ class Meta: include_fk = True load_instance = True transient = True + + +class StrikeLineDescriptorSchema(SQLAlchemyAutoSchema): + """Schema for the StrikeLineDescriptor model.""" + + class Meta: + model = StrikeLineDescriptor + include_relationships = True + include_fk = True + load_instance = True + transient = True diff --git a/thermal_events/strike_line_descriptor.py b/thermal_events/strike_line_descriptor.py index 18626db..e3f010e 100644 --- a/thermal_events/strike_line_descriptor.py +++ b/thermal_events/strike_line_descriptor.py @@ -1,3 +1,4 @@ +from copy import deepcopy from sqlalchemy import ( Boolean, Column, @@ -6,7 +7,7 @@ ) from sqlalchemy.dialects import sqlite from sqlalchemy.dialects.mysql import DOUBLE -from sqlalchemy.orm import relationship +from sqlalchemy.orm import make_transient, relationship from .base import Base from .thermal_event_instance import ( @@ -107,6 +108,14 @@ def __init__( else: setattr(self, key, value) + def to_dict(self): + from .schemas import StrikeLineDescriptorSchema + + out = deepcopy(self) + make_transient(out) + make_transient(out.instance) + return {"0": StrikeLineDescriptorSchema().dump(out)} + def return_segmented_points(self) -> list: """Return the segmented points as a list. diff --git a/thermal_events/thermal_event.py b/thermal_events/thermal_event.py index 7b874a4..43e2962 100644 --- a/thermal_events/thermal_event.py +++ b/thermal_events/thermal_event.py @@ -321,13 +321,17 @@ def compute(self) -> None: self._computed = True - def to_dict(self): + def to_dict(self, use_id_as_key=False): from .schemas import ThermalEventSchema out = deepcopy(self) make_transient(out) [make_transient(x) for x in out.instances] - return {"0": ThermalEventSchema().dump(out)} + if use_id_as_key: + key = str(self.id) + else: + key = "0" + return {key: ThermalEventSchema().dump(out)} def to_json(self, path_to_file): """ diff --git a/thermal_events/thermal_event_instance.py b/thermal_events/thermal_event_instance.py index 68604d8..a5b751b 100644 --- a/thermal_events/thermal_event_instance.py +++ b/thermal_events/thermal_event_instance.py @@ -259,6 +259,7 @@ class ThermalEventInstance(Base): "StrikeLineDescriptor", back_populates="instance", cascade="all, delete, delete-orphan", + uselist=False, ) _buffer = None # image bugger used to draw polygons