1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-today OpenERP SA (<http://www.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 ##############################################################################
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 from openerp.addons.base_status.base_state import base_state
30 # crm.meeting is defined here so that it may be used by modules other than crm,
31 # without forcing the installation of crm.
34 class crm_meeting_type(osv.Model):
35 _name = 'crm.meeting.type'
36 _description = 'Meeting Type'
38 'name': fields.char('Name', size=64, required=True, translate=True),
41 class crm_meeting(base_state, osv.Model):
42 """ Model for CRM meetings """
44 _description = "Meeting"
46 _inherit = ["calendar.event", "mail.thread", "ir.needaction_mixin"]
48 # base_state required fields
49 'create_date': fields.datetime('Creation Date', readonly=True),
50 'write_date': fields.datetime('Write Date', readonly=True),
51 'date_open': fields.datetime('Confirmed', readonly=True),
52 'date_closed': fields.datetime('Closed', readonly=True),
53 'partner_ids': fields.many2many('res.partner', 'crm_meeting_partner_rel', 'meeting_id', 'partner_id',
54 string='Attendees', states={'done': [('readonly', True)]}),
55 'state': fields.selection(
56 [('draft', 'Unconfirmed'), ('open', 'Confirmed')],
57 string='Status', size=16, readonly=True, track_visibility='onchange'),
59 'name': fields.char('Meeting Subject', size=128, required=True, states={'done': [('readonly', True)]}),
60 'categ_ids': fields.many2many('crm.meeting.type', 'meeting_category_rel',
61 'event_id', 'type_id', 'Tags'),
62 'attendee_ids': fields.many2many('calendar.attendee', 'meeting_attendee_rel',\
63 'event_id', 'attendee_id', 'Attendees', states={'done': [('readonly', True)]}),
69 def message_get_subscription_data(self, cr, uid, ids, context=None):
71 for virtual_id in ids:
72 real_id = base_calendar_id2real_id(virtual_id)
73 result = super(crm_meeting, self).message_get_subscription_data(cr, uid, [real_id], context=context)
74 res[virtual_id] = result[real_id]
77 def copy(self, cr, uid, id, default=None, context=None):
78 default = default or {}
79 default['attendee_ids'] = False
80 return super(crm_meeting, self).copy(cr, uid, id, default, context)
82 def onchange_partner_ids(self, cr, uid, ids, value, context=None):
83 """ The basic purpose of this method is to check that destination partners
84 effectively have email addresses. Otherwise a warning is thrown.
85 :param value: value format: [[6, 0, [3, 4]]]
88 if not value or not value[0] or not value[0][0] == 6:
90 res.update(self.check_partners_email(cr, uid, value[0][2], context=context))
93 def check_partners_email(self, cr, uid, partner_ids, context=None):
94 """ Verify that selected partner_ids have an email_address defined.
95 Otherwise throw a warning. """
96 partner_wo_email_lst = []
97 for partner in self.pool.get('res.partner').browse(cr, uid, partner_ids, context=context):
99 partner_wo_email_lst.append(partner)
100 if not partner_wo_email_lst:
102 warning_msg = _('The following contacts have no email address :')
103 for partner in partner_wo_email_lst:
104 warning_msg += '\n- %s' % (partner.name)
106 'title': _('Email addresses not found'),
107 'message': warning_msg,
110 # ----------------------------------------
112 # ----------------------------------------
114 # shows events of the day for this user
115 def _needaction_domain_get(self, cr, uid, context=None):
116 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)]
118 def message_post(self, cr, uid, thread_id, body='', subject=None, type='notification',
119 subtype=None, parent_id=False, attachments=None, context=None, **kwargs):
120 if isinstance(thread_id, str):
121 thread_id = get_real_ids(thread_id)
122 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)
124 class mail_message(osv.osv):
125 _inherit = "mail.message"
127 def search(self, cr, uid, args, offset=0, limit=0, order=None, context=None, count=False):
129 convert the search on real ids in the case it was asked on virtual ids, then call super()
131 for index in range(len(args)):
132 if args[index][0] == "res_id" and isinstance(args[index][2], str):
133 args[index][2] = get_real_ids(args[index][2])
134 return super(mail_message, self).search(cr, uid, args, offset=offset, limit=limit, order=order, context=context, count=count)
136 def _find_allowed_model_wise(self, cr, uid, doc_model, doc_dict, context=None):
137 if doc_model == 'crm.meeting':
138 for virtual_id in self.pool[doc_model].get_recurrent_ids(cr, uid, doc_dict.keys(), [], context=context):
139 doc_dict.setdefault(virtual_id, doc_dict[get_real_ids(virtual_id)])
140 return super(mail_message, self)._find_allowed_model_wise(cr, uid, doc_model, doc_dict, context=context)
142 class ir_attachment(osv.osv):
143 _inherit = "ir.attachment"
145 def search(self, cr, uid, args, offset=0, limit=0, order=None, context=None, count=False):
147 convert the search on real ids in the case it was asked on virtual ids, then call super()
149 for index in range(len(args)):
150 if args[index][0] == "res_id" and isinstance(args[index][2], str):
151 args[index][2] = get_real_ids(args[index][2])
152 return super(ir_attachment, self).search(cr, uid, args, offset=offset, limit=limit, order=order, context=context, count=count)
154 def write(self, cr, uid, ids, vals, context=None):
156 when posting an attachment (new or not), convert the virtual ids in real ids.
158 if isinstance(vals.get('res_id'), str):
159 vals['res_id'] = get_real_ids(vals.get('res_id'))
160 return super(ir_attachment, self).write(cr, uid, ids, vals, context=context)
162 class invite_wizard(osv.osv_memory):
163 _inherit = 'mail.wizard.invite'
165 def default_get(self, cr, uid, fields, context=None):
167 in case someone clicked on 'invite others' wizard in the followers widget, transform virtual ids in real ids
169 result = super(invite_wizard, self).default_get(cr, uid, fields, context=context)
170 if 'res_id' in result:
171 result['res_id'] = get_real_ids(result['res_id'])