Skip to content

Commit

Permalink
Make StructuredRels JSON serializable
Browse files Browse the repository at this point in the history
  • Loading branch information
DMalone87 committed Oct 30, 2024
1 parent fb26193 commit 3bed2b7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion backend/database/models/agency.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Jurisdiction(str, PropertyEnum):
OTHER = "OTHER"


class UnitMembership(StructuredRel):
class UnitMembership(StructuredRel, JsonSerializable):
earliest_date = DateProperty()
latest_date = DateProperty()
badge_number = StringProperty()
Expand Down
2 changes: 1 addition & 1 deletion backend/database/models/complaint.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class RecordType(str, PropertyEnum):


# Neo4j Models
class BaseSourceRel(StructuredRel):
class BaseSourceRel(StructuredRel, JsonSerializable):
record_type = StringProperty(
choices=RecordType.choices(),
required=True
Expand Down
2 changes: 1 addition & 1 deletion backend/database/models/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def serialize(self):
}


class SourceMember(StructuredRel):
class SourceMember(StructuredRel, JsonSerializable):
def __init__(self, **kwargs):
super().__init__(**kwargs)

Expand Down
30 changes: 15 additions & 15 deletions backend/schemas.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations
import math
import json
import datetime
import textwrap
from functools import wraps
from enum import Enum
Expand All @@ -14,9 +15,8 @@
RelationshipTo,
RelationshipFrom, Relationship,
RelationshipManager, RelationshipDefinition,
UniqueIdProperty, StructuredRel
UniqueIdProperty, StructuredRel, StructuredNode
)
from neomodel.properties import Property
from neomodel.exceptions import DoesNotExist


Expand Down Expand Up @@ -238,24 +238,24 @@ def to_dict(self, include_relationships=True,
getattr(self, '__hidden_properties__', [])).union(
set(exclude_fields))

all_props = self.defined_properties()
node_props = OrderedDict()
all_props = self.defined_properties(aliases=False, rels=False)
obj_props = OrderedDict()

if field_order:
ordered_props = [prop for prop in field_order if prop in all_props]
else:
ordered_props = list(all_props.keys())

# Serialize node properties
# Serialize properties
for prop_name in ordered_props:
if prop_name not in all_excludes:
prop = all_props[prop_name]
if isinstance(prop, Property):
value = getattr(self, prop_name, None)
node_props[prop_name] = value
value = getattr(self, prop_name, None)
if isinstance(value, (datetime.datetime, datetime.date)):
value = value.isoformat()
obj_props[prop_name] = value

# Optionally add related nodes
if include_relationships:
if include_relationships and isinstance(self, StructuredNode):
relationships = {
key: value for key, value in self.__class__.__dict__.items()
if isinstance(value, RelationshipDefinition)
Expand All @@ -270,7 +270,7 @@ def to_dict(self, include_relationships=True,
# Limit the number of related nodes to serialize
if relationship_def.definition.get('model', None):
# If there is a relationship model, serialize it as well
node_props[key] = [
obj_props[key] = [
{
'node': node.to_dict(
include_relationships=False),
Expand All @@ -283,12 +283,12 @@ def to_dict(self, include_relationships=True,
]
else:
# No specific relationship model, just serialize nodes
node_props[key] = [
obj_props[key] = [
node.to_dict(include_relationships=False)
for node in related_nodes
]

return node_props
return obj_props

def to_json(self):
"""Convert the node instance into a JSON string."""
Expand Down Expand Up @@ -383,13 +383,13 @@ def from_dict(cls: Type[T], data: dict, uid=None) -> T:
return instance

@classmethod
def __all_properties__(cls) -> List[str]:
def __all_properties_JS__(cls) -> List[str]:
"""Get a list of all properties defined in the class."""
return [prop_name for prop_name in cls.__dict__ if isinstance(
cls.__dict__[prop_name], property)]

@classmethod
def __all_relationships__(cls) -> dict:
def __all_relationships_JS__(cls) -> dict:
"""Get all relationships defined in the class."""
return {
rel_name: rel_manager
Expand Down

0 comments on commit 3bed2b7

Please sign in to comment.