1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
26 from osv import fields, osv
29 class module(osv.osv):
30 _inherit = 'ir.module.module'
31 _description = 'Module With Relationship Graph'
33 'file_graph': fields.binary('Relationship Graph'),
36 def _get_graphical_representation(self, cr, uid, model_ids, level=1, context=None):
37 obj_model = self.pool.get('ir.model')
42 model_data = obj_model.browse(cr, uid, id, context=context)
43 for field in (f for f in model_data.field_id if f.ttype in ('many2many', 'many2one', 'one2many')):
44 relation.append((model_data.model, field.name, field.ttype, field.relation, field.field_description))
45 new_model_ids = obj_model.search(cr, uid, [('model', '=', field.relation)], context=context)
47 model = obj_model.read(cr, uid, new_model_ids, ['id', 'name'], context=context)[0]
48 relation.extend(self._get_graphical_representation(cr, uid, model['id'], level - 1))
49 return tuple(relation)
51 def _get_structure(self, relations, main_element):
54 # if we have to display the string along with field name then uncomment the first line n comment the second line
55 res.setdefault(rel[0], set()).add(rel[1])
56 res.setdefault(rel[3], set())
58 for obj, fields in res.items():
59 val.append('"%s" [%s label="{<id>%s|%s}"];' % (obj,
60 obj in main_element and 'fillcolor=yellow, style="filled,rounded"' or "",
62 "|".join(["<%s> %s" % (fn, fn) for fn in fields])))
65 def _get_arrow(self, field_type='many2one'):
67 'many2one': 'arrowtail="none" arrowhead="normal" color="red" label="m2o"',
68 'many2many': 'arrowtail="crow" arrowhead="crow" color="green" label="m2m"',
69 'one2many': 'arrowtail="none" arrowhead="crow" color="blue" label="o2m"',
72 def get_graphical_representation(self, cr, uid, model_ids, context=None):
73 obj_model = self.pool.get('ir.model')
78 for obj in obj_model.browse(cr, uid, model_ids, context=context):
79 models.append(obj.model)
80 relations = set(self._get_graphical_representation(cr, uid, model_ids, context.get('level', 1)))
81 res[obj.model] = "digraph G {\nnode [style=rounded, shape=record];\n%s\n%s }" % (
82 self._get_structure(relations, models),
83 ''.join('"%s":%s -> "%s":id:n [%s]; // %s\n' % (m, fn, fr, self._get_arrow(ft),ft) for m, fn, ft, fr, fl in relations),
87 def _get_module_objects(self, cr, uid, module, context=None):
88 obj_model = self.pool.get('ir.model')
89 obj_mod_data = self.pool.get('ir.model.data')
91 model_data_ids = obj_mod_data.search(cr, uid, [('module', '=', module), ('model', '=', 'ir.model')], context=context)
93 for mod in obj_mod_data.browse(cr, uid, model_data_ids, context=context):
94 model_ids.append(mod.res_id)
95 models = obj_model.browse(cr, uid, model_ids, context=context)
96 map(lambda x: obj_ids.append(x.id),models)
99 def get_relation_graph(self, cr, uid, module_name, context=None):
100 if context is None: context = {}
101 object_ids = self._get_module_objects(cr, uid, module_name, context=context)
103 return {'module_file': False}
104 context.update({'level': 1})
105 dots = self.get_graphical_representation(cr, uid, object_ids, context=context)
106 # todo: use os.realpath
107 file_path = addons.get_module_resource('base_module_doc_rst')
108 path_png = file_path + "/module.png"
109 for key, val in dots.items():
110 path_dotfile = file_path + "/%s.dot" % (key,)
111 fp = file(path_dotfile, "w")
114 os.popen('dot -Tpng' +' '+ path_dotfile + ' '+ '-o' +' ' + path_png)
115 fp = file(path_png, "r")
118 os.popen('rm ' + path_dotfile + ' ' + path_png)
119 return {'module_file': base64.encodestring(x)}
124 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: