2024-07-12 02:30:25 +02:00
|
|
|
from collections.abc import Iterable
|
2022-04-14 23:46:56 +02:00
|
|
|
|
|
|
|
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
|
2022-07-08 17:17:46 +02:00
|
|
|
from zerver.lib.types import ProfileDataElementUpdateDict, ProfileFieldData
|
2023-10-11 09:34:26 +02:00
|
|
|
from zerver.lib.users import get_user_ids_who_can_access_user
|
2023-12-15 20:57:08 +01:00
|
|
|
from zerver.models import CustomProfileField, CustomProfileFieldValue, Realm, UserProfile
|
|
|
|
from zerver.models.custom_profile_fields import custom_profile_fields_for_realm
|
2023-12-15 01:16:00 +01:00
|
|
|
from zerver.models.users import active_user_ids
|
2022-04-14 23:46:56 +02:00
|
|
|
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(
|
2022-07-12 21:04:47 +02:00
|
|
|
realm: Realm,
|
|
|
|
field_subtype: str,
|
|
|
|
display_in_profile_summary: bool = False,
|
2024-03-19 14:22:03 +01:00
|
|
|
required: bool = False,
|
2022-04-14 23:46:56 +02:00
|
|
|
) -> CustomProfileField:
|
|
|
|
field_data = DEFAULT_EXTERNAL_ACCOUNTS[field_subtype]
|
|
|
|
custom_profile_field = CustomProfileField(
|
|
|
|
realm=realm,
|
2022-09-20 22:45:56 +02:00
|
|
|
name=str(field_data.name),
|
2022-04-14 23:46:56 +02:00
|
|
|
field_type=CustomProfileField.EXTERNAL_ACCOUNT,
|
2022-09-20 22:45:56 +02:00
|
|
|
hint=field_data.hint,
|
2022-04-14 23:46:56 +02:00
|
|
|
field_data=orjson.dumps(dict(subtype=field_subtype)).decode(),
|
2022-07-12 21:04:47 +02:00
|
|
|
display_in_profile_summary=display_in_profile_summary,
|
2024-03-19 14:22:03 +01:00
|
|
|
required=required,
|
2022-04-14 23:46:56 +02:00
|
|
|
)
|
|
|
|
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 = "",
|
2024-07-12 02:30:23 +02:00
|
|
|
field_data: ProfileFieldData | None = None,
|
2022-07-12 21:04:47 +02:00
|
|
|
display_in_profile_summary: bool = False,
|
2024-03-19 14:22:03 +01:00
|
|
|
required: bool = False,
|
2022-04-14 23:46:56 +02:00
|
|
|
) -> CustomProfileField:
|
2022-07-12 21:04:47 +02:00
|
|
|
custom_profile_field = CustomProfileField(
|
|
|
|
realm=realm,
|
|
|
|
name=name,
|
|
|
|
field_type=field_type,
|
|
|
|
display_in_profile_summary=display_in_profile_summary,
|
2024-03-19 14:22:03 +01:00
|
|
|
required=required,
|
2022-07-12 21:04:47 +02:00
|
|
|
)
|
2022-04-14 23:46:56 +02:00
|
|
|
custom_profile_field.hint = hint
|
2023-07-22 01:15:10 +02:00
|
|
|
if custom_profile_field.field_type in (
|
|
|
|
CustomProfileField.SELECT,
|
|
|
|
CustomProfileField.EXTERNAL_ACCOUNT,
|
2022-04-14 23:46:56 +02:00
|
|
|
):
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
2022-06-14 16:33:14 +02:00
|
|
|
def remove_custom_profile_field_value_if_required(
|
|
|
|
field: CustomProfileField, field_data: ProfileFieldData
|
|
|
|
) -> None:
|
|
|
|
old_values = set(orjson.loads(field.field_data).keys())
|
|
|
|
new_values = set(field_data.keys())
|
|
|
|
removed_values = old_values - new_values
|
|
|
|
|
|
|
|
if removed_values:
|
|
|
|
CustomProfileFieldValue.objects.filter(field=field, value__in=removed_values).delete()
|
|
|
|
|
|
|
|
|
2022-04-14 23:46:56 +02:00
|
|
|
def try_update_realm_custom_profile_field(
|
|
|
|
realm: Realm,
|
|
|
|
field: CustomProfileField,
|
2024-07-12 02:30:23 +02:00
|
|
|
name: str | None = None,
|
|
|
|
hint: str | None = None,
|
|
|
|
field_data: ProfileFieldData | None = None,
|
|
|
|
display_in_profile_summary: bool | None = None,
|
|
|
|
required: bool | None = None,
|
2022-04-14 23:46:56 +02:00
|
|
|
) -> None:
|
2024-03-30 09:56:26 +01:00
|
|
|
if name is not None:
|
|
|
|
field.name = name
|
|
|
|
if hint is not None:
|
|
|
|
field.hint = hint
|
2024-03-30 13:12:18 +01:00
|
|
|
if required is not None:
|
|
|
|
field.required = required
|
2024-03-30 12:40:57 +01:00
|
|
|
if display_in_profile_summary is not None:
|
|
|
|
field.display_in_profile_summary = display_in_profile_summary
|
2024-01-20 07:18:26 +01:00
|
|
|
|
|
|
|
if field.field_type in (
|
|
|
|
CustomProfileField.SELECT,
|
|
|
|
CustomProfileField.EXTERNAL_ACCOUNT,
|
|
|
|
):
|
|
|
|
# If field_data is None, field_data is unchanged and there is no need for
|
|
|
|
# comparing field_data values.
|
|
|
|
if field_data is not None and field.field_type == CustomProfileField.SELECT:
|
2022-06-14 16:33:14 +02:00
|
|
|
remove_custom_profile_field_value_if_required(field, field_data)
|
2024-01-20 07:18:26 +01:00
|
|
|
|
|
|
|
# If field.field_data is the default empty string, we will set field_data
|
|
|
|
# to an empty dict.
|
|
|
|
if field_data is not None or field.field_data == "":
|
|
|
|
field.field_data = orjson.dumps(field_data or {}).decode()
|
2022-04-14 23:46:56 +02:00
|
|
|
field.save()
|
|
|
|
notify_realm_custom_profile_fields(realm)
|
|
|
|
|
|
|
|
|
2022-06-23 20:07:19 +02:00
|
|
|
def try_reorder_realm_custom_profile_fields(realm: Realm, order: Iterable[int]) -> None:
|
2022-04-14 23:46:56 +02:00
|
|
|
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(
|
2024-07-12 02:30:23 +02:00
|
|
|
user_profile: UserProfile, field: dict[str, int | str | list[int] | None]
|
2022-04-14 23:46:56 +02:00
|
|
|
) -> 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)
|
2023-10-11 09:34:26 +02:00
|
|
|
send_event(user_profile.realm, event, get_user_ids_who_can_access_user(user_profile))
|
2022-04-14 23:46:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
def do_update_user_custom_profile_data_if_changed(
|
|
|
|
user_profile: UserProfile,
|
2024-07-12 02:30:17 +02:00
|
|
|
data: list[ProfileDataElementUpdateDict],
|
2022-04-14 23:46:56 +02:00
|
|
|
) -> 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(
|
2022-10-29 20:52:47 +02:00
|
|
|
custom_profile_field_value_string, user_profile.realm
|
2022-04-14 23:46:56 +02:00
|
|
|
)
|
|
|
|
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
|