[FIX] Schedule jobs even if their next time has passed.
[odoo/odoo.git] / addons / wiki / wizard / make_index.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution    
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 import time
24 import wizard
25 import osv
26 import pooler
27
28 section_form = '''<?xml version="1.0"?>
29 <form string="Create Menu">
30     <separator string="Menu Information" colspan="4"/>
31     <label string="Want to create a Index on Selected Pages ? "/>
32 </form>'''
33
34 def wiki_do_index(self, cr, uid, data, context):
35     ids = data['ids']
36     pool = pooler.get_pool(cr.dbname)
37     wiki_pool = pool.get('wiki.wiki')
38
39     sSQL = "Select id, section from wiki_wiki where id in %s order by section "
40     cr.execute(sSQL, (tuple(ids),))
41     lst0 = cr.fetchall()
42     lst = []
43     ids = {}
44     for l in lst0:
45         ids[l[1]] = l[0]
46         lst.append(l[1])
47
48     lst.sort()
49     val = None
50     def toint(x):
51         try:
52             return int(x)
53         except:
54             return 1
55     
56     lst = map(lambda x: map(toint, x.split('.')), lst)
57     
58     result = []
59     current = ['0']
60     current2 = []
61
62     for l in lst:
63         for pos in range(len(l)):
64             if pos >= len(current):
65                 current.append('1')
66                 continue
67             if (pos == len(l) - 1) or (pos >= len(current2)) or (toint(l[pos]) > toint(current2[pos])):
68                  current[pos] = str(toint(current[pos]) + 1)
69                  current = current[:pos + 1]
70             if pos == len(l) - 1:
71                  break
72              
73         key = ('.'.join([str(x) for x in l]))
74         id = ids[key]
75         val = ('.'.join([str(x) for x in current[:]]), id)
76
77         if val:
78             result.append(val)
79         current2 = l
80     
81     for rs in result:
82         wiki_pool.write(cr, uid, [rs[1]], {'section':rs[0]})
83         
84     return {}
85
86 class make_index(wizard.interface):
87     states = {
88         'init': {
89             'actions': [], 
90             'result': {'type':'form', 'arch':section_form, 'fields':{}, 'state':[('end','Cancel'),('yes','Create Index')]}
91         },
92         'yes': {
93             'actions': [wiki_do_index], 
94             'result': {
95                 'type':'state', 
96                 'state':'end'
97             }
98         }
99     }
100 make_index('wiki.make.index')
101
102 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: