1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
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 base_calendar import base_calendar
23 from osv import fields, osv
24 from tools.translate import _
28 class base_calendar_invite_attendee(osv.osv_memory):
33 _name = "base_calendar.invite.attendee"
34 _description = "Invite Attendees"
37 'type': fields.selection([('internal', 'Internal User'), \
38 ('external', 'External Email'), \
39 ('partner', 'Partner Contacts')], 'Type', required=True, help="Select whom you want to Invite"),
40 'user_ids': fields.many2many('res.users', 'invite_user_rel',
41 'invite_id', 'user_id', 'Users'),
42 'partner_id': fields.many2one('res.partner', 'Partner'),
43 'email': fields.char('Email', size=124, help="Provide external email address who will receive this invitation."),
44 'contact_ids': fields.many2many('res.partner.address', 'invite_contact_rel',
45 'invite_id', 'contact_id', 'Contacts'),
46 'send_mail': fields.boolean('Send mail?', help='Check this if you want to \
47 send an Email to Invited Person')
55 def do_invite(self, cr, uid, ids, context=None):
57 Invites attendee for meeting..
58 @param cr: the current row, from the database cursor,
59 @param uid: the current user’s ID for security checks,
60 @param ids: List of base calendar invite attendee’s IDs.
61 @param context: A standard dictionary for contextual values
62 @return: Dictionary of {}.
69 context_id = context and context.get('active_id', False) or False
70 if not context or not context.get('model'):
71 return {'type': 'ir.actions.act_window_close'}
73 model = context.get('model')
75 model_field = context.get('attendee_field', False)
76 obj = self.pool.get(model)
77 res_obj = obj.browse(cr, uid, context_id, context=context)
78 att_obj = self.pool.get('calendar.attendee')
79 user_obj = self.pool.get('res.users')
80 current_user = user_obj.browse(cr, uid, uid, context=context)
81 for datas in self.read(cr, uid, ids, context=context):
82 type = datas.get('type')
88 if not model == 'calendar.attendee':
90 ref = {'ref': '%s,%s' % (model, base_calendar.base_calendar_id2real_id(context_id))}
92 return {'type': 'ir.actions.act_window_close'}
93 if type == 'internal':
95 if not datas.get('user_ids'):
96 raise osv.except_osv(_('Error!'), ("Please select any User"))
97 for user_id in datas.get('user_ids'):
98 user = user_obj.browse(cr, uid, user_id)
101 'email': user.address_id.email
105 if user.address_id.email:
106 mail_to.append(user.address_id.email)
108 elif type == 'external' and datas.get('email'):
109 res = {'email': datas['email']}
112 mail_to.append(datas['email'])
114 elif type == 'partner':
115 add_obj = self.pool.get('res.partner.address')
116 for contact in add_obj.browse(cr, uid, datas['contact_ids']):
118 'partner_address_id': contact.id,
119 'email': contact.email
124 mail_to.append(contact.email)
127 if model == 'calendar.attendee':
128 att = att_obj.browse(cr, uid, context_id)
130 'parent_ids': [(4, att.id)],
131 'ref': att.ref and (att.ref._name + ',' +str(att.ref.id)) or False
134 attendees.append(att_obj.create(cr, uid, att_val))
136 for attendee in attendees:
137 obj.write(cr, uid, res_obj.id, {model_field: [(4, attendee)]})
139 if datas.get('send_mail'):
141 name = map(lambda x: x[1], filter(lambda x: type==x[0], \
142 self._columns['type'].selection))
143 raise osv.except_osv(_('Error!'), ("%s must have an email Address to send mail") %(name[0]))
144 att_obj._send_mail(cr, uid, attendees, mail_to, \
145 email_from = current_user.user_email or tools.config.get('email_from', False))
147 return {'type': 'ir.actions.act_window_close'}
150 def onchange_partner_id(self, cr, uid, ids, partner_id, *args, **argv):
152 Make entry on contact_ids on change of partner_id field.
153 @param cr: the current row, from the database cursor,
154 @param uid: the current user’s ID for security checks,
155 @param ids: List of base calendar invite attendee’s IDs.
156 @param partner_id: id of Partner
157 @return: dictionary of value.
161 return {'value': {'contact_ids': []}}
162 cr.execute('SELECT id FROM res_partner_address \
163 WHERE partner_id=%s', (partner_id,))
164 contacts = map(lambda x: x[0], cr.fetchall())
165 return {'value': {'contact_ids': contacts}}
167 base_calendar_invite_attendee()
169 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: