[FIX] Empty all current line(s) when you change template. Before, the display do...
[odoo/odoo.git] / addons / calendar / 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 import time
23
24 from openerp.osv import fields, osv
25 from openerp.tools import DEFAULT_SERVER_DATE_FORMAT
26 from openerp.tools.translate import _
27 from base_calendar import get_real_ids, base_calendar_id2real_id
28 #
29 # crm.meeting is defined here so that it may be used by modules other than crm,
30 # without forcing the installation of crm.
31 #
32
33 class crm_meeting_type(osv.Model):
34     _name = 'crm.meeting.type'
35     _description = 'Meeting Type'
36     _columns = {
37         'name': fields.char('Name', size=64, required=True, translate=True),
38     }
39
40 class crm_meeting(osv.Model):
41     """ Model for CRM meetings """
42     _name = 'crm.meeting'
43     _description = "Meeting"
44     _order = "id desc"
45     _inherit = ["calendar.event", "mail.thread", "ir.needaction_mixin"]
46     _columns = {
47         'create_date': fields.datetime('Creation Date', readonly=True),
48         'write_date': fields.datetime('Write Date', readonly=True),
49         'date_open': fields.datetime('Confirmed', readonly=True),
50         'date_closed': fields.datetime('Closed', readonly=True),
51         'partner_ids': fields.many2many('res.partner', 'crm_meeting_partner_rel', 'meeting_id', 'partner_id',
52             string='Attendees', states={'done': [('readonly', True)]}),
53         'state': fields.selection(
54                     [('draft', 'Unconfirmed'), ('open', 'Confirmed')],
55                     string='Status', size=16, readonly=True, track_visibility='onchange'),
56         # Meeting fields
57         'name': fields.char('Meeting Subject', size=128, required=True, states={'done': [('readonly', True)]}),
58         'categ_ids': fields.many2many('crm.meeting.type', 'meeting_category_rel',
59             'event_id', 'type_id', 'Tags'),
60         'attendee_ids': fields.many2many('calendar.attendee', 'meeting_attendee_rel',\
61                             'event_id', 'attendee_id', 'Invited People', states={'done': [('readonly', True)]}),
62     }
63     _defaults = {
64         'state': 'open',
65     }
66
67     def message_get_subscription_data(self, cr, uid, ids, context=None):
68         res = {}
69         for virtual_id in ids:
70             real_id = base_calendar_id2real_id(virtual_id)
71             result = super(crm_meeting, self).message_get_subscription_data(cr, uid, [real_id], context=context)
72             res[virtual_id] = result[real_id]
73         return res
74
75     def copy(self, cr, uid, id, default=None, context=None):
76         default = default or {}
77         default['attendee_ids'] = False
78         return super(crm_meeting, self).copy(cr, uid, id, default, context)
79
80     def write(self, cr, uid, ids, values, context=None):
81         """ Override to add case management: open/close dates """
82         if values.get('state')and values.get('state') == 'open':
83             values['date_open'] = fields.datetime.now()
84         return super(crm_meeting, self).write(cr, uid, ids, values, context=context)
85
86     def onchange_partner_ids(self, cr, uid, ids, value, context=None):
87         """ The basic purpose of this method is to check that destination partners
88             effectively have email addresses. Otherwise a warning is thrown.
89             :param value: value format: [[6, 0, [3, 4]]]
90         """
91         res = {'value': {}}
92         if not value or not value[0] or not value[0][0] == 6:
93             return
94         res.update(self.check_partners_email(cr, uid, value[0][2], context=context))
95         return res
96
97     def check_partners_email(self, cr, uid, partner_ids, context=None):
98         """ Verify that selected partner_ids have an email_address defined.
99             Otherwise throw a warning. """
100         partner_wo_email_lst = []
101         for partner in self.pool.get('res.partner').browse(cr, uid, partner_ids, context=context):
102             if not partner.email:
103                 partner_wo_email_lst.append(partner)
104         if not partner_wo_email_lst:
105             return {}
106         warning_msg = _('The following contacts have no email address :')
107         for partner in partner_wo_email_lst:
108             warning_msg += '\n- %s' % (partner.name)
109         return {'warning': {
110                     'title': _('Email addresses not found'),
111                     'message': warning_msg,
112                     }
113                 }
114     # ----------------------------------------
115     # OpenChatter
116     # ----------------------------------------
117
118     # shows events of the day for this user
119     def _needaction_domain_get(self, cr, uid, context=None):
120         return [('date', '<=', time.strftime(DEFAULT_SERVER_DATE_FORMAT + ' 23:59:59')), ('date_deadline', '>=', time.strftime(DEFAULT_SERVER_DATE_FORMAT + ' 23:59:59')), ('user_id', '=', uid)]
121
122     def message_post(self, cr, uid, thread_id, body='', subject=None, type='notification',
123                         subtype=None, parent_id=False, attachments=None, context=None, **kwargs):
124         if isinstance(thread_id, str):
125             thread_id = get_real_ids(thread_id)
126         return super(crm_meeting, self).message_post(cr, uid, thread_id, body=body, subject=subject, type=type, subtype=subtype, parent_id=parent_id, attachments=attachments, context=context, **kwargs)
127
128 class mail_message(osv.osv):
129     _inherit = "mail.message"
130
131     def search(self, cr, uid, args, offset=0, limit=0, order=None, context=None, count=False):
132         '''
133         convert the search on real ids in the case it was asked on virtual ids, then call super()
134         '''
135         for index in range(len(args)):
136             if args[index][0] == "res_id" and isinstance(args[index][2], str):
137                 args[index][2] = get_real_ids(args[index][2])
138         return super(mail_message, self).search(cr, uid, args, offset=offset, limit=limit, order=order, context=context, count=count)
139
140     def _find_allowed_model_wise(self, cr, uid, doc_model, doc_dict, context=None):
141         if doc_model == 'crm.meeting':
142             for virtual_id in self.pool[doc_model].get_recurrent_ids(cr, uid, doc_dict.keys(), [], context=context):
143                 doc_dict.setdefault(virtual_id, doc_dict[get_real_ids(virtual_id)])
144         return super(mail_message, self)._find_allowed_model_wise(cr, uid, doc_model, doc_dict, context=context)
145
146 class ir_attachment(osv.osv):
147     _inherit = "ir.attachment"
148
149     def search(self, cr, uid, args, offset=0, limit=0, order=None, context=None, count=False):
150         '''
151         convert the search on real ids in the case it was asked on virtual ids, then call super()
152         '''
153         for index in range(len(args)):
154             if args[index][0] == "res_id" and isinstance(args[index][2], str):
155                 args[index][2] = get_real_ids(args[index][2])
156         return super(ir_attachment, self).search(cr, uid, args, offset=offset, limit=limit, order=order, context=context, count=count)
157
158     def write(self, cr, uid, ids, vals, context=None):
159         '''
160         when posting an attachment (new or not), convert the virtual ids in real ids.
161         '''
162         if isinstance(vals.get('res_id'), str):
163             vals['res_id'] = get_real_ids(vals.get('res_id'))
164         return super(ir_attachment, self).write(cr, uid, ids, vals, context=context)
165
166 class invite_wizard(osv.osv_memory):
167     _inherit = 'mail.wizard.invite'
168
169     def default_get(self, cr, uid, fields, context=None):
170         '''
171         in case someone clicked on 'invite others' wizard in the followers widget, transform virtual ids in real ids
172         '''
173         result = super(invite_wizard, self).default_get(cr, uid, fields, context=context)
174         if 'res_id' in result:
175             result['res_id'] = get_real_ids(result['res_id'])
176         return result