...
 
Commits (3)
......@@ -381,7 +381,7 @@ class MgvGraphTemplate(object):
"""Create the equivalent object in the database."""
self.uuid = mgvWrapper.createNode(self.pattern, self.code, name=self.name, icon=self.icon)
path = ':'.join([self.pattern.getName(), self.getName(), '*template*'])
mgvWrapper.createNode(self, "Graph", name=self.name, path=path, template_name='')
mgvWrapper.createNode(self, "Graph", name=self.name, path=path, template_uuid='')
def getName(self):
"""Return the name of the template."""
......@@ -481,6 +481,14 @@ class MgvPattern(object):
"""Return the pattern attribute of the pattern."""
return self.pattern
def getTemplate(self, template_name=None, template_uuid=None):
for t in self.templates:
if template_name is not None and t.name == template_name:
return t
if template_uuid is not None and t.uuid == template_uuid:
return t
return None
def setPattern(self, pattern):
"""Set the pattern attribute of the pattern."""
self.pattern = pattern
......@@ -551,14 +559,14 @@ class MgvPattern(object):
Parameters:
keys (list of str): path of the graph from the pattern name to its name.
Returns
dict: a dictionary with a uuid value and a template_name value.
dict: a dictionary with a uuid value and a template_uuid value.
"""
return mgvWrapper.getGraphInfo(self, keys)
def setGraphTemplate(self, keys, template_name):
def setGraphTemplate(self, keys, template_uuid):
gid = self.getGraphInfo(keys)['uuid']
mgvWrapper.setNodeAttr({'code': 'Graph', 'uuid': gid, 'project_name': self.project.name},
template_name=template_name)
template_uuid=template_uuid)
def deleteGraph(self, keys):
"""Delete a graph in the database and all its hierarchy."""
......@@ -893,7 +901,7 @@ class MgvGraph(object):
uuid (str): unique id in the database.
name (str): the name of the graph.
pattern (MgvPattern): the pattern of the graph.
template_name (str): the name of the template to use
template_uuid (str): the uuid of the template to use
at first opening.
path (list of str): the keys of the graph.
nodes (list of MgvNode): the nodes of the graph.
......@@ -906,10 +914,10 @@ class MgvGraph(object):
"""
code = "Graph"
def __init__(self, pattern=None, path=None, uuid=None, template_name='', name='', load=True):
def __init__(self, pattern=None, path=None, uuid=None, template_uuid='', name='', load=True):
self.uuid = uuid
self.pattern = pattern
self.template_name = template_name
self.template_uuid = template_uuid
self.path = path if path is not None else []
self.nodes = []
self.groups = []
......@@ -928,7 +936,7 @@ class MgvGraph(object):
d = mgvWrapper.getGraphInfo(self.pattern, self.path)
if 'uuid' in d.keys():
self.uuid = d['uuid']
self.template_name = d['template_name']
self.template_uuid = d['template_uuid']
def __repr__(self):
return "MgvGraph %s" % self.name
......@@ -941,7 +949,7 @@ class MgvGraph(object):
Also create node, group and variable objects if any.
"""
self.uuid = mgvWrapper.createNode(self.pattern, 'Graph', path=':'.join(self.path), name=self.name,
template_name=self.template_name)
template_uuid=self.template_uuid)
for node in self.nodes:
node.create()
for node in self.nodes:
......@@ -973,14 +981,14 @@ class MgvGraph(object):
"""Replace all components of this graph by the templates's."""
if template is None:
for tem in self.pattern.templates:
if tem.name == self.template_name:
if tem.uuid == self.template_uuid:
template = tem
break
else:
return
template_graph = MgvGraph(pattern=template.pattern, name=template.getName(),
path=[template.pattern.getName(), template.getName(), '*template*'])
mgvWrapper.setNodeAttr(self, template_name=template.name)
mgvWrapper.setNodeAttr(self, template_uuid=template.uuid)
self.nodes = [x._dup(self) for x in template_graph.nodes]
self.groups = [x._dup(self) for x in template_graph.groups]
for node in self.nodes:
......@@ -1217,8 +1225,8 @@ class MgvGraph(object):
with open(self.path, 'r') as fid:
root = json.load(fid)
self.uuid = root['uuid']
if 'template_name' in root:
self.template_name = root['template_name']
if 'template_uuid' in root:
self.template_uuid = root['template_uuid']
self._linksToString(graphview)
# on degage de la scene les nodes pas a nous
for node in list(self.nodes):
......@@ -1254,7 +1262,7 @@ class MgvGraph(object):
def getJson(self):
"""Return a dictionary representing the graph."""
return {'code': 'Graph', 'name': self.getName(), 'path': self.path,
'template_name': self.template_name, 'nodes': [x.getJson() for x in self.nodes], 'uuid': str(self.uuid),
'template_uuid': self.template_uuid, 'nodes': [x.getJson() for x in self.nodes], 'uuid': str(self.uuid),
'groups': [x.getJson() for x in self.groups], 'variables': [x.getJson() for x in self.variables]}
@classmethod
......@@ -1262,7 +1270,7 @@ class MgvGraph(object):
"""Create a node from a dictionary."""
element = noBytes(element)
newGraph = cls(uuid=element['uuid'], pattern=pattern, path=[str(x) for x in element['path']],
template_name=element['template_name'],
template_uuid=element['template_uuid'],
name=element['name'], load=False)
for childElement in element['variables']:
MgvVariable.getFromJson(childElement, newGraph)
......
This diff is collapsed.
......@@ -6,6 +6,13 @@ import os
from pymongo import MongoClient
def getTemplateUuidFromName(pattern, name):
for t in pattern.templates:
if t.name == name:
return t.uuid
return None
def isString(var):
return isinstance(var, str if sys.version_info[0] >= 3 else basestring)
......@@ -353,17 +360,20 @@ def getGraphInfo(pattern, keys):
pattern (MgvPattern): the pattern.
keys (list of str): path of the graph from the pattern name to its name.
Returns
dict: a dictionary with a uuid value and a template_name value.
dict: a dictionary with a uuid value and a template_uuid value.
"""
project_name = pattern.project.name
try:
pid = db[project_name].find_one({'code': 'Pattern', 'name': pattern.name, 'parent': pattern.project.uuid},
{"_id": 1})['_id']
g = db[project_name].find_one({'code': 'Graph', 'parent': pid, 'path': ':'.join(keys)},
{'template_name': 1})
return {'code': 'Graph', 'uuid': g['_id'], 'template_name': g['template_name']}
except:
return {'template_name': ''}
pat = db[project_name].find_one({'code': 'Pattern', 'name': pattern.name, 'parent': pattern.project.uuid},
{"_id": 1})
if pat is None:
return {'template_uuid': ''}
g = db[project_name].find_one({'code': 'Graph', 'parent': pat['_id'], 'path': ':'.join(keys)},
{'template_name': 1, 'template_uuid': 1})
if g is None:
return {'template_uuid': ''}
if 'template_uuid' not in g.keys():
g['template_uuid'] = getTemplateUuidFromName(pattern, g['template_name'])
return {'code': 'Graph', 'uuid': g['_id'], 'template_uuid': g['template_uuid']}
def getGraphVars(pattern, keys):
......@@ -446,12 +456,14 @@ def getObjects(graph):
g = db[project_name].find_one({'code': 'Graph', '_id': graph.uuid}, {'template_name': 1})
else:
g = db[project_name].find_one({'code': 'Graph', 'name': graph.getName(), 'path': ':'.join(graph.path),
'parent': graph.pattern.uuid}, {'template_name': 1})
'parent': graph.pattern.uuid}, {'template_name': 1, 'template_uuid': 1})
if g is None:
return None
if 'template_uuid' not in g.keys():
g['template_uuid'] = getTemplateUuidFromName(graph.pattern, g['template_name'])
graphObject['uuid'] = g['_id']
graphObject['template_name'] = g['template_name']
graphObject['template_uuid'] = g['template_uuid']
graphObject['variables'] = []
graphObject['groups'] = []
for data in db[project_name].find({'parent': g['_id']}):
......