From 8c30b612014e994ce7e6e50395e250c0a7229c07 Mon Sep 17 00:00:00 2001 From: Shubham Padia Date: Sat, 30 Mar 2024 17:10:57 +0530 Subject: [PATCH] custom_profile_fields: Display_in_profile_summary optional for update. --- api_docs/changelog.md | 8 ++++---- zerver/actions/custom_profile_fields.py | 5 +++-- zerver/tests/test_custom_profile_data.py | 12 ++++++++++++ zerver/views/custom_profile_fields.py | 6 ++++-- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/api_docs/changelog.md b/api_docs/changelog.md index e5e5bf225e..5e6a667f02 100644 --- a/api_docs/changelog.md +++ b/api_docs/changelog.md @@ -22,10 +22,10 @@ format used by the Zulip server that they are interacting with. **Feature level 252** -* `PATCH /realm/profile_fields/{field_id}`: `name`, `hint` and `field_data` fields are - now optional during an update. Previously we required the clients to populate - the fields in the PATCH request even if there was no change to those fields' - values. +* `PATCH /realm/profile_fields/{field_id}`: `name`, `hint`, `display_in_profile_summary` + and `field_data` fields are now optional during an update. Previously we required + the clients to populate the fields in the PATCH request even if there was no + change to those fields' values. **Feature level 251** diff --git a/zerver/actions/custom_profile_fields.py b/zerver/actions/custom_profile_fields.py index e17fec2ad6..f3e694c1ff 100644 --- a/zerver/actions/custom_profile_fields.py +++ b/zerver/actions/custom_profile_fields.py @@ -104,7 +104,7 @@ def try_update_realm_custom_profile_field( name: Optional[str] = None, hint: Optional[str] = None, field_data: Optional[ProfileFieldData] = None, - display_in_profile_summary: bool = False, + display_in_profile_summary: Optional[bool] = None, required: bool = False, ) -> None: if name is not None: @@ -112,8 +112,9 @@ def try_update_realm_custom_profile_field( if hint is not None: field.hint = hint - field.display_in_profile_summary = display_in_profile_summary field.required = required + if display_in_profile_summary is not None: + field.display_in_profile_summary = display_in_profile_summary if field.field_type in ( CustomProfileField.SELECT, diff --git a/zerver/tests/test_custom_profile_data.py b/zerver/tests/test_custom_profile_data.py index 2205c9ecdc..e5a3e08716 100644 --- a/zerver/tests/test_custom_profile_data.py +++ b/zerver/tests/test_custom_profile_data.py @@ -581,6 +581,18 @@ class UpdateCustomProfileFieldTest(CustomProfileFieldTestCase): ) self.assert_json_success(result) + # Not sending display_in_profile_summary should not set it to false. + result = self.client_patch( + f"/json/realm/profile_fields/{field.id}", + info={ + "hint": "Fav editor", + }, + ) + field.refresh_from_db() + self.assertEqual(field.hint, "Fav editor") + self.assertEqual(field.display_in_profile_summary, True) + self.assert_json_success(result) + field = CustomProfileField.objects.get(name="Birthday", realm=realm) result = self.client_patch( f"/json/realm/profile_fields/{field.id}", diff --git a/zerver/views/custom_profile_fields.py b/zerver/views/custom_profile_fields.py index 5abbbf30b2..364af2cb92 100644 --- a/zerver/views/custom_profile_fields.py +++ b/zerver/views/custom_profile_fields.py @@ -118,7 +118,7 @@ def validate_custom_profile_field( def validate_custom_profile_field_update( field: CustomProfileField, - display_in_profile_summary: bool, + display_in_profile_summary: Optional[bool] = None, field_data: Optional[ProfileFieldData] = None, name: Optional[str] = None, hint: Optional[str] = None, @@ -134,6 +134,8 @@ def validate_custom_profile_field_update( field_data = {} else: field_data = orjson.loads(field.field_data) + if display_in_profile_summary is None: + display_in_profile_summary = field.display_in_profile_summary assert field_data is not None validate_custom_profile_field( @@ -245,8 +247,8 @@ def update_realm_custom_profile_field( field_data: Optional[ProfileFieldData] = REQ( default=None, json_validator=check_profile_field_data ), - display_in_profile_summary: bool = REQ(default=False, json_validator=check_bool), required: bool = REQ(default=False, json_validator=check_bool), + display_in_profile_summary: Optional[bool] = REQ(default=None, json_validator=check_bool), ) -> HttpResponse: realm = user_profile.realm try: