[FIX] wek_kanban_gauge: use field raw_value to avoid thousands separator etc.
[odoo/odoo.git] / addons / fetchmail / res_config.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Business Applications
5 #    Copyright (C) 2004-2012 OpenERP S.A. (<http://openerp.com>).
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 openerp.osv import fields, osv
23
24 class fetchmail_config_settings(osv.osv_memory):
25     """ This wizard can be inherited in conjunction with 'res.config.settings', in order to
26         define fields that configure a fetchmail server.
27
28         It relies on the following convention on the object::
29
30             class my_config_settings(osv.osv_memory):
31                 _name = 'my.settings'
32                 _inherit = ['res.config.settings', 'fetchmail.config.settings']
33
34                 _columns = {
35                     'fetchmail_stuff': fields.boolean(...,
36                         fetchmail_model='my.stuff', fetchmail_name='Incoming Stuff'),
37                 }
38
39                 def configure_fetchmail_stuff(self, cr, uid, ids, context=None):
40                     return self.configure_fetchmail(cr, uid, 'fetchmail_stuff', context)
41
42         and in the form view::
43
44             <field name="fetchmail_stuff"/>
45             <button type="object" name="configure_fetchmail_stuff"/>
46
47         The method ``get_default_fetchmail`` determines the value of all fields that start
48         with 'fetchmail_'.  It looks up fetchmail server configurations that match the given
49         model name (``fetchmail_model``) and are active.
50
51         The button action ``configure_fetchmail_stuff`` is caught by the object, and calls
52         automatically the method ``configure_fetchmail``; it opens the fetchmail server
53         configuration form for the corresponding field.
54     """
55     _name = 'fetchmail.config.settings'
56
57     def get_default_fetchmail(self, cr, uid, fields, context=None):
58         """ determine the value of all fields like 'fetchmail_XXX' """
59         ir_model = self.pool.get('ir.model')
60         fetchmail_server = self.pool.get('fetchmail.server')
61         fetchmail_fields = [f for f in fields if f.startswith('fetchmail_')]
62         res = {}
63         for f in fetchmail_fields:
64             model_name = self._columns[f].fetchmail_model
65             model_id = ir_model.search(cr, uid, [('model', '=', model_name)])[0]
66             server_ids = fetchmail_server.search(cr, uid, [('object_id', '=', model_id), ('state', '=', 'done')])
67             res[f] = bool(server_ids)
68         return res
69
70     def set_fetchmail(self, cr, uid, ids, context=None):
71         """ deactivate fetchmail servers for all fields like 'fetchmail_XXX' that are False """
72         config = self.browse(cr, uid, ids[0], context)
73         fetchmail_fields = [f for f in self._columns if f.startswith('fetchmail_')]
74         # determine which models should not have active fetchmail servers, and
75         # deactivate all active servers for those models
76         models = [self._columns[f].fetchmail_model for f in fetchmail_fields if not config[f]]
77         if models:
78             fetchmail_server = self.pool.get('fetchmail.server')
79             server_ids = fetchmail_server.search(cr, uid, [('object_id.model', 'in', models), ('state', '=', 'done')])
80             fetchmail_server.set_draft(cr, uid, server_ids, context)
81
82     def configure_fetchmail(self, cr, uid, field, context=None):
83         """ open the form view of the fetchmail.server to configure """
84         action = {
85             'type': 'ir.actions.act_window',
86             'res_model': 'fetchmail.server',
87             'view_mode': 'form',
88             'target': 'current',
89         }
90         model_name = self._columns[field].fetchmail_model
91         model_id = self.pool.get('ir.model').search(cr, uid, [('model', '=', model_name)])[0]
92         server_ids = self.pool.get('fetchmail.server').search(cr, uid, [('object_id', '=', model_id)])
93         if server_ids:
94             action['res_id'] = server_ids[0]
95         else:
96             action['context'] = {
97                 'default_name': self._columns[field].fetchmail_name,
98                 'default_object_id': model_id,
99             }
100         return action
101
102     def __getattr__(self, name):
103         """ catch calls to 'configure_fetchmail_XXX' """
104         if name.startswith('configure_fetchmail_'):
105             return (lambda cr, uid, ids, context=None:
106                     self.configure_fetchmail(cr, uid, name[10:], context))
107         return super(fetchmail_config_settings, self).__getattr__(name)
108
109 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: