users: Send custom profile fields with the /profile endpoint.

This commit is contained in:
Hemanth V. Alluri 2019-10-25 23:58:53 +05:30 committed by Tim Abbott
parent d6377b00c0
commit 5b3e346369
3 changed files with 65 additions and 1 deletions

View File

@ -1232,6 +1232,13 @@ paths:
type: integer type: integer
description: The user's ID. description: The user's ID.
example: 1 example: 1
profile_data:
type: object
description: A dictionary containing custom profile field data for the user.
Each entry maps the integer ID of a custom profile field in the organization
to a dictionary containing the user's data for that field. Generally the
data includes just a single `value` key; for those custom profile fields
supporting markdown, a `rendered_value` key will also be present.
- example: - example:
{ {
"avatar_url": "https://secure.gravatar.com/avatar/af4f06322c177ef4e1e9b2c424986b54?d=identicon&version=1", "avatar_url": "https://secure.gravatar.com/avatar/af4f06322c177ef4e1e9b2c424986b54?d=identicon&version=1",
@ -1245,7 +1252,36 @@ paths:
"pointer": -1, "pointer": -1,
"result": "success", "result": "success",
"short_name": "iago", "short_name": "iago",
"user_id": 5 "user_id": 5,
"profile_data": {
"5": {
"value": "2000-1-1"
},
"4": {
"value": "emacs"
},
"7": {
"value": "[10]"
},
"1": {
"value": "+1-234-567-8901",
"rendered_value": "<p>+1-234-567-8901</p>"
},
"2": {
"rendered_value": "<p>Betrayer of Othello.</p>",
"value": "Betrayer of Othello."
},
"8": {
"value": "zulip"
},
"3": {
"value": "Apples",
"rendered_value": "<p>Apples</p>"
},
"6": {
"value": "https://zulip.readthedocs.io/en/latest/"
}
}
} }
delete: delete:
description: Delete the requesting user from the realm. description: Delete the requesting user from the realm.

View File

@ -684,6 +684,19 @@ class ListCustomProfileFieldTest(CustomProfileFieldTestCase):
with self.assertRaises(KeyError): with self.assertRaises(KeyError):
user_dict["profile_data"] user_dict["profile_data"]
def test_get_custom_profile_fields_from_api_for_single_user(self) -> None:
self.login(self.example_email("iago"))
expected_keys = {
"result", "msg", "pointer", "client_id", "max_message_id", "user_id",
"avatar_url", "full_name", "email", "is_bot", "is_admin", "short_name",
"profile_data"}
url = "/json/users/me"
response = self.client_get(url)
self.assertEqual(response.status_code, 200)
raw_user_data = response.json()
self.assertEqual(set(raw_user_data.keys()), expected_keys)
class ReorderCustomProfileFieldTest(CustomProfileFieldTestCase): class ReorderCustomProfileFieldTest(CustomProfileFieldTestCase):
def test_reorder(self) -> None: def test_reorder(self) -> None:

View File

@ -469,6 +469,21 @@ def get_profile_backend(request: HttpRequest, user_profile: UserProfile) -> Http
is_admin = user_profile.is_realm_admin, is_admin = user_profile.is_realm_admin,
short_name = user_profile.short_name) short_name = user_profile.short_name)
if not user_profile.is_bot:
custom_profile_field_values = user_profile.customprofilefieldvalue_set.all()
profile_data = dict() # type: Dict[int, Dict[str, Any]]
for profile_field in custom_profile_field_values:
if profile_field.field.is_renderable():
profile_data[profile_field.field_id] = {
"value": profile_field.value,
"rendered_value": profile_field.rendered_value
}
else:
profile_data[profile_field.field_id] = {
"value": profile_field.value
}
result["profile_data"] = profile_data
messages = Message.objects.filter(usermessage__user_profile=user_profile).order_by('-id')[:1] messages = Message.objects.filter(usermessage__user_profile=user_profile).order_by('-id')[:1]
if messages: if messages:
result['max_message_id'] = messages[0].id result['max_message_id'] = messages[0].id