From fbda5e94e81247be0dabe5f5a0dac44376427b4e Mon Sep 17 00:00:00 2001 From: Tomeu Vizoso Date: Thu, 29 Jan 2009 12:59:12 +0000 Subject: Serialize descendants --- diff --git a/model.py b/model.py index f15758b..5ea84f2 100644 --- a/model.py +++ b/model.py @@ -42,30 +42,41 @@ class MindMapModel(gtk.TreeStore): self._next_thought_id += 1 return thought_id - def serialize(self): + def _serialize_thoughts(self, rows): thoughts = [] - for row in self: - logging.debug('serialize %r' % row[0]) + for row in rows: thought_dict = {} + thoughts.append(thought_dict) + thought_dict['id'] = row[0] thought_dict['name'] = row[1] thought_dict['x'] = row[2] thought_dict['y'] = row[3] thought_dict['color'] = row[4] - thoughts.append(thought_dict) + + children = self._serialize_thoughts(row.iterchildren()) + thought_dict['children'] = children + + return thoughts - return cjson.encode({'thoughts': thoughts}) + def serialize(self): + return cjson.encode({'thoughts': self._serialize_thoughts(self)}) def unserialize(self, data): thoughts = cjson.decode(data)['thoughts'] + self._unserialize_thoughts(parent=None, thoughts=thoughts) + + def _unserialize_thoughts(self, parent, thoughts): for thought_dict in thoughts: self._next_thought_id = max(self._next_thought_id + 1, thought_dict['id'] + 1) - self.append(None, (thought_dict['id'], - thought_dict.get('name', None), - thought_dict.get('x', None), - thought_dict.get('y', None), - thought_dict.get('color', None))) + new_row = self.append(parent, (thought_dict['id'], + thought_dict.get('name', None), + thought_dict.get('x', None), + thought_dict.get('y', None), + thought_dict.get('color', None))) + children = thought_dict.get('children', []) + self._unserialize_thoughts(parent=new_row, thoughts=children) def find_by_id(self, thought_id, rows=None): if rows is None: -- cgit v0.9.1