1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Business Applications
5 # Copyright (C) 2004-2012 OpenERP S.A. (<http://openerp.com>).
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.
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.
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/>.
20 ##############################################################################
22 from openerp.osv import fields, osv
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.
28 It relies on the following convention on the object::
30 class my_config_settings(osv.osv_memory):
32 _inherit = ['res.config.settings', 'fetchmail.config.settings']
35 'fetchmail_stuff': fields.boolean(...,
36 fetchmail_model='my.stuff', fetchmail_name='Incoming Stuff'),
39 def configure_fetchmail_stuff(self, cr, uid, ids, context=None):
40 return self.configure_fetchmail(cr, uid, 'fetchmail_stuff', context)
42 and in the form view::
44 <field name="fetchmail_stuff"/>
45 <button type="object" name="configure_fetchmail_stuff"/>
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.
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.
55 _name = 'fetchmail.config.settings'
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_')]
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)
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]]
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)
82 def configure_fetchmail(self, cr, uid, field, context=None):
83 """ open the form view of the fetchmail.server to configure """
85 'type': 'ir.actions.act_window',
86 'res_model': 'fetchmail.server',
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)])
94 action['res_id'] = server_ids[0]
97 'default_name': self._columns[field].fetchmail_name,
98 'default_object_id': model_id,
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)
109 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: