[MERGE] account period generation revert wrong date_context patch
[odoo/odoo.git] / addons / base_setup / base_setup.py
1 # -*- coding: 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 pytz
22
23 import simplejson
24 import cgi
25 import pooler
26 import tools
27 from osv import fields, osv
28 from tools.translate import _
29 from lxml import etree
30
31 #Migrate data from another application Conf wiz
32
33 class migrade_application_installer_modules(osv.osv_memory):
34     _name = 'migrade.application.installer.modules'
35     _inherit = 'res.config.installer'
36     _columns = {
37         'import_saleforce': fields.boolean('Import Saleforce',
38             help="For Import Saleforce"),
39         'import_sugarcrm': fields.boolean('Import Sugarcrm',
40             help="For Import Sugarcrm"),
41         'sync_google_contact': fields.boolean('Sync Google Contact',
42             help="For Sync Google Contact"),
43         'quickbooks_ippids': fields.boolean('Quickbooks Ippids',
44             help="For Quickbooks Ippids"),
45     }
46
47 class product_installer(osv.osv_memory):
48     _name = 'product.installer'
49     _inherit = 'res.config'
50     _columns = {
51         'customers': fields.selection([('create','Create'), ('import','Import')], 'Customers', size=32, required=True, help="Import or create customers"),
52     }
53     _defaults = {
54         'customers': 'create',
55     }
56
57     def execute(self, cr, uid, ids, context=None):
58         if context is None:
59              context = {}
60         data_obj = self.pool.get('ir.model.data')
61         val = self.browse(cr, uid, ids, context=context)[0]
62         if val.customers == 'create':
63             id2 = data_obj._get_id(cr, uid, 'base', 'view_partner_form')
64             if id2:
65                 id2 = data_obj.browse(cr, uid, id2, context=context).res_id
66             return {
67                     'view_type': 'form',
68                     'view_mode': 'form',
69                     'res_model': 'res.partner',
70                     'views': [(id2, 'form')],
71                     'type': 'ir.actions.act_window',
72                     'target': 'current',
73                     'nodestroy':False,
74                 }
75         if val.customers == 'import':
76             return {'type': 'ir.actions.act_window'}
77
78 # Define users preferences for new users (ir.values)
79
80 def _lang_get(self, cr, uid, context=None):
81     obj = self.pool.get('res.lang')
82     ids = obj.search(cr, uid, [('translatable','=',True)])
83     res = obj.read(cr, uid, ids, ['code', 'name'], context=context)
84     res = [(r['code'], r['name']) for r in res]
85     return res
86
87 def _tz_get(self,cr,uid, context=None):
88     return [(x, x) for x in pytz.all_timezones]
89
90 class user_preferences_config(osv.osv_memory):
91     _name = 'user.preferences.config'
92     _inherit = 'res.config'
93     _columns = {
94         'context_tz': fields.selection(_tz_get,  'Timezone', size=64,
95             help="Set default for new user's timezone, used to perform timezone conversions "
96                  "between the server and the client."),
97         'context_lang': fields.selection(_lang_get, 'Language', required=True,
98             help="Sets default language for the all user interface, when UI "
99                 "translations are available. If you want to Add new Language, you can add it from 'Load an Official Translation' wizard  from 'Administration' menu."),
100         'view': fields.selection([('simple','Simplified'),
101                                   ('extended','Extended')],
102                                  'Interface', required=True, help= "If you use OpenERP for the first time we strongly advise you to select the simplified interface, which has less features but is easier. You can always switch later from the user preferences." ),
103         'menu_tips': fields.boolean('Display Tips', help="Check out this box if you want to always display tips on each menu action"),
104
105     }
106     _defaults={
107                'view' : lambda self,cr,uid,*args: self.pool.get('res.users').browse(cr, uid, uid).view or 'simple',
108                'context_lang' : 'en_US',
109                'menu_tips' : True
110     }
111
112     def default_get(self, cr, uid, fields, context=None):
113         if context is None:
114             context = {}
115         res = super(user_preferences_config, self).default_get(cr, uid, fields, context=context)
116         res_default = self.pool.get('ir.values').get(cr, uid, 'default', False, ['res.users'])
117         for id, field, value in res_default:
118             res.update({field: value})
119         return res
120
121     def execute(self, cr, uid, ids, context=None):
122         user_obj = self.pool.get('res.users')
123         user_ids = user_obj.search(cr, uid, [], context=context)
124         for o in self.browse(cr, uid, ids, context=context):
125             user_obj.write(cr , uid, user_ids ,{'context_tz' : o.context_tz, 'context_lang' : o.context_lang, 'view' : o.view, 'menu_tips' : o.menu_tips}, context=context)
126             ir_values_obj = self.pool.get('ir.values')
127             ir_values_obj.set(cr, uid, 'default', False, 'context_tz', ['res.users'], o.context_tz)
128             ir_values_obj.set(cr, uid, 'default', False, 'context_lang', ['res.users'], o.context_lang)
129             ir_values_obj.set(cr, uid, 'default', False, 'view', ['res.users'], o.view)
130             ir_values_obj.set(cr, uid, 'default', False, 'menu_tips', ['res.users'], o.menu_tips)
131         return {}
132
133 # Specify Your Terminology
134
135 class specify_partner_terminology(osv.osv_memory):
136     _name = 'base.setup.terminology'
137     _inherit = 'res.config'
138     _columns = {
139         'partner': fields.selection([
140             ('Customer','Customer'),
141             ('Client','Client'),
142             ('Member','Member'),
143             ('Patient','Patient'),
144             ('Partner','Partner'),
145             ('Donor','Donor'),
146             ('Guest','Guest'),
147             ('Tenant','Tenant')
148         ], 'How do you call a Customer', required=True ),
149     }
150     _defaults={
151         'partner' :'Customer',
152     }
153
154     def make_translations(self, cr, uid, ids, name, type, src, value, res_id=0, context=None):
155         trans_obj = self.pool.get('ir.translation')
156         user_obj = self.pool.get('res.users')
157         context_lang = user_obj.browse(cr, uid, uid, context=context).context_lang
158         existing_trans_ids = trans_obj.search(cr, uid, [('name','=',name), ('lang','=',context_lang), ('type','=',type), ('src','=',src), ('res_id','=',res_id)])
159         if existing_trans_ids:
160             trans_obj.write(cr, uid, existing_trans_ids, {'value': value}, context=context)
161         else:
162             create_id = trans_obj.create(cr, uid, {'name': name,'lang': context_lang, 'type': type, 'src': src, 'value': value , 'res_id': res_id}, context=context)
163         return {}
164
165     def execute(self, cr, uid, ids, context=None):
166         def _case_insensitive_replace(ref_string, src, value):
167             import re
168             pattern = re.compile(src, re.IGNORECASE)
169             return pattern.sub(_(value), _(ref_string))
170         trans_obj = self.pool.get('ir.translation')
171         fields_obj = self.pool.get('ir.model.fields')
172         menu_obj = self.pool.get('ir.ui.menu')
173         act_window_obj = self.pool.get('ir.actions.act_window')
174         for o in self.browse(cr, uid, ids, context=context):
175             #translate label of field
176             field_ids = fields_obj.search(cr, uid, [('field_description','ilike','Customer')])
177             for f_id in fields_obj.browse(cr ,uid, field_ids, context=context):
178                 field_ref = f_id.model_id.model + ',' + f_id.name
179                 self.make_translations(cr, uid, ids, field_ref, 'field', f_id.field_description, _case_insensitive_replace(f_id.field_description,'Customer',o.partner), context=context)
180             #translate help tooltip of field
181             for obj in self.pool.models.values():
182                 for field_name, field_rec in obj._columns.items():
183                     if field_rec.help.lower().count('customer'):
184                         field_ref = obj._name + ',' + field_name
185                         self.make_translations(cr, uid, ids, field_ref, 'help', field_rec.help, _case_insensitive_replace(field_rec.help,'Customer',o.partner), context=context)
186             #translate menuitems
187             menu_ids = menu_obj.search(cr,uid, [('name','ilike','Customer')])
188             for m_id in menu_obj.browse(cr, uid, menu_ids, context=context):
189                 menu_name = m_id.name
190                 menu_ref = 'ir.ui.menu' + ',' + 'name'
191                 self.make_translations(cr, uid, ids, menu_ref, 'model', menu_name, _case_insensitive_replace(menu_name,'Customer',o.partner), res_id=m_id.id, context=context)
192             #translate act window name
193             act_window_ids = act_window_obj.search(cr, uid, [('name','ilike','Customer')])
194             for act_id in act_window_obj.browse(cr ,uid, act_window_ids, context=context):
195                 act_ref = 'ir.actions.act_window' + ',' + 'name'
196                 self.make_translations(cr, uid, ids, act_ref, 'model', act_id.name, _case_insensitive_replace(act_id.name,'Customer',o.partner), res_id=act_id.id, context=context)
197             #translate act window tooltips
198             act_window_ids = act_window_obj.search(cr, uid, [('help','ilike','Customer')])
199             for act_id in act_window_obj.browse(cr ,uid, act_window_ids, context=context):
200                 act_ref = 'ir.actions.act_window' + ',' + 'help'
201                 self.make_translations(cr, uid, ids, act_ref, 'model', act_id.help, _case_insensitive_replace(act_id.help,'Customer',o.partner), res_id=act_id.id, context=context)
202         return {}
203
204 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: