server/python/django2/renkanmanager/models.py
changeset 626 112912309726
parent 621 192ce5938726
child 662 df0060476f35
--- a/server/python/django2/renkanmanager/models.py	Wed Jun 29 22:48:10 2016 +0200
+++ b/server/python/django2/renkanmanager/models.py	Wed Jun 29 23:11:56 2016 +0200
@@ -16,6 +16,39 @@
 logger = logging.getLogger(__name__)
 auth_user_model = getattr(settings, 'AUTH_USER_MODEL', 'auth.User')
 
+# Copy the content of a révision.
+# This will return a map (not a serialized) to allow further treatment
+# This changes the ids of the project, nodes, views and edges but NOT users
+#
+def content_copy(content_str): #TODO: ??? Extract this in another class, like a RevisionManager ???
+    node_ids_map = {}
+    content = json.loads(content_str)
+
+    content['id'] = str(uuid.uuid4())
+
+    #nodes
+    for node in content.get('nodes', []):
+        id_key = 'id' if 'id' in node else '_id'
+        node_ids_map[node[id_key]] = node['id'] = str(uuid.uuid4())
+        node.pop('_id', None)
+
+    for edge in content.get('edges', []):
+        edge['id'] = str(uuid.uuid4())
+        edge.pop('_id', None)
+        edge['from'] = node_ids_map[edge['from']]
+        edge['to'] = node_ids_map[edge['to']]
+
+    for view in content.get('views', []):
+        view['id'] = str(uuid.uuid4())
+        view.pop('_id', None)
+        view['hidden_nodes'] = [ node_ids_map[n] for n in view.get('hidden_nodes', [])]
+
+    for user in content.get('users', []):
+        if '_id' in user:
+            user['id'] = user.pop('_id')
+
+    return content
+
 class Workspace(models.Model):
 
     workspace_guid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True, blank=False, null=False)
@@ -60,7 +93,8 @@
         initial_revision.creator = creator
         initial_revision.last_updated_by = creator
         if new_renkan_content:
-            new_renkan_content_dict = json.loads(new_renkan.validate_json_content(new_renkan_content))
+            new_renkan_content_dict = content_copy(new_renkan.validate_json_content(new_renkan_content))
+            new_renkan_content_dict["id"] = str(new_renkan.renkan_guid)
             new_renkan_content_dict["created"] = str(creation_date)
             new_renkan_content_dict["updated"] = str(creation_date)
         else: