[FIX] web: many2many field handle "no_create" option
[odoo/odoo.git] / addons / portal_crm / contact.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2011 OpenERP S.A (<http://www.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 osv, fields
23 from openerp import SUPERUSER_ID
24
25 class crm_contact_us(osv.TransientModel):
26     """ Create new leads through the "contact us" form """
27     _name = 'portal_crm.crm_contact_us'
28     _description = 'Contact form for the portal'
29     _inherit = 'crm.lead'
30     _columns = {
31         'company_ids' : fields.many2many('res.company', string='Companies', readonly=True),
32     }
33
34     def _get_companies(self, cr, uid, context=None):
35         """
36         Fetch companies in order to display them in the wizard view
37
38         @return a list of ids of the companies
39         """
40         r = self.pool.get('res.company').search(cr, uid, [], context=context)
41         return r
42
43     def _get_user_name(self, cr, uid, context=None):
44         """
45         If the user is logged in (i.e. not anonymous), get the user's name to
46         pre-fill the partner_name field.
47         Same goes for the other _get_user_attr methods.
48
49         @return current user's name if the user isn't "anonymous", None otherwise
50         """
51         user = self.pool.get('res.users').read(cr, uid, uid, ['login'], context)
52
53         if (user['login'] != 'anonymous'):
54             return self.pool.get('res.users').name_get(cr, uid, uid, context)[0][1]
55         else:
56             return None
57
58     def _get_user_email(self, cr, uid, context=None):
59         user = self.pool.get('res.users').read(cr, uid, uid, ['login', 'email'], context)
60
61         if (user['login'] != 'anonymous' and user['email']):
62             return user['email']
63         else:
64             return None
65
66     def _get_user_phone(self, cr, uid, context=None):
67         user = self.pool.get('res.users').read(cr, uid, uid, ['login', 'phone'], context)
68
69         if (user['login'] != 'anonymous' and user['phone']):
70             return user['phone']
71         else:
72             return None
73
74     _defaults = {
75         'partner_name': _get_user_name,
76         'email_from': _get_user_email,
77         'phone': _get_user_phone,
78         'company_ids': _get_companies,
79     }
80
81     def create(self, cr, uid, values, context=None):
82         """
83         Since they are potentially sensitive, we don't want any user to be able
84         to read datas generated through this module.  Therefore we'll write
85         these information directly in the crm.lead table and leave blank
86         entries in the contact table.
87         This is why the create() method is overwritten.
88         """
89         crm_lead = self.pool.get('crm.lead')
90
91         """
92         Because of the complex inheritance of the crm.lead model and the other
93         models implied (like mail.thread, among others, that performs a read
94         when its create() method is called (in method message_get_subscribers()),
95         it is quite complicated to set proper rights for this object.
96         Therefore, user SUPERUSER_ID will perform the creation.
97         """
98         values['contact_name'] = values['partner_name']
99         crm_lead.create(cr, SUPERUSER_ID, dict(values, user_id=False), context)
100
101         """
102         Create an empty record in the contact table.
103         Since the 'name' field is mandatory, give an empty string to avoid an integrity error.
104         Pass mail_create_nosubscribe key in context because otherwise the inheritance
105         leads to a message_subscribe_user, that triggers access right issues.
106         """
107         empty_values = dict((k, False) if k != 'name' else (k, '') for k, v in values.iteritems())
108         return super(crm_contact_us, self).create(cr, SUPERUSER_ID, empty_values, {'mail_create_nosubscribe': True})
109
110     def submit(self, cr, uid, ids, context=None):
111         """ When the form is submitted, redirect the user to a "Thanks" message """
112         return {
113             'type': 'ir.actions.act_window',
114             'view_mode': 'form',
115             'view_type': 'form',
116             'res_model': self._name,
117             'res_id': ids[0],
118             'view_id': self.pool.get('ir.model.data').get_object_reference(cr, uid, 'portal_crm', 'wizard_contact_form_view_thanks')[1],
119             'target': 'new',
120         }
121
122     def _needaction_domain_get(self, cr, uid, context=None):
123         """
124         This model doesn't need the needactions mechanism inherited from
125         crm_lead, so simply override the method to return an empty domain
126         and, therefore, 0 needactions.
127         """
128         return False