Skip to content

Commit 30905a3

Browse files
committed
Enhance Bulletin serialization to include dynamic field values, also implement fail-safe error handling during merging to guarantee a json response
1 parent 53ea834 commit 30905a3

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

enferno/admin/models/Bulletin.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from enferno.admin.models.Location import Location
2121
from enferno.admin.models.GeoLocation import GeoLocation
2222
from enferno.admin.models.Event import Event
23+
from enferno.admin.models.DynamicField import DynamicField
2324

2425

2526
from enferno.admin.models.tables import (
@@ -690,7 +691,7 @@ def to_dict(self, mode: Optional[str] = None) -> dict[str, Any]:
690691
if mode == "1":
691692
return self.min_json()
692693

693-
# Get base dictionary with dynamic fields
694+
# Get base dictionary
694695
data = super().to_dict()
695696

696697
# locations json
@@ -753,7 +754,7 @@ def to_dict(self, mode: Optional[str] = None) -> dict[str, Any]:
753754
for relation in self.incident_relations:
754755
incident_relations_dict.append(relation.to_dict())
755756

756-
# Update with bulletin-specific fields
757+
# Update with bulletin-specific core fields
757758
data.update(
758759
{
759760
"class": self.__tablename__,
@@ -794,6 +795,15 @@ def to_dict(self, mode: Optional[str] = None) -> dict[str, Any]:
794795
}
795796
)
796797

798+
# Merge dynamic fields (non-core) into the response
799+
try:
800+
dynamic_values = DynamicField.extract_values_for(self)
801+
if dynamic_values:
802+
data.update(dynamic_values)
803+
except Exception:
804+
# Fail-safe: never break serialization because of dynamic fields
805+
logger.error("Failed to merge dynamic fields for Bulletin %s", self.id, exc_info=True)
806+
797807
return data
798808

799809
# custom serialization mode

0 commit comments

Comments
 (0)