[IMP]:contract,analytic_project,analytic_hr_expense: improve whole contract view...
[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 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(..., readonly=True,
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 configure_fetchmail(self, cr, uid, field, context=None):
71         """ open the form view of the fetchmail.server to configure """
72         action = {
73             'type': 'ir.actions.act_window',
74             'res_model': 'fetchmail.server',
75             'view_mode': 'form',
76         }
77         model_name = self._columns[field].fetchmail_model
78         model_id = self.pool.get('ir.model').search(cr, uid, [('model', '=', model_name)])[0]
79         server_ids = self.pool.get('fetchmail.server').search(cr, uid, [('object_id', '=', model_id)])
80         if server_ids:
81             action['res_id'] = server_ids[0]
82         else:
83             action['context'] = {
84                 'default_name': self._columns[field].fetchmail_name,
85                 'default_object_id': model_id,
86             }
87         return action
88
89     def __getattr__(self, name):
90         """ catch calls to 'configure_fetchmail_XXX' """
91         if name.startswith('configure_fetchmail_'):
92             return (lambda cr, uid, ids, context=None:
93                     self.configure_fetchmail(cr, uid, name[10:], context))
94         return super(fetchmail_config_settings, self).__getattr__(name)
95
96 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: