[FIX] Thread widget: cleaned function calls (using this.proxy) to avoid asynchronous...
[odoo/odoo.git] / addons / base_module_doc_rst / base_module_doc_rst.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 ##############################################################################
21 import os
22 import pydot
23 import base64
24
25 import report
26 from osv import fields, osv
27 import addons
28
29 class module(osv.osv):
30     _inherit = 'ir.module.module'
31     _description = 'Module With Relationship Graph'
32     _columns = {
33         'file_graph': fields.binary('Relationship Graph'),
34             }
35
36     def _get_graphical_representation(self, cr, uid, model_ids, level=1, context=None):
37         obj_model = self.pool.get('ir.model')
38         if level == 0:
39             return tuple()
40         relation = []
41         for id in model_ids:
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)
46                 if new_model_ids:
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)
50
51     def _get_structure(self, relations, main_element):
52         res = {}
53         for rel in relations:
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())
57         val = []
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 "",
61                                                        obj,
62                                                        "|".join(["<%s> %s" % (fn, fn) for fn in fields])))
63         return "\n".join(val)
64
65     def _get_arrow(self, field_type='many2one'):
66         return {
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"',
70         }[field_type]
71
72     def get_graphical_representation(self, cr, uid, model_ids, context=None):
73         obj_model = self.pool.get('ir.model')
74         if context is None:
75             context = {}
76         res = {}
77         models = []
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),
84             )
85         return res
86
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')
90         obj_ids = []
91         model_data_ids = obj_mod_data.search(cr, uid, [('module', '=', module), ('model', '=', 'ir.model')], context=context)
92         model_ids = []
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)
97         return obj_ids
98
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)
102         if not object_ids:
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")
112            fp.write(val)
113            fp.close()
114         os.popen('dot -Tpng' +' '+ path_dotfile + ' '+ '-o' +' ' + path_png)
115         fp = file(path_png, "r")
116         x = fp.read()
117         fp.close()
118         os.popen('rm ' + path_dotfile + ' ' + path_png)
119         return {'module_file': base64.encodestring(x)}
120
121 module()
122
123
124 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
125