[MERGE] [IMP] hr: broadcast the welcome message only for new employee coming

from the recruitment, not all created employees.
Before this merge, every created employee was broadcasted to all existing users
of the same company. However when creating a batch of employees, for example
when configuring an new openerp instance, this was creating a lot of noise
in the inboxes.
Now, only recruited employee have a welcome message.

bzr revid: tde@openerp.com-20140218162958-2rr967jh9zu2spwe
This commit is contained in:
Thibault Delavallée 2014-02-18 17:29:58 +01:00
commit 79a0a10f93
2 changed files with 36 additions and 15 deletions

View File

@ -21,6 +21,7 @@
import logging
from openerp import SUPERUSER_ID
from openerp import tools
from openerp.modules.module import get_module_resource
from openerp.osv import fields, osv
@ -248,27 +249,46 @@ class hr_employee(osv.osv):
'color': 0,
}
def create(self, cr, uid, data, context=None):
if context is None:
context = {}
create_ctx = dict(context, mail_create_nolog=True)
employee_id = super(hr_employee, self).create(cr, uid, data, context=create_ctx)
def _broadcast_welcome(self, cr, uid, employee_id, context=None):
""" Broadcast the welcome message to all users in the employee company. """
employee = self.browse(cr, uid, employee_id, context=context)
partner_ids = []
_model, group_id = self.pool['ir.model.data'].get_object_reference(cr, uid, 'base', 'group_user')
if employee.user_id:
res_users = self.pool['res.users']
# send a copy to every user of the company
# TODO: post to the `Whole Company` mail.group when we'll be able to link to the employee record
_model, group_id = self.pool['ir.model.data'].get_object_reference(cr, uid, 'base', 'group_user')
user_ids = res_users.search(cr, uid, [('company_id', '=', employee.user_id.company_id.id),
('groups_id', 'in', group_id)])
partner_ids = list(set(u.partner_id.id for u in res_users.browse(cr, uid, user_ids, context=context)))
company_id = employee.user_id.company_id.id
elif employee.company_id:
company_id = employee.company_id.id
elif employee.job_id:
company_id = employee.job_id.company_id.id
elif employee.department_id:
company_id = employee.department_id.company_id.id
else:
partner_ids = []
self.message_post(cr, uid, [employee_id],
company_id = self.pool['res.company']._company_default_get(cr, uid, 'hr.employee', context=context)
res_users = self.pool['res.users']
user_ids = res_users.search(
cr, SUPERUSER_ID, [
('company_id', '=', company_id),
('groups_id', 'in', group_id)
], context=context)
partner_ids = list(set(u.partner_id.id for u in res_users.browse(cr, SUPERUSER_ID, user_ids, context=context)))
self.message_post(
cr, uid, [employee_id],
body=_('Welcome to %s! Please help him/her take the first steps with OpenERP!') % (employee.name),
partner_ids=partner_ids,
subtype='mail.mt_comment', context=context
)
return True
def create(self, cr, uid, data, context=None):
if context is None:
context = {}
if context.get("mail_broadcast"):
context['mail_create_nolog'] = True
employee_id = super(hr_employee, self).create(cr, uid, data, context=context)
if context.get("mail_broadcast"):
self._broadcast_welcome(cr, uid, employee_id, context=context)
return employee_id
def unlink(self, cr, uid, ids, context=None):

View File

@ -453,6 +453,7 @@ class hr_applicant(osv.Model):
contact_name = self.pool.get('res.partner').name_get(cr, uid, [applicant.partner_id.id])[0][1]
if applicant.job_id and (applicant.partner_name or contact_name):
applicant.job_id.write({'no_of_hired_employee': applicant.job_id.no_of_hired_employee + 1}, context=context)
create_ctx = dict(context, mail_broadcast=True)
emp_id = hr_employee.create(cr, uid, {'name': applicant.partner_name or contact_name,
'job_id': applicant.job_id.id,
'address_home_id': address_id,
@ -460,7 +461,7 @@ class hr_applicant(osv.Model):
'address_id': applicant.company_id and applicant.company_id.partner_id and applicant.company_id.partner_id.id or False,
'work_email': applicant.department_id and applicant.department_id.company_id and applicant.department_id.company_id.email or False,
'work_phone': applicant.department_id and applicant.department_id.company_id and applicant.department_id.company_id.phone or False,
})
}, context=create_ctx)
self.write(cr, uid, [applicant.id], {'emp_id': emp_id}, context=context)
self.pool['hr.job'].message_post(
cr, uid, [applicant.job_id.id],