Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomeu Vizoso <tomeu@sugarlabs.org>2009-01-29 12:59:12 (GMT)
committer Tomeu Vizoso <tomeu@sugarlabs.org>2009-01-29 12:59:12 (GMT)
commitfbda5e94e81247be0dabe5f5a0dac44376427b4e (patch)
tree91f793400b3b300335ea4103e7b1b17d56b57f07
parent05a816a38fe398e9b80712764bd4946df23eb4d0 (diff)
Serialize descendants
-rw-r--r--model.py31
1 files changed, 21 insertions, 10 deletions
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: