Repository: zulip/zulip

Issue:
Save failed: Only 2 custom profile fields can be displayed in the profile summary.
I tried to set a custom profile field to be displayed in the profile summary. However, it fails with an error message "Save failed: Only 2 custom profile fields can be displayed in the profile summary." even though no custom profile field has been set to displayed.

https://user-images.githubusercontent.com/2891235/198561250-59519f45-85a4-4490-8f72-9fae343930be.mov


Server: hosted Zulip server
```console
Zulip Server
Version 6.0-dev-2021-gd461383c1f 
Forked from upstream at 6.0-dev-2009-g0affc7ac6f
```

Client: Zulip Desktop v5.9.3


Discussion:
@yogesh-sirsat , @sahil839 Any ideas on what might be going on here / how we can debug it?
Hello @zulip/server-settings members, this issue was labeled with the "area: settings (admin/org)" label, so you may want to check it out!

<!-- areaLabelAddition -->

Hey @caizixian, is it the first time you are setting custom profile fields to display in profile summary? 
I can't reproduce this error on main, but it is reproducible on domainname.zulipchat.com

P.S. Not a frontend bug I think because I tried sending an API request and got the same error.
It is not even reproducible on branch 6.0-beta1, @alya @sahil839 which branch do we use for subdomain.zulipchat.com?
@yogesh-sirsat

>  is it the first time you are setting custom profile fields to display in profile summary?

Yes

> I can't reproduce this error on main, but it is reproducible on domainname.zulipchat.com

Yes, I had the error on one of the *domainname*.zulipchat.com

Resolving patch:
diff --git a/zerver/views/custom_profile_fields.py b/zerver/views/custom_profile_fields.py
--- a/zerver/views/custom_profile_fields.py
+++ b/zerver/views/custom_profile_fields.py
@@ -33,7 +33,7 @@
     check_union,
     validate_select_field_data,
 )
-from zerver.models import CustomProfileField, UserProfile, custom_profile_fields_for_realm
+from zerver.models import CustomProfileField, Realm, UserProfile, custom_profile_fields_for_realm
 
 
 def list_realm_custom_profile_fields(
@@ -135,8 +135,10 @@ def update_only_display_in_profile_summary(
     return True
 
 
-def display_in_profile_summary_limit_reached(profile_field_id: Optional[int] = None) -> bool:
-    query = CustomProfileField.objects.filter(display_in_profile_summary=True)
+def display_in_profile_summary_limit_reached(
+    realm: Realm, profile_field_id: Optional[int] = None
+) -> bool:
+    query = CustomProfileField.objects.filter(realm=realm, display_in_profile_summary=True)
     if profile_field_id is not None:
         query = query.exclude(id=profile_field_id)
     return query.count() >= CustomProfileField.MAX_DISPLAY_IN_PROFILE_SUMMARY_FIELDS
@@ -153,7 +155,7 @@ def create_realm_custom_profile_field(
     field_type: int = REQ(json_validator=check_int),
     display_in_profile_summary: bool = REQ(default=False, json_validator=check_bool),
 ) -> HttpResponse:
-    if display_in_profile_summary and display_in_profile_summary_limit_reached():
+    if display_in_profile_summary and display_in_profile_summary_limit_reached(user_profile.realm):
         raise JsonableError(
             _("Only 2 custom profile fields can be displayed in the profile summary.")
         )
@@ -213,7 +215,9 @@ def update_realm_custom_profile_field(
     except CustomProfileField.DoesNotExist:
         raise JsonableError(_("Field id {id} not found.").format(id=field_id))
 
-    if display_in_profile_summary and display_in_profile_summary_limit_reached(field.id):
+    if display_in_profile_summary and display_in_profile_summary_limit_reached(
+        user_profile.realm, field.id
+    ):
         raise JsonableError(
             _("Only 2 custom profile fields can be displayed in the profile summary.")
         )


Test patch: