Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
actions: Split out zerver.actions.custom_profile_fields.
Anders Kaseorgbbce879c8177
diff --git a/zerver/actions/custom_profile_fields.py b/zerver/actions/custom_profile_fields.pynew file mode 100644index 00000000000..11ae58cad5e--- /dev/null+++ b/zerver/actions/custom_profile_fields.py@@ -0,0 +1,185 @@+from typing import Dict, List, Optional, Union++import orjson+from django.db import transaction+from django.utils.translation import gettext as _++from zerver.lib.exceptions import JsonableError+from zerver.lib.external_accounts import DEFAULT_EXTERNAL_ACCOUNTS+from zerver.lib.streams import render_stream_description+from zerver.lib.types import ProfileDataElementValue, ProfileFieldData+from zerver.models import (+ CustomProfileField,+ CustomProfileFieldValue,+ Realm,+ UserProfile,+ active_user_ids,+ custom_profile_fields_for_realm,+)+from zerver.tornado.django_api import send_event+++def notify_realm_custom_profile_fields(realm: Realm) -> None:+ fields = custom_profile_fields_for_realm(realm.id)+ event = dict(type="custom_profile_fields", fields=[f.as_dict() for f in fields])+ send_event(realm, event, active_user_ids(realm.id))+++def try_add_realm_default_custom_profile_field(+ realm: Realm, field_subtype: str+) -> CustomProfileField:+ field_data = DEFAULT_EXTERNAL_ACCOUNTS[field_subtype]+ custom_profile_field = CustomProfileField(+ realm=realm,+ name=field_data["name"],+ field_type=CustomProfileField.EXTERNAL_ACCOUNT,+ hint=field_data["hint"],+ field_data=orjson.dumps(dict(subtype=field_subtype)).decode(),+ )+ custom_profile_field.save()+ custom_profile_field.order = custom_profile_field.id+ custom_profile_field.save(update_fields=["order"])+ notify_realm_custom_profile_fields(realm)+ return custom_profile_field+++def try_add_realm_custom_profile_field(+ realm: Realm,+ name: str,+ field_type: int,+ hint: str = "",+ field_data: Optional[ProfileFieldData] = None,+) -> CustomProfileField:+ custom_profile_field = CustomProfileField(realm=realm, name=name, field_type=field_type)+ custom_profile_field.hint = hint+ if (+ custom_profile_field.field_type == CustomProfileField.SELECT+ or custom_profile_field.field_type == CustomProfileField.EXTERNAL_ACCOUNT+ ):+ custom_profile_field.field_data = orjson.dumps(field_data or {}).decode()++ custom_profile_field.save()+ custom_profile_field.order = custom_profile_field.id+ custom_profile_field.save(update_fields=["order"])+ notify_realm_custom_profile_fields(realm)+ return custom_profile_field+++def do_remove_realm_custom_profile_field(realm: Realm, field: CustomProfileField) -> None:+ """+ Deleting a field will also delete the user profile data+ associated with it in CustomProfileFieldValue model.+ """+ field.delete()+ notify_realm_custom_profile_fields(realm)+++def do_remove_realm_custom_profile_fields(realm: Realm) -> None:+ CustomProfileField.objects.filter(realm=realm).delete()+++def try_update_realm_custom_profile_field(+ realm: Realm,+ field: CustomProfileField,+ name: str,+ hint: str = "",+ field_data: Optional[ProfileFieldData] = None,+) -> None:+ field.name = name+ field.hint = hint+ if (+ field.field_type == CustomProfileField.SELECT+ or field.field_type == CustomProfileField.EXTERNAL_ACCOUNT+ ):+ field.field_data = orjson.dumps(field_data or {}).decode()+ field.save()+ notify_realm_custom_profile_fields(realm)+++def try_reorder_realm_custom_profile_fields(realm: Realm, order: List[int]) -> None:+ order_mapping = {_[1]: _[0] for _ in enumerate(order)}+ custom_profile_fields = CustomProfileField.objects.filter(realm=realm)+ for custom_profile_field in custom_profile_fields:+ if custom_profile_field.id not in order_mapping:+ raise JsonableError(_("Invalid order mapping."))+ for custom_profile_field in custom_profile_fields:+ custom_profile_field.order = order_mapping[custom_profile_field.id]+ custom_profile_field.save(update_fields=["order"])+ notify_realm_custom_profile_fields(realm)+++def notify_user_update_custom_profile_data(+ user_profile: UserProfile, field: Dict[str, Union[int, str, List[int], None]]+) -> None:+ data = dict(id=field["id"], value=field["value"])++ if field["rendered_value"]:+ data["rendered_value"] = field["rendered_value"]+ payload = dict(user_id=user_profile.id, custom_profile_field=data)+ event = dict(type="realm_user", op="update", person=payload)+ send_event(user_profile.realm, event, active_user_ids(user_profile.realm.id))+++def do_update_user_custom_profile_data_if_changed(+ user_profile: UserProfile,+ data: List[Dict[str, Union[int, ProfileDataElementValue]]],+) -> None:+ with transaction.atomic():+ for custom_profile_field in data:+ field_value, created = CustomProfileFieldValue.objects.get_or_create(+ user_profile=user_profile, field_id=custom_profile_field["id"]+ )++ # field_value.value is a TextField() so we need to have field["value"]+ # in string form to correctly make comparisons and assignments.+ if isinstance(custom_profile_field["value"], str):+ custom_profile_field_value_string = custom_profile_field["value"]+ else:+ custom_profile_field_value_string = orjson.dumps(+ custom_profile_field["value"]+ ).decode()++ if not created and field_value.value == custom_profile_field_value_string:+ # If the field value isn't actually being changed to a different one,+ # we have nothing to do here for this field.+ continue++ field_value.value = custom_profile_field_value_string+ if field_value.field.is_renderable():+ field_value.rendered_value = render_stream_description(+ custom_profile_field_value_string+ )+ field_value.save(update_fields=["value", "rendered_value"])+ else:+ field_value.save(update_fields=["value"])+ notify_user_update_custom_profile_data(+ user_profile,+ {+ "id": field_value.field_id,+ "value": field_value.value,+ "rendered_value": field_value.rendered_value,+ "type": field_value.field.field_type,+ },+ )+++def check_remove_custom_profile_field_value(user_profile: UserProfile, field_id: int) -> None:+ try:+ custom_profile_field = CustomProfileField.objects.get(realm=user_profile.realm, id=field_id)+ field_value = CustomProfileFieldValue.objects.get(+ field=custom_profile_field, user_profile=user_profile+ )+ field_value.delete()+ notify_user_update_custom_profile_data(+ user_profile,+ {+ "id": field_id,+ "value": None,+ "rendered_value": None,+ "type": custom_profile_field.field_type,+ },+ )+ except CustomProfileField.DoesNotExist:+ raise JsonableError(_("Field id {id} not found.").format(id=field_id))+ except CustomProfileFieldValue.DoesNotExist:+ passdiff --git a/zerver/lib/actions.py b/zerver/lib/actions.pyindex 496fa711f06..e13e57d3fc4 100644--- a/zerver/lib/actions.py+++ b/zerver/lib/actions.py@@ -34,6 +34,7 @@ from typing_extensions import TypedDictfrom analytics.lib.counts import COUNT_STATS, do_increment_logging_statfrom confirmation import settings as confirmation_settingsfrom confirmation.models import Confirmation, create_confirmation_link, generate_key+from zerver.actions.custom_profile_fields import do_remove_realm_custom_profile_fieldsfrom zerver.actions.default_streams import (do_remove_default_stream,do_remove_streams_from_default_stream_group,@@ -78,7 +79,6 @@ from zerver.lib.exceptions import (StreamWithIDDoesNotExistError,ZephyrMessageAlreadySentException,)-from zerver.lib.external_accounts import DEFAULT_EXTERNAL_ACCOUNTSfrom zerver.lib.i18n import get_language_namefrom zerver.lib.markdown import MessageRenderingResult, topic_linksfrom zerver.lib.markdown import version as markdown_version@@ -154,7 +154,7 @@ from zerver.lib.topic import (update_edit_history,update_messages_for_topic_edit,)-from zerver.lib.types import EditHistoryEvent, ProfileDataElementValue, ProfileFieldData+from zerver.lib.types import EditHistoryEventfrom zerver.lib.upload import delete_avatar_imagefrom zerver.lib.user_counts import realm_user_count, realm_user_count_by_rolefrom zerver.lib.user_groups import (@@ -179,8 +179,6 @@ from zerver.models import (ArchivedAttachment,Attachment,Client,- CustomProfileField,- CustomProfileFieldValue,DefaultStream,DefaultStreamGroup,Draft,@@ -208,7 +206,6 @@ from zerver.models import (active_non_guest_user_ids,active_user_ids,bot_owner_user_ids,- custom_profile_fields_for_realm,get_bot_dicts_in_realm,get_bot_services,get_client,@@ -6480,172 +6477,6 @@ def do_remove_realm_domain(transaction.on_commit(lambda: send_event(realm, event, active_user_ids(realm.id)))-def notify_realm_custom_profile_fields(realm: Realm) -> None:- fields = custom_profile_fields_for_realm(realm.id)- event = dict(type="custom_profile_fields", fields=[f.as_dict() for f in fields])- send_event(realm, event, active_user_ids(realm.id))---def try_add_realm_default_custom_profile_field(- realm: Realm, field_subtype: str-) -> CustomProfileField:- field_data = DEFAULT_EXTERNAL_ACCOUNTS[field_subtype]- custom_profile_field = CustomProfileField(- realm=realm,- name=field_data["name"],- field_type=CustomProfileField.EXTERNAL_ACCOUNT,- hint=field_data["hint"],- field_data=orjson.dumps(dict(subtype=field_subtype)).decode(),- )- custom_profile_field.save()- custom_profile_field.order = custom_profile_field.id- custom_profile_field.save(update_fields=["order"])- notify_realm_custom_profile_fields(realm)- return custom_profile_field---def try_add_realm_custom_profile_field(- realm: Realm,- name: str,- field_type: int,- hint: str = "",- field_data: Optional[ProfileFieldData] = None,-) -> CustomProfileField:- custom_profile_field = CustomProfileField(realm=realm, name=name, field_type=field_type)- custom_profile_field.hint = hint- if (- custom_profile_field.field_type == CustomProfileField.SELECT- or custom_profile_field.field_type == CustomProfileField.EXTERNAL_ACCOUNT- ):- custom_profile_field.field_data = orjson.dumps(field_data or {}).decode()-- custom_profile_field.save()- custom_profile_field.order = custom_profile_field.id- custom_profile_field.save(update_fields=["order"])- notify_realm_custom_profile_fields(realm)- return custom_profile_field---def do_remove_realm_custom_profile_field(realm: Realm, field: CustomProfileField) -> None:- """- Deleting a field will also delete the user profile data- associated with it in CustomProfileFieldValue model.- """- field.delete()- notify_realm_custom_profile_fields(realm)---def do_remove_realm_custom_profile_fields(realm: Realm) -> None:- CustomProfileField.objects.filter(realm=realm).delete()---def try_update_realm_custom_profile_field(- realm: Realm,- field: CustomProfileField,- name: str,- hint: str = "",- field_data: Optional[ProfileFieldData] = None,-) -> None:- field.name = name- field.hint = hint- if (- field.field_type == CustomProfileField.SELECT- or field.field_type == CustomProfileField.EXTERNAL_ACCOUNT- ):- field.field_data = orjson.dumps(field_data or {}).decode()- field.save()- notify_realm_custom_profile_fields(realm)---def try_reorder_realm_custom_profile_fields(realm: Realm, order: List[int]) -> None:- order_mapping = {_[1]: _[0] for _ in enumerate(order)}- custom_profile_fields = CustomProfileField.objects.filter(realm=realm)- for custom_profile_field in custom_profile_fields:- if custom_profile_field.id not in order_mapping:- raise JsonableError(_("Invalid order mapping."))- for custom_profile_field in custom_profile_fields:- custom_profile_field.order = order_mapping[custom_profile_field.id]- custom_profile_field.save(update_fields=["order"])- notify_realm_custom_profile_fields(realm)---def notify_user_update_custom_profile_data(- user_profile: UserProfile, field: Dict[str, Union[int, str, List[int], None]]-) -> None:- data = dict(id=field["id"], value=field["value"])-- if field["rendered_value"]:- data["rendered_value"] = field["rendered_value"]- payload = dict(user_id=user_profile.id, custom_profile_field=data)- event = dict(type="realm_user", op="update", person=payload)- send_event(user_profile.realm, event, active_user_ids(user_profile.realm.id))---def do_update_user_custom_profile_data_if_changed(- user_profile: UserProfile,- data: List[Dict[str, Union[int, ProfileDataElementValue]]],-) -> None:- with transaction.atomic():- for custom_profile_field in data:- field_value, created = CustomProfileFieldValue.objects.get_or_create(- user_profile=user_profile, field_id=custom_profile_field["id"]- )-- # field_value.value is a TextField() so we need to have field["value"]- # in string form to correctly make comparisons and assignments.- if isinstance(custom_profile_field["value"], str):- custom_profile_field_value_string = custom_profile_field["value"]- else:- custom_profile_field_value_string = orjson.dumps(- custom_profile_field["value"]- ).decode()-- if not created and field_value.value == custom_profile_field_value_string:- # If the field value isn't actually being changed to a different one,- # we have nothing to do here for this field.- continue-- field_value.value = custom_profile_field_value_string- if field_value.field.is_renderable():- field_value.rendered_value = render_stream_description(- custom_profile_field_value_string- )- field_value.save(update_fields=["value", "rendered_value"])- else:- field_value.save(update_fields=["value"])- notify_user_update_custom_profile_data(- user_profile,- {- "id": field_value.field_id,- "value": field_value.value,- "rendered_value": field_value.rendered_value,- "type": field_value.field.field_type,- },- )---def check_remove_custom_profile_field_value(user_profile: UserProfile, field_id: int) -> None:- try:- custom_profile_field = CustomProfileField.objects.get(realm=user_profile.realm, id=field_id)- field_value = CustomProfileFieldValue.objects.get(- field=custom_profile_field, user_profile=user_profile- )- field_value.delete()- notify_user_update_custom_profile_data(- user_profile,- {- "id": field_id,- "value": None,- "rendered_value": None,- "type": custom_profile_field.field_type,- },- )- except CustomProfileField.DoesNotExist:- raise JsonableError(_("Field id {id} not found.").format(id=field_id))- except CustomProfileFieldValue.DoesNotExist:- pass--def do_update_outgoing_webhook_service(bot_profile: UserProfile, service_interface: int, service_payload_url: str) -> None:diff --git a/zerver/tests/test_custom_profile_data.py b/zerver/tests/test_custom_profile_data.pyindex 6d963905bac..205e68733e0 100644--- a/zerver/tests/test_custom_profile_data.py+++ b/zerver/tests/test_custom_profile_data.py@@ -3,7 +3,7 @@ from unittest import mockimport orjson-from zerver.lib.actions import (+from zerver.actions.custom_profile_fields import (do_remove_realm_custom_profile_field,do_update_user_custom_profile_data_if_changed,try_add_realm_custom_profile_field,@@ -709,7 +709,9 @@ class UpdateCustomProfileFieldTest(CustomProfileFieldTestCase):]do_update_user_custom_profile_data_if_changed(iago, data)- with mock.patch("zerver.lib.actions.notify_user_update_custom_profile_data") as mock_notify:+ with mock.patch(+ "zerver.actions.custom_profile_fields.notify_user_update_custom_profile_data"+ ) as mock_notify:# Attempting to "update" the field value, when it wouldn't actually change,# shouldn't trigger notify.do_update_user_custom_profile_data_if_changed(iago, data)diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.pyindex 99dcb5a6f78..d7339f4bb8c 100644--- a/zerver/tests/test_events.py+++ b/zerver/tests/test_events.py@@ -14,6 +14,12 @@ import orjsonfrom django.utils.timezone import now as timezone_nowfrom zerver.actions.alert_words import do_add_alert_words, do_remove_alert_words+from zerver.actions.custom_profile_fields import (+ do_remove_realm_custom_profile_field,+ do_update_user_custom_profile_data_if_changed,+ try_add_realm_custom_profile_field,+ try_update_realm_custom_profile_field,+)from zerver.actions.default_streams import (do_add_default_stream,do_add_streams_to_default_stream_group,@@ -85,7 +91,6 @@ from zerver.lib.actions import (do_reactivate_user,do_regenerate_api_key,do_remove_reaction,- do_remove_realm_custom_profile_field,do_remove_realm_domain,do_rename_stream,do_set_realm_authentication_methods,@@ -99,9 +104,6 @@ from zerver.lib.actions import (do_update_message,do_update_message_flags,do_update_outgoing_webhook_service,- do_update_user_custom_profile_data_if_changed,- try_add_realm_custom_profile_field,- try_update_realm_custom_profile_field,)from zerver.lib.drafts import do_create_drafts, do_delete_draft, do_edit_draftfrom zerver.lib.event_schema import (diff --git a/zerver/tests/test_import_export.py b/zerver/tests/test_import_export.pyindex c90dd66d374..8725e8d23d6 100644--- a/zerver/tests/test_import_export.py+++ b/zerver/tests/test_import_export.py@@ -11,6 +11,10 @@ from django.utils.timezone import now as timezone_nowfrom analytics.models import UserCountfrom zerver.actions.alert_words import do_add_alert_words+from zerver.actions.custom_profile_fields import (+ do_update_user_custom_profile_data_if_changed,+ try_add_realm_custom_profile_field,+)from zerver.actions.presence import do_update_user_presence, do_update_user_statusfrom zerver.actions.realm_emoji import check_add_realm_emojifrom zerver.actions.realm_icon import do_change_icon_source@@ -25,8 +29,6 @@ from zerver.lib.actions import (do_create_user,do_deactivate_user,do_mute_user,- do_update_user_custom_profile_data_if_changed,- try_add_realm_custom_profile_field,)from zerver.lib.avatar_hash import user_avatar_pathfrom zerver.lib.bot_config import set_bot_configdiff --git a/zerver/views/custom_profile_fields.py b/zerver/views/custom_profile_fields.pyindex e378f054b04..f36bd1bf25c 100644--- a/zerver/views/custom_profile_fields.py+++ b/zerver/views/custom_profile_fields.py@@ -6,8 +6,7 @@ from django.db import IntegrityErrorfrom django.http import HttpRequest, HttpResponsefrom django.utils.translation import gettext as _-from zerver.decorator import human_users_only, require_realm_admin-from zerver.lib.actions import (+from zerver.actions.custom_profile_fields import (check_remove_custom_profile_field_value,do_remove_realm_custom_profile_field,do_update_user_custom_profile_data_if_changed,@@ -16,6 +15,7 @@ from zerver.lib.actions import (try_reorder_realm_custom_profile_fields,try_update_realm_custom_profile_field,)+from zerver.decorator import human_users_only, require_realm_adminfrom zerver.lib.exceptions import JsonableErrorfrom zerver.lib.external_accounts import validate_external_account_field_datafrom zerver.lib.request import REQ, has_request_variablesdiff --git a/zerver/views/users.py b/zerver/views/users.pyindex eafd5908d50..abbb4733f50 100644--- a/zerver/views/users.py+++ b/zerver/views/users.py@@ -6,13 +6,16 @@ from django.http import HttpRequest, HttpResponsefrom django.shortcuts import redirectfrom django.utils.translation import gettext as _+from zerver.actions.custom_profile_fields import (+ check_remove_custom_profile_field_value,+ do_update_user_custom_profile_data_if_changed,+)from zerver.context_processors import get_valid_realm_from_requestfrom zerver.decorator import require_member_or_admin, require_realm_adminfrom zerver.forms import PASSWORD_TOO_WEAK_ERROR, CreateUserFormfrom zerver.lib.actions import (check_change_bot_full_name,check_change_full_name,- check_remove_custom_profile_field_value,do_change_avatar_fields,do_change_bot_owner,do_change_default_all_public_streams,@@ -25,7 +28,6 @@ from zerver.lib.actions import (do_regenerate_api_key,do_update_bot_config_data,do_update_outgoing_webhook_service,- do_update_user_custom_profile_data_if_changed,notify_created_bot,)from zerver.lib.avatar import avatar_url, get_gravatar_urldiff --git a/zilencer/management/commands/populate_db.py b/zilencer/management/commands/populate_db.pyindex 088688aece6..c6c53abecd4 100644--- a/zilencer/management/commands/populate_db.py+++ b/zilencer/management/commands/populate_db.py@@ -18,15 +18,17 @@ from django.utils.timezone import now as timezone_nowfrom django.utils.timezone import timedelta as timezone_timedeltafrom scripts.lib.zulip_tools import get_or_create_dev_uuid_var_path+from zerver.actions.custom_profile_fields import (+ do_update_user_custom_profile_data_if_changed,+ try_add_realm_custom_profile_field,+ try_add_realm_default_custom_profile_field,+)from zerver.actions.realm_emoji import check_add_realm_emojifrom zerver.lib.actions import (build_message_send_dict,do_change_user_role,do_create_realm,do_send_messages,- do_update_user_custom_profile_data_if_changed,- try_add_realm_custom_profile_field,- try_add_realm_default_custom_profile_field,)from zerver.lib.bulk_create import bulk_create_streamsfrom zerver.lib.cache import cache_setdiff --git a/zproject/backends.py b/zproject/backends.pyindex 757e3e3ebbf..f5e6b350735 100644--- a/zproject/backends.py+++ b/zproject/backends.py@@ -58,13 +58,13 @@ from social_core.pipeline.partial import partialfrom typing_extensions import TypedDictfrom zxcvbn import zxcvbn+from zerver.actions.custom_profile_fields import do_update_user_custom_profile_data_if_changedfrom zerver.decorator import client_is_exempt_from_rate_limitingfrom zerver.lib.actions import (do_create_user,do_deactivate_user,do_reactivate_user,do_regenerate_api_key,- do_update_user_custom_profile_data_if_changed,)from zerver.lib.avatar import avatar_url, is_avatar_newfrom zerver.lib.avatar_hash import user_avatar_content_hash