Skip to content

Commit

Permalink
uses serialized graph in tile model (#9753)
Browse files Browse the repository at this point in the history
* uses serialized graph in tile model

* be more specific about caught error types

* Update arches/app/models/tile.py

Update error handling for missing serialized graph.

Co-authored-by: Jacob Walls <[email protected]>

* Adds more explicit catch in the case of missing serialized graph

* Don't retrieve serialized graph until it's actually required

* Shrinks query set for nodes

* Don't refetch nodegroup

---------

Co-authored-by: Jacob Walls <[email protected]>
  • Loading branch information
aarongundel and jacobtylerwalls authored Sep 20, 2023
1 parent e288099 commit 22332fb
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions arches/app/models/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,13 @@ def __init__(self, *args, **kwargs):
tile = Tile(tile_obj)
tile.parenttile = self
self.tiles.append(tile)
self.load_serialized_graph()

def load_serialized_graph(self):
try:
published_graph = self.resourceinstance.graph.get_published_graph()
self.serialized_graph = published_graph.serialized_graph
except:
except Exception as e:
self.serialized_graph = None

def save_edit(
Expand Down Expand Up @@ -330,7 +333,10 @@ def validate(self, errors=None, raise_early=True, strict=False, request=None):

tile_errors = []
for nodeid, value in self.data.items():
node = models.Node.objects.get(nodeid=nodeid)
try:
node = SimpleNamespace(**next((x for x in self.serialized_graph["nodes"] if x["nodeid"] == nodeid), None))
except TypeError: # will catch if serialized_graph is None
node = models.Node.objects.get(nodeid=nodeid)
datatype = self.datatype_factory.get_instance(node.datatype)
error = datatype.validate(value, node=node, strict=strict, request=request)
tile_errors += error
Expand Down Expand Up @@ -392,8 +398,7 @@ def save(self, *args, **kwargs):
oldprovisionalvalue = None

if not self.serialized_graph:
published_graph = self.resourceinstance.graph.get_published_graph()
self.serialized_graph = published_graph.serialized_graph
self.load_serialized_graph()
try:
if user is None and request is not None:
user = request.user
Expand Down Expand Up @@ -525,7 +530,10 @@ def delete(self, *args, **kwargs):
try:
super(Tile, self).delete(*args, **kwargs)
for nodeid in self.data.keys():
node = models.Node.objects.get(nodeid=nodeid)
try:
node = SimpleNamespace(**next((x for x in self.serialized_graph["nodes"] if x["nodeid"] == nodeid), None))
except TypeError: #will catch if serialized_graph is None
node = models.Node.objects.get(nodeid=nodeid)
datatype = self.datatype_factory.get_instance(node.datatype)
datatype.post_tile_delete(self, nodeid, index=index)
if index:
Expand Down Expand Up @@ -557,8 +565,13 @@ def flatten_tiles(obj):
return tiles

def after_update_all(self):
nodegroup = models.NodeGroup.objects.get(pk=self.nodegroup_id)
for node in nodegroup.node_set.all():
try:
nodes = [(node for node in self.serialized_graph["nodes"] if node["nodegroup_id"] == self.nodegroup_id)]
nodes = [SimpleNamespace(**next(node, None)) for node in nodes]
except TypeError: # handle if serialized_graph is None
nodes = self.nodegroup.node_set.all()

for node in nodes:
datatype = self.datatype_factory.get_instance(node.datatype)
datatype.after_update_all(tile=self)
for tile in self.tiles:
Expand Down Expand Up @@ -598,10 +611,10 @@ def get_blank_tile_from_nodegroup_id(nodegroup_id, resourceid=None, parenttile=N
tile.resourceinstance_id = resourceid
tile.parenttile = parenttile
tile.data = {}
nodes = models.Node.objects.filter(nodegroup=nodegroup_id).exclude(datatype="semantic")

for node in models.Node.objects.filter(nodegroup=nodegroup_id):
if node.datatype != "semantic":
tile.data[str(node.nodeid)] = None
for node in nodes:
tile.data[str(node.nodeid)] = None

return tile

Expand Down

0 comments on commit 22332fb

Please sign in to comment.