api: Refactor get_members_backend in zerver/views/users.py.

This refactors get_members_backend to return user data of a single
user in the form of a dictionary (earlier being a list with a single
dictionary).

This also refactors it to return the data with an appropriate key
(inside a dictionary), "user" or "members", according to the type of
data being returned.

Tweaked by tabbott to use somewhat less opaque code and simple OpenAPI
descriptions.
This commit is contained in:
akashaviator 2020-03-09 01:43:11 +05:30 committed by Tim Abbott
parent 2c75b39078
commit 9c63976da5
6 changed files with 33 additions and 30 deletions

View File

@ -54,9 +54,9 @@ You may pass the `client_gravatar` query parameter as follows:
#### Return values
* `members`: A list of dictionaries where each dictionary contains information
about a particular user or bot.
* `email`: The email address of the user or bot..
* `members`: A list of dictionaries, each containing the details for
one of the users in the organization.
* `email`: The email address of the user or bot.
* `is_bot`: A boolean specifying whether the user is a bot or not.
* `avatar_url`: URL to the user's gravatar. `None` if the `client_gravatar`
query paramater was set to `True`.

View File

@ -33,8 +33,7 @@ You may pass the `client_gravatar` or `include_custom_profile_fields` query para
#### Return values
* `members`: A list with a single dictionary that contains information
about a particular user or bot.
* `user`: A dictionary that contains the requested user's details.
* `email`: The email address of the user or bot.
* `is_bot`: A boolean specifying whether the user is a bot or not.
* `avatar_url`: URL to the user's gravatar. `None` if the `client_gravatar`

View File

@ -1006,9 +1006,8 @@ paths:
- properties:
members:
type: array
description: A list of `member` objects, each containing
details of a user in the realm (like their email, name,
avatar, user type...).
description: A list of `user` objects, each containing
details about a user in the organization.
- example:
{
"msg": "",
@ -1171,17 +1170,15 @@ paths:
allOf:
- $ref: '#/components/schemas/JsonSuccess'
- properties:
members:
type: array
description: A list `member` containing a single object with
details of a user in the realm (like their email, name,
avatar, user type...).
user:
type: object
description: |
A dictionary containing the requested user's details.
- example:
{
"msg": "",
"result": "success",
"members": [
{
"user": {
"date_joined": "2019-10-20T07:50:53.729659+00:00",
"full_name": "King Hamlet",
"is_guest": false,
@ -1223,7 +1220,8 @@ paths:
"is_active": true,
"email": "hamlet@zulip.com"
}
]
}
patch:

View File

@ -421,16 +421,16 @@ class RealmTest(ZulipTestCase):
# Check normal user cannot access email
result = self.api_get(cordelia.delivery_email, "/api/v1/users/%s" % (hamlet.id,))
self.assert_json_success(result)
self.assertEqual(result.json()['members'][0]['email'],
self.assertEqual(result.json()['user']['email'],
'user%s@zulip.testserver' % (hamlet.id,))
self.assertEqual(result.json()['members'][0].get('delivery_email'), None)
self.assertEqual(result.json()['user'].get('delivery_email'), None)
# Check administrator gets delivery_email with EMAIL_ADDRESS_VISIBILITY_ADMINS
result = self.api_get(user_profile.delivery_email, "/api/v1/users/%s" % (hamlet.id,))
self.assert_json_success(result)
self.assertEqual(result.json()['members'][0]['email'],
self.assertEqual(result.json()['user']['email'],
'user%s@zulip.testserver' % (hamlet.id,))
self.assertEqual(result.json()['members'][0].get('delivery_email'),
self.assertEqual(result.json()['user'].get('delivery_email'),
hamlet.delivery_email)
req = dict(email_address_visibility = ujson.dumps(Realm.EMAIL_ADDRESS_VISIBILITY_NOBODY))
@ -446,9 +446,9 @@ class RealmTest(ZulipTestCase):
# EMAIL_ADDRESS_VISIBILITY_NOBODY
result = self.api_get(user_profile.delivery_email, "/api/v1/users/%s" % (hamlet.id,))
self.assert_json_success(result)
self.assertEqual(result.json()['members'][0]['email'],
self.assertEqual(result.json()['user']['email'],
'user%s@zulip.testserver' % (hamlet.id,))
self.assertEqual(result.json()['members'][0].get('delivery_email'), None)
self.assertEqual(result.json()['user'].get('delivery_email'), None)
def test_change_stream_creation_policy(self) -> None:
# We need an admin user.

View File

@ -1341,16 +1341,16 @@ class GetProfileTest(ZulipTestCase):
# Tests the GET ../users/{id} api endpoint.
user = self.example_user('hamlet')
result = ujson.loads(self.client_get('/json/users/{}'.format(user.id)).content)
self.assertEqual(result['members'][0]['email'], user.email)
self.assertEqual(result['members'][0]['full_name'], user.full_name)
self.assertIn("user_id", result['members'][0])
self.assertNotIn("profile_data", result['members'][0])
self.assertFalse(result['members'][0]['is_bot'])
self.assertFalse(result['members'][0]['is_admin'])
self.assertEqual(result['user']['email'], user.email)
self.assertEqual(result['user']['full_name'], user.full_name)
self.assertIn("user_id", result['user'])
self.assertNotIn("profile_data", result['user'])
self.assertFalse(result['user']['is_bot'])
self.assertFalse(result['user']['is_admin'])
result = ujson.loads(self.client_get('/json/users/{}?include_custom_profile_fields=true'.format(user.id)).content)
self.assertIn('profile_data', result['members'][0])
self.assertIn('profile_data', result['user'])
result = self.client_get('/json/users/{}?'.format(30))
self.assert_json_error(result, "No such user")

View File

@ -420,7 +420,13 @@ def get_members_backend(request: HttpRequest, user_profile: UserProfile, user_id
members = get_raw_user_data(realm, user_profile, client_gravatar=client_gravatar,
target_user=target_user,
include_custom_profile_fields=include_custom_profile_fields)
return json_success({'members': members.values()})
if target_user is not None:
data = {"user": members[target_user.id]} # type: Dict[str, Any]
else:
data = {"members": [members[k] for k in members]}
return json_success(data)
@require_realm_admin
@has_request_variables