Merge with addons/trunk revno 8168.
[odoo/odoo.git] / addons / document_page / wizard / wiki_make_index.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 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
22 from osv import fields, osv
23 from tools.translate import _
24
25 class wiki_make_index(osv.osv_memory):
26     """ Create Index For Selected Page """
27
28     _name = "wiki.make.index"
29     _description = "Create Index"
30
31     def wiki_do_index(self, cr, uid, ids, context=None):
32
33         """ Makes Index according to page hierarchy
34         @param cr: the current row, from the database cursor,
35         @param uid: the current user’s ID for security checks,
36         @param ids: list of wiki index’s IDs
37
38         """
39         if context is None:
40             context = {}
41         data = context and context.get('active_ids', []) or []
42         
43         if not data:
44             return {'type':  'ir.actions.act_window_close'}
45         
46         for index_obj in self.browse(cr, uid, ids, context=context):
47             wiki_pool = self.pool.get('wiki.wiki')
48             cr.execute("Select id, section from wiki_wiki where id IN %s \
49                             order by section ", (tuple(data),))
50             lst0 = cr.fetchall()
51             if not lst0[0][1]:
52                 raise osv.except_osv(_('Warning !'), _('There is no section in this Page'))
53
54             lst = []
55             s_ids = {}
56
57             for l in lst0:
58                 s_ids[l[1]] = l[0]
59                 lst.append(l[1])
60
61             lst.sort()
62             val = None
63             def toint(x):
64                 try:
65                     return int(x)
66                 except:
67                     return 1
68
69             lst = map(lambda x: map(toint, x.split('.')), lst)
70
71             result = []
72             current = ['0']
73             current2 = []
74
75             for l in lst:
76                 for pos in range(len(l)):
77                     if pos >= len(current):
78                         current.append('1')
79                         continue
80                     if (pos == len(l) - 1) or (pos >= len(current2)) or (toint(l[pos]) > toint(current2[pos])):
81                         current[pos] = str(toint(current[pos]) + 1)
82                         current = current[:pos + 1]
83                         if pos == len(l) - 1:
84                             break
85                 key = ('.'.join([str(x) for x in l]))
86                 id = s_ids[key]
87                 val = ('.'.join([str(x) for x in current[:]]), id)
88
89             if val:
90                 result.append(val)
91             current2 = l
92
93             for rs in result:
94                 wiki_pool.write(cr, uid, [rs[1]], {'section':rs[0]})
95
96         return {'type':  'ir.actions.act_window_close'}
97
98 wiki_make_index()
99
100 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: