Skip to content

Commit

Permalink
Merge pull request #191 from longguikeji/zzr/get_user_by_usernames
Browse files Browse the repository at this point in the history
feat(get user): 支持通过一组用户名高级搜索
  • Loading branch information
RockLi authored Jul 10, 2020
2 parents bb7c968 + e88f875 commit 968e17f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion siteapi/v1/blueprint.md
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@ TODO: 校对
# Group User
用户管理

## 所有用户 [/user/{?keyword,wechat_unionid,name,name__icontains,username,username__icontains,email,email__icontains,private_email,private_email__icontains,mobile,mobile__icontains,gender,remark,remark__icontains,created__lte,created__gte,last_active_time__lte,last_active_time__gte,unbound_wechat,unbound_ding,unbound_qq,unbound_alipay,user_ids,group_uids,%2Dgroup_uids,perm_uids,%2Dperm_uids,sort,%2A__custom,%2A__lt__custom,%2A__lte__custom,%2A__gt__custom,%2A__gte__custom,page,page_size}]
## 所有用户 [/user/{?keyword,wechat_unionid,name,name__icontains,username,username__icontains,email,email__icontains,private_email,private_email__icontains,mobile,mobile__icontains,gender,remark,remark__icontains,created__lte,created__gte,last_active_time__lte,last_active_time__gte,unbound_wechat,unbound_ding,unbound_qq,unbound_alipay,user_ids,usernames,group_uids,%2Dgroup_uids,perm_uids,%2Dperm_uids,sort,%2A__custom,%2A__lt__custom,%2A__lte__custom,%2A__gt__custom,%2A__gte__custom,page,page_size}]

### 创建用户 [POST]
+ Request JSON Message
Expand Down Expand Up @@ -1014,6 +1014,7 @@ TODO: 校对
+ unbound_qq (boolean, optional) - 未关联QQ搜索;'true'表示搜索未关联QQ的用户,'false'表示搜索关联QQ的用户
+ unbound_alipay (boolean, optional) - 未关联支付宝搜索;'true'表示搜索未关联支付宝的用户,'false'表示搜索关联支付宝的用户
+ user_ids (string, optional) - 指定用户id的搜索;形如'&=id1 id2 ... id3',参数间用空格分隔
+ usernames (string, optional) - 指定用户名的搜索;形如'&=username1 username2 ... username3',参数间用空格分隔
+ group_uids (string, optional) - 指定属于一些组的搜索;形如'&=group1 group2 ... group3',参数间用空格分隔
+ %2Dgroup_uids (string, optional) - 指定不属于一些组的搜索;形如'&=group1 group2 ... group3',参数间用空格分隔
+ perm_uids (string, optional) - 指定拥有一些权限的搜索;形如'&=perm1 perm2 ... perm3',参数间用空格分隔
Expand Down
6 changes: 6 additions & 0 deletions siteapi/v1/tests/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,12 @@ def test_advanced_search_user(self): # pylint:disable=too-many-locals
res = self.client.get(reverse('siteapi:user_list'), {'-perm_uids': 'system_oneid_all'})
self.assertEqual(res.json()['count'], 1)
self.assertNotIn(res.json()['results'][0]['username'], _random_users)
# 测试通过 usernames 搜索
_random_users = random.sample(['employee1', 'employee2', 'employee3'], 2)
res = self.client.get(reverse('siteapi:user_list'), {'usernames': ' '.join(_random_users)})
self.assertEqual(res.json()['count'], 2)
for user in res.json()['results']:
self.assertIn(str(user['username']), _random_users)
# 测试通过 sort 获取指定排序搜索
_random_users = ['employee1', 'employee2', 'employee3']
random.shuffle(_random_users)
Expand Down
7 changes: 7 additions & 0 deletions siteapi/v1/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ def get_queryset(self):
user_ids = self._get_user_ids(self.request.query_params.get('user_ids', ''), 'user')
queryset = queryset.filter(pk__in=user_ids) if user_ids is not None else queryset

# 支持通过 usernames 搜索
# QueryString 中格式为 '&usernames=username1 ... usernamen'
usernames = self.request.query_params.get('usernames', '')
if usernames != '':
usernames = usernames.split(' ')
queryset = queryset.filter(username__in=usernames)

# 支持通过 group_uid 搜索 (保留属于group_uids[]的用户)
# QueryString 中格式为 '&group_uids=uid1 ... uidn'
user_ids = self._get_user_ids(self.request.query_params.get('group_uids', ''), 'group')
Expand Down

0 comments on commit 968e17f

Please sign in to comment.