Skip to content

Commit

Permalink
更新 WeChatDepartment (#712)
Browse files Browse the repository at this point in the history
* 更新 WeChatDepartment

* rename: get -> list, retrieve -> get

Co-authored-by: yaoqiankun <yaoqiankun@datarc.cn>
  • Loading branch information
yqkcn and yaoqiankun committed Mar 20, 2022
1 parent fdc4688 commit 64fb263
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 31 deletions.
4 changes: 2 additions & 2 deletions tests/test_work_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ def test_department_delete(self):
res = self.client.department.delete(2)
self.assertEqual(0, res["errcode"])

def test_department_get(self):
def test_department_list(self):
with HTTMock(wechat_api_mock):
res = self.client.department.get()
res = self.client.department.list()
self.assertEqual(2, len(res))

def test_department_get_users(self):
Expand Down
79 changes: 50 additions & 29 deletions wechatpy/work/client/api/department.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,56 +8,58 @@


class WeChatDepartment(BaseWeChatAPI):
"""
https://work.weixin.qq.com/api/doc#90000/90135/90204
"""

def create(self, name, parent_id=1, order=None, id=None):
def create(self, name, parent_id=1, order=None, id=None, name_en=None):
"""
创建部门
https://work.weixin.qq.com/api/doc#90000/90135/90205
详情请参考
https://developer.work.weixin.qq.com/document/path/90205
:param name: 部门名称。长度限制为1~32个字符,字符不能包括\\:?”<>|
:param parent_id: 父部门id,32位整型
:param order: 在父部门中的次序值。order值大的排序靠前。有效的值范围是[0, 2^32)
:param id: 部门id,32位整型,指定时必须大于1。若不填该参数,将自动生成id
:param name_en: 英文名称。同一个层级的部门名称不能重复。需要在管理后台开启多语言支持才能生效。长度限制为1~32个字符,字符不能包括:*?"<>|
:return: 返回的 JSON 数据包
"""
data = optionaldict(name=name, parentid=parent_id, order=order, id=id)
data = optionaldict(name=name, parentid=parent_id, order=order, id=id, name_en=name_en)
return self._post("department/create", data=data)

def update(self, id, name=None, parent_id=None, order=None):
def update(self, id, name=None, parent_id=None, order=None, name_en=None):
"""
更新部门
https://work.weixin.qq.com/api/doc#90000/90135/90206
详情请参考
https://developer.work.weixin.qq.com/document/path/90206
:param id: 部门 id
:param name: 部门名称。长度限制为1~32个字符,字符不能包括\\:?”<>|
:param parent_id: 父亲部门id
:param order: 在父部门中的次序值。order值大的排序靠前。有效的值范围是[0, 2^32)
:param name_en: 英文名称。同一个层级的部门名称不能重复。需要在管理后台开启多语言支持才能生效。长度限制为1~32个字符,字符不能包括:*?"<>|
:return: 返回的 JSON 数据包
"""
data = optionaldict(id=id, name=name, parentid=parent_id, order=order)
data = optionaldict(id=id, name=name, parentid=parent_id, order=order, name_en=name_en)
return self._post("department/update", data=data)

def delete(self, id):
"""
删除部门
https://work.weixin.qq.com/api/doc#90000/90135/90207
详情请参考
https://developer.work.weixin.qq.com/document/path/90207
:param id: 部门id。(注:不能删除根部门;不能删除含有子部门、成员的部门)
:return: 返回的 JSON 数据包
"""
return self._get("department/delete", params={"id": id})

def get(self, id=None):
def list(self, id=None):
"""
获取指定部门列表
https://work.weixin.qq.com/api/doc#90000/90135/90208
详情请参考
https://developer.work.weixin.qq.com/document/path/90208
权限说明:
只能拉取token对应的应用的权限范围内的部门列表
Expand All @@ -71,15 +73,42 @@ def get(self, id=None):
res = self._get("department/list", params={"id": id})
return res["department"]

def get_users(self, id, status=0, fetch_child=0, simple=True):
def simple_list(self, id=None):
"""
获取子部门 ID 列表,和 list 接口相比,此接口只返回部门 ID,ORDER 和 PARENTID 字段
详情请参考
https://developer.work.weixin.qq.com/document/path/95350
:param id: 部门id。获取指定部门及其下的子部门(以及子部门的子部门等等,递归)。 如果不填,默认获取全量组织架构
:return: 部门列表
"""
if id is None:
res = self._get("department/simplelist")
else:
res = self._get("department/simplelist", params={"id": id})
return res["department_id"]

def get(self, id):
"""
获取单个部门详情
详情请参考
https://developer.work.weixin.qq.com/document/path/95351
:param id: 部门 ID
:return: 部门信息
"""
res = self._get("department/get", params={"id": id})
return res["department"]

def get_users(self, id, fetch_child=0, simple=True):
"""
获取部门成员:https://work.weixin.qq.com/api/doc#90000/90135/90200
获取部门成员:https://developer.work.weixin.qq.com/document/path/90200
获取部门成员详情:https://work.weixin.qq.com/api/doc#90000/90135/90201
获取部门成员详情:https://developer.work.weixin.qq.com/document/path/90201
:param id: 部门 id
:param status: 0 获取全部员工,1 获取已关注成员列表,
2 获取禁用成员列表,4 获取未关注成员列表。可叠加
:param fetch_child: 1/0:是否递归获取子部门下面的成员
:param simple: True 获取部门成员,False 获取部门成员详情
:return: 部门成员列表
Expand All @@ -89,13 +118,12 @@ def get_users(self, id, status=0, fetch_child=0, simple=True):
url,
params={
"department_id": id,
"status": status,
"fetch_child": 1 if fetch_child else 0,
},
)
return res["userlist"]

def get_map_users(self, id=None, key="name", status=0, fetch_child=0):
def get_map_users(self, id=None, key="name", fetch_child=0):
"""
映射员工某详细字段到 ``user_id``
Expand All @@ -105,19 +133,12 @@ def get_map_users(self, id=None, key="name", status=0, fetch_child=0):
:param id: 部门 id, 如果不填,默认获取有权限的所有部门
:param key: 员工详细信息字段 key,所指向的值必须唯一
:param status: 0 获取全部员工,1 获取已关注成员列表,
2 获取禁用成员列表,4 获取未关注成员列表。可叠加
:param fetch_child: 1/0:是否递归获取子部门下面的成员
:return: dict - 部门成员指定字段到 user_id 的 map ``{ key: user_id }``
"""
ids = [id] if id is not None else [item["id"] for item in self.get()]
ids = [id] if id is not None else [item["id"] for item in self.list()]
users_info = list(
chain(
*[
self.get_users(department, status=status, fetch_child=fetch_child, simple=False)
for department in ids
]
)
chain(*[self.get_users(department, fetch_child=fetch_child, simple=False) for department in ids])
)
users_zip = [(user[key], user["userid"]) for user in users_info]
return dict(users_zip)

0 comments on commit 64fb263

Please sign in to comment.