diff --git a/siteapi/v1/blueprint.md b/siteapi/v1/blueprint.md index bd75a941c..e88e4aad3 100644 --- a/siteapi/v1/blueprint.md +++ b/siteapi/v1/blueprint.md @@ -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 @@ -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',参数间用空格分隔 diff --git a/siteapi/v1/tests/test_user.py b/siteapi/v1/tests/test_user.py index 66d1c975d..9ca033679 100644 --- a/siteapi/v1/tests/test_user.py +++ b/siteapi/v1/tests/test_user.py @@ -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) diff --git a/siteapi/v1/views/user.py b/siteapi/v1/views/user.py index b449dda7a..e9bde496d 100644 --- a/siteapi/v1/views/user.py +++ b/siteapi/v1/views/user.py @@ -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')