[FIX] hr_timesheet_sheet: revert stupid fix in 8095.1.1 that created very obvious...
[odoo/odoo.git] / addons / crm / crm_meeting.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-today OpenERP SA (<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 osv import fields, osv
23 import tools
24 from tools.translate import _
25 import logging
26 _logger = logging.getLogger(__name__)
27
28 #
29 # crm.meeting is defined in module base_calendar
30 #
31 class crm_meeting(osv.Model):
32     """ Model for CRM meetings """
33     _inherit = 'crm.meeting'
34     _columns = {
35         'phonecall_id': fields.many2one ('crm.phonecall', 'Phonecall'),
36         'opportunity_id': fields.many2one ('crm.lead', 'Opportunity', domain="[('type', '=', 'opportunity')]"),
37     }
38
39     def create(self, cr, uid, vals, context=None):
40         obj_id = super(crm_meeting, self).create(cr, uid, vals, context=context)
41         self.create_send_note(cr, uid, [obj_id], context=context)
42         return obj_id
43
44     def create_send_note(self, cr, uid, ids, context=None):
45         if context is None:
46             context = {}
47         # update context: if come from phonecall, default state values can make the message_post crash
48         context.pop('default_state', False)
49         for meeting in self.browse(cr, uid, ids, context=context):
50             # in the message, transpose meeting.date to the timezone of the current user
51             meeting_date = fields.DT.datetime.strptime(meeting.date, tools.DEFAULT_SERVER_DATETIME_FORMAT)
52             meeting_date_tz = fields.datetime.context_timestamp(cr, uid, meeting_date, context=context).strftime(tools.DATETIME_FORMATS_MAP['%+'] + " (%Z)")
53             if meeting.opportunity_id: # meeting can be create from phonecalls or opportunities, therefore checking for the parent
54                 lead = meeting.opportunity_id
55                 message = _("Meeting linked to the opportunity <em>%s</em> has been <b>created</b> and <b>scheduled</b> on <em>%s</em>.") % (lead.name, meeting_date_tz)
56                 lead.message_post(body=message)
57             elif meeting.phonecall_id:
58                 phonecall = meeting.phonecall_id
59                 message = _("Meeting linked to the phonecall <em>%s</em> has been <b>created</b> and <b>scheduled</b> on <em>%s</em>.") % (phonecall.name, meeting_date_tz)
60                 phonecall.message_post(body=message)
61             else:
62                 message = _("A meeting has been <b>scheduled</b> on <em>%s</em>.") % (meeting_date_tz)
63             meeting.message_post(body=message)
64         return True
65
66 class calendar_attendee(osv.osv):
67     """ Calendar Attendee """
68
69     _inherit = 'calendar.attendee'
70     _description = 'Calendar Attendee'
71
72     def _compute_data(self, cr, uid, ids, name, arg, context=None):
73        """
74         @param self: The object pointer
75         @param cr: the current row, from the database cursor,
76         @param uid: the current user’s ID for security checks,
77         @param ids: List of compute data’s IDs
78         @param context: A standard dictionary for contextual values
79         """
80        name = name[0]
81        result = super(calendar_attendee, self)._compute_data(cr, uid, ids, name, arg, context=context)
82
83        for attdata in self.browse(cr, uid, ids, context=context):
84             id = attdata.id
85             result[id] = {}
86             if name == 'categ_id':
87                 if attdata.ref and 'categ_id' in attdata.ref._columns:
88                     result[id][name] = (attdata.ref.categ_id.id, attdata.ref.categ_id.name,)
89                 else:
90                     result[id][name] = False
91        return result
92
93     _columns = {
94         'categ_id': fields.function(_compute_data, \
95                         string='Event Type', type="many2one", \
96                         relation="crm.case.categ", multi='categ_id'),
97     }
98
99 class res_users(osv.osv):
100     _name = 'res.users'
101     _inherit = 'res.users'
102
103     def create(self, cr, uid, data, context=None):
104         user_id = super(res_users, self).create(cr, uid, data, context=context)
105
106         # add shortcut unless 'noshortcut' is True in context
107         if not(context and context.get('noshortcut', False)):
108             data_obj = self.pool.get('ir.model.data')
109             try:
110                 data_id = data_obj._get_id(cr, uid, 'crm', 'ir_ui_view_sc_calendar0')
111                 view_id  = data_obj.browse(cr, uid, data_id, context=context).res_id
112                 self.pool.get('ir.ui.view_sc').copy(cr, uid, view_id, default = {
113                                             'user_id': user_id}, context=context)
114             except:
115                 # Tolerate a missing shortcut. See product/product.py for similar code.
116                 _logger.debug('Skipped meetings shortcut for user "%s".', data.get('name','<new'))
117         return user_id
118
119 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: