This question has been flagged
1 Reply
6706 Views

If I set a meeting with 2 attendees who are some OpenERP users they receive 2 mails:

  • One at the meeting creation
  • A second when an invitation is validate

this validation create a new meeting with the same attendees and send email to them, again.

How to prevent this second email?

regards

edit: I try this:

class calendar_attendee(osv.osv):
_name = 'calendar.attendee'
_inherit = 'calendar.attendee'

def _send_mail(self, cr, uid, ids, mail_to, email_from=tools.config.get('email_from', False), context=None):
    """
    Send mail for event invitation to event attendees.
    @param email_from: email address for user sending the mail
    @return: True
    """
    company = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.name
    for att in self.browse(cr, uid, ids, context=context):
        sign = att.sent_by_uid and att.sent_by_uid.signature or ''
        sign = '<br>'.join(sign and sign.split('\n') or [])
        res_obj = att.ref
        if res_obj:
            att_infos = []
            sub = res_obj.name
            other_invitation_ids = self.search(cr, uid, [('ref', '=', res_obj._name + ',' + str(res_obj.id))])
            for att2 in self.browse(cr, uid, other_invitation_ids):
                att_infos.append(((att2.user_id and att2.user_id.name) or \
                             (att2.partner_id and att2.partner_id.name) or \
                                att2.email) + ' - Status: ' + att2.state.title())
            #dates and times are gonna be expressed in `tz` time (local timezone of the `uid`)
            tz = context.get('tz', pytz.timezone('UTC'))
            #res_obj.date and res_obj.date_deadline are in UTC in database so we use context_timestamp() to transform them in the `tz` timezone
            date_start = fields.datetime.context_timestamp(cr, uid, datetime.strptime(res_obj.date, tools.DEFAULT_SERVER_DATETIME_FORMAT), context=context)
            date_stop = False
            if res_obj.date_deadline:
                date_stop = fields.datetime.context_timestamp(cr, uid, datetime.strptime(res_obj.date_deadline, tools.DEFAULT_SERVER_DATETIME_FORMAT), context=context)
            body_vals = {'name': res_obj.name,
                        'start_date': date_start,
                        'end_date': date_stop,
                        'timezone': tz,
                        'description': res_obj.description or '-',
                        'location': res_obj.location or '-',
                        'attendees': '<br>'.join(att_infos),
                        'user': res_obj.user_id and res_obj.user_id.name or 'OpenERP User',
                        'sign': sign,
                        'company': company
            }
            body = html_invitation2 % body_vals
            if mail_to and email_from:
                ics_file = self.get_ics_file(cr, uid, res_obj, context=context)
                vals = {'email_from': email_from,
                        'email_to': mail_to,
                        'state': 'outgoing',
                        'subject': sub,
                        'body_html': body,
                        'auto_delete': True}
                if ics_file:
                    vals['attachment_ids'] = [(0,0,{'name': 'invitation.ics',
                                                    'datas_fname': 'invitation.ics',
                                                    'datas': str(ics_file).encode('base64')})]
                self.pool.get('mail.mail').create(cr, uid, vals, context=context)
        return True
def do_accept(self, cr, uid, ids, context=None, *args):
    """
    Update state of invitation as Accepted and if the invited user is other
    then event user it will make a copy of this event for invited user.
    @param cr: the current row, from the database cursor
    @param uid: the current user's ID for security checks
    @param ids: list of calendar attendee's IDs
    @param context: a standard dictionary for contextual values
    @return: True
    """
    if context is None:
        context = {}

    for vals in self.browse(cr, uid, ids, context=context):
        if vals.ref and vals.ref.user_id:
            mod_obj = self.pool.get(vals.ref._name)
            res=mod_obj.read(cr,uid,[vals.ref.id],['duration','class'],context)
            defaults = {'user_id': vals.user_id.id, 'organizer_id': vals.ref.user_id.id,'duration':res[0]['duration'],'class':res[0]['class']}
            if 'already_send' not in context :
                _logger.debug('               pas encore     ')
                context['already_send']=True
                mod_obj.copy(cr, uid, vals.ref.id, default=defaults, context=context)
                del context['already_send']
            else:
                _logger.debug('               oui     ')
        self.write(cr, uid, vals.id, {'state': 'accepted'}, context)

    return True
class crm_meeting(osv.osv):
    _name='crm.meeting'
    _inherit = 'crm.meeting'
def create_attendees(self, cr, uid, ids, context):
        #définition du context
        context = context or {}
        att_obj = self.pool.get('calendar.attendee')
        user_obj = self.pool.get('res.users')
        current_user = user_obj.browse(cr, uid, uid, context=context)
        for event in self.browse(cr, uid, ids, context):
            attendees = {}
            for att in event.attendee_ids:
                attendees[att.partner_id.id] = True
            new_attendees = []
            mail_to = ""
            for partner in event.partner_ids:
                if partner.id in attendees:
                    continue
                att_id = self.pool.get('calendar.attendee').create(cr, uid, {
                    'partner_id': partner.id,
                    'user_id': partner.user_ids and partner.user_ids[0].id or False,
                    'ref': self._name+','+str(event.id),
                    'email': partner.email
                }, context=context)
                if partner.email:
                    mail_to = mail_to + " " + partner.email
                self.write(cr, uid, [event.id], {
                    'attendee_ids': [(4, att_id)]
                }, context=context)
                new_attendees.append(att_id)

            if mail_to and current_user.email and 'already_send' not in context:
                att_obj._send_mail(cr, uid, new_attendees, mail_to,
                    email_from = current_user.email, context=context)
            att_obj.do_accept(cr, uid, new_attendees, context=context)
        return True

And it's working.

Avatar
Discard

File: <openerp_home>/addons/base_calendar/base_calendar.py

Author Best Answer

I found the solution by myself. See question edit for the code.

Avatar
Discard