Example #1
0
 def get_tags_users(self):
     """
     获取标签成员列表
     :return:
     """
     for res in self:
         user_list = []
         dept_list = []
         try:
             client = WeChatTag(address_client())
             result = client.get_users(res.wx_ent_id)
         except Exception as e:
             raise UserError(str(e))
         if result['errcode'] != 0:
             raise UserError(result['errmsg'])
         for user in result['userlist']:
             emp = self.env['hr.employee'].search(
                 [('ent_wx_id', '=', user.get('userid'))], limit=1)
             if emp:
                 user_list.append(emp.id)
         for party in result['partylist']:
             dept = self.env['hr.department'].search(
                 [('ent_wx_id', '=', party)], limit=1)
             if dept:
                 dept_list.append(dept.id)
         res.write({
             'employee_ids': [(6, 0, user_list)],
             'department_ids': [(6, 0, dept_list)]
         })
         res.message_post(body=u"已成功获取{}条成员信息和{}条部门信息!".format(
             len(user_list), len(dept_list)),
                          message_type='notification')
Example #2
0
 def update_weixin_ent_tags(self):
     """
     更新标签纸企业微信
     :return:
     """
     for res in self:
         try:
             client = WeChatTag(address_client())
             result = client.update(res.wx_ent_id, res.name)
         except Exception as e:
             raise UserError(str(e))
         if result['errcode'] != 0:
             raise UserError(result['errmsg'])
         res.message_post(body=u"已更新至企业微信中!", message_type='notification')
Example #3
0
 def unlink(self):
     """
     删除标签时同时删除企业微信中的标签
     :return:
     """
     for res in self:
         if res.wx_ent_id:
             try:
                 client = WeChatTag(address_client())
                 result = client.delete(res.wx_ent_id)
             except Exception as e:
                 raise UserError(str(e))
             if result['errcode'] != 0:
                 raise UserError(result['errmsg'])
         super(EmployeeTags, self).unlink()
Example #4
0
 def create(self, values):
     """
     创建时将标签自动推送到企业微信
     :param values:
     :return:
     """
     if 'api_state' not in values or values['api_state']:
         try:
             client = WeChatTag(address_client())
             result = client.create(values.get('name'))
         except Exception as e:
             raise UserError(str(e))
         if result['errcode'] != 0:
             raise UserError(result['errmsg'])
         values['wx_ent_id'] = result['tagid']
     values['api_state'] = True
     return super(EmployeeTags, self).create(values)
 def delete_employee(self):
     """
     删除标签成员方法
     :return:
     """
     self.ensure_one()
     tag_id = self.tag_id
     tags = self.env['hr.employee.tags'].search([('wx_ent_id', '=', tag_id)
                                                 ])
     emp_ex_id = list()
     for emp in self.emp_ids:
         emp_ex_id.append(emp.ent_wx_id)
     try:
         client = WeChatTag(address_client())
         result = client.delete_users(tag_id, emp_ex_id)
     except Exception as e:
         raise UserError(str(e))
     if result['errcode'] != 0:
         raise UserError(result['errmsg'])
     tags.write({'employee_ids': [(2, 0, self.emp_ids.ids)]})
     tags.message_post(body=u"已成功删除{}条成员!".format(len(self.emp_ids)),
                       message_type='notification')
     return {'type': 'ir.actions.act_window_close'}
 def pull_employee_tags(self):
     """
     拉取成员标签
     :return:
     """
     try:
         client = WeChatTag(address_client())
         result = client.list()
     except Exception as e:
         raise UserError(str(e))
     for res in result:
         data = {
             'name': res['tagname'],
             'wx_ent_id': res['tagid'],
             'api_state': False,  # 标识是同步下来的记录,不再通过创建、修改时上传操作
         }
         tags = self.env['hr.employee.tags'].search([('wx_ent_id', '=',
                                                      res['tagid'])])
         if tags:
             tags.write(data)
         else:
             self.env['hr.employee.tags'].create(data)
     return {'type': 'ir.actions.client', 'tag': 'reload'}
 def add_employee(self):
     """
     将选择的成员添加到标签中
     :return:
     """
     self.ensure_one()
     tag_id = self.tag_id
     tags = self.env['hr.employee.tags'].search([('wx_ent_id', '=', tag_id)
                                                 ])
     tag_emp_ids = tags.employee_ids.ids
     for emp in self.emp_ids:
         tag_emp_ids.append(emp.id)
         try:
             client = WeChatTag(address_client())
             result = client.add_users(tag_id, emp.ent_wx_id)
         except Exception as e:
             raise UserError(str(e))
         if result['errcode'] != 0:
             raise UserError(result['errmsg'])
     new_ids = list(set(tag_emp_ids))
     tags.write({'employee_ids': [(6, 0, new_ids)]})
     tags.message_post(body=u"已成功添加{}条成员到标签中!".format(len(self.emp_ids)),
                       message_type='notification')
     return {'type': 'ir.actions.act_window_close'}