Launchpad automatic translations update.
[odoo/odoo.git] / addons / base_calendar / wizard / base_calendar_invite_attendee.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
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 base_calendar import base_calendar
23 from osv import fields, osv
24 from tools.translate import _
25 import tools
26
27
28 class base_calendar_invite_attendee(osv.osv_memory):
29     """
30     Invite attendee.
31     """
32
33     _name = "base_calendar.invite.attendee"
34     _description = "Invite Attendees"
35
36     _columns = {
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')
48     }
49
50     _defaults = {
51         'type': 'internal',
52         'send_mail': True
53     }
54
55     def do_invite(self, cr, uid, ids, context=None):
56         """
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 {}.
63         """
64
65         if context is None:
66             context = {}
67
68         model = False
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'}
72         else:
73             model = context.get('model')
74
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')
83             vals = []
84             mail_to = []
85             attendees = []
86             ref = {}
87
88             if not model == 'calendar.attendee':
89                 if context_id:
90                     ref = {'ref': '%s,%s' % (model, base_calendar.base_calendar_id2real_id(context_id))}
91                 else:
92                     return {'type': 'ir.actions.act_window_close'}
93             if type == 'internal':
94                 
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)
99                     res = {
100                            'user_id': user_id,
101                            'email': user.address_id.email
102                            }
103                     res.update(ref)
104                     vals.append(res)
105                     if user.address_id.email:
106                         mail_to.append(user.address_id.email)
107
108             elif  type == 'external' and datas.get('email'):
109                 res = {'email': datas['email']}
110                 res.update(ref)
111                 vals.append(res)
112                 mail_to.append(datas['email'])
113
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']):
117                     res = {
118                            'partner_address_id': contact.id,
119                            'email': contact.email
120                            }
121                     res.update(ref)
122                     vals.append(res)
123                     if contact.email:
124                         mail_to.append(contact.email)
125
126             for att_val in vals:
127                 if model == 'calendar.attendee':
128                     att = att_obj.browse(cr, uid, context_id)
129                     att_val.update({
130                         'parent_ids': [(4, att.id)],
131                         'ref': att.ref and (att.ref._name + ',' +str(att.ref.id)) or False
132                         })
133
134                 attendees.append(att_obj.create(cr, uid, att_val))
135             if model_field:
136                 for attendee in attendees:
137                     obj.write(cr, uid, res_obj.id, {model_field: [(4, attendee)]})
138
139             if datas.get('send_mail'):
140                 if not mail_to:
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))
146
147         return {'type': 'ir.actions.act_window_close'}
148
149
150     def onchange_partner_id(self, cr, uid, ids, partner_id, *args, **argv):
151         """
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.
158         """
159
160         if not partner_id:
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}}
166
167 base_calendar_invite_attendee()
168
169 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: