2024-07-12 02:30:17 +02:00
|
|
|
from typing import Annotated, Optional
|
2017-03-17 10:07:22 +01:00
|
|
|
|
2020-08-07 01:09:47 +02:00
|
|
|
import orjson
|
2020-06-21 02:36:20 +02:00
|
|
|
from django.core.exceptions import ValidationError
|
2019-02-02 23:53:22 +01:00
|
|
|
from django.db import IntegrityError
|
2017-03-17 10:07:22 +01:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2021-04-16 00:57:30 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2024-06-19 18:02:21 +02:00
|
|
|
from pydantic import Json, StringConstraints
|
2017-03-17 10:07:22 +01:00
|
|
|
|
2022-04-14 23:46:56 +02:00
|
|
|
from zerver.actions.custom_profile_fields import (
|
2020-06-11 00:54:34 +02:00
|
|
|
check_remove_custom_profile_field_value,
|
|
|
|
do_remove_realm_custom_profile_field,
|
|
|
|
do_update_user_custom_profile_data_if_changed,
|
|
|
|
try_add_realm_custom_profile_field,
|
|
|
|
try_add_realm_default_custom_profile_field,
|
|
|
|
try_reorder_realm_custom_profile_fields,
|
|
|
|
try_update_realm_custom_profile_field,
|
|
|
|
)
|
2022-04-14 23:46:56 +02:00
|
|
|
from zerver.decorator import human_users_only, require_realm_admin
|
2018-08-16 20:12:49 +02:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
2019-05-27 10:59:55 +02:00
|
|
|
from zerver.lib.external_accounts import validate_external_account_field_data
|
2021-06-30 18:35:50 +02:00
|
|
|
from zerver.lib.response import json_success
|
2024-06-19 18:02:21 +02:00
|
|
|
from zerver.lib.typed_endpoint import PathOnly, typed_endpoint
|
|
|
|
from zerver.lib.types import ProfileDataElementUpdateDict, ProfileFieldData
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.users import validate_user_custom_profile_data
|
2024-06-19 18:02:21 +02:00
|
|
|
from zerver.lib.validator import check_capped_string, validate_select_field_data
|
2023-12-15 20:57:08 +01:00
|
|
|
from zerver.models import CustomProfileField, Realm, UserProfile
|
|
|
|
from zerver.models.custom_profile_fields import custom_profile_fields_for_realm
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2017-03-17 10:07:22 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
def list_realm_custom_profile_fields(
|
|
|
|
request: HttpRequest, user_profile: UserProfile
|
|
|
|
) -> HttpResponse:
|
2017-03-17 10:07:22 +01:00
|
|
|
fields = custom_profile_fields_for_realm(user_profile.realm_id)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request, data={"custom_fields": [f.as_dict() for f in fields]})
|
2017-03-17 10:07:22 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-03-31 07:30:24 +02:00
|
|
|
hint_validator = check_capped_string(CustomProfileField.HINT_MAX_LENGTH)
|
2018-08-16 20:12:49 +02:00
|
|
|
name_validator = check_capped_string(CustomProfileField.NAME_MAX_LENGTH)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-08-16 20:12:49 +02:00
|
|
|
def validate_field_name_and_hint(name: str, hint: str) -> None:
|
|
|
|
if not name.strip():
|
2019-08-03 02:30:15 +02:00
|
|
|
raise JsonableError(_("Label cannot be blank."))
|
2018-08-16 20:12:49 +02:00
|
|
|
|
2020-06-21 02:36:20 +02:00
|
|
|
try:
|
2021-02-12 08:20:45 +01:00
|
|
|
hint_validator("hint", hint)
|
|
|
|
name_validator("name", name)
|
2020-06-21 02:36:20 +02:00
|
|
|
except ValidationError as error:
|
|
|
|
raise JsonableError(error.message)
|
2018-03-31 07:30:24 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
def validate_custom_field_data(field_type: int, field_data: ProfileFieldData) -> None:
|
2020-06-21 02:36:20 +02:00
|
|
|
try:
|
2021-03-20 11:39:22 +01:00
|
|
|
if field_type == CustomProfileField.SELECT:
|
2020-06-21 02:36:20 +02:00
|
|
|
# Choice type field must have at least have one choice
|
|
|
|
if len(field_data) < 1:
|
|
|
|
raise JsonableError(_("Field must have at least one choice."))
|
2021-03-24 12:48:00 +01:00
|
|
|
validate_select_field_data(field_data)
|
2020-06-21 02:36:20 +02:00
|
|
|
elif field_type == CustomProfileField.EXTERNAL_ACCOUNT:
|
|
|
|
validate_external_account_field_data(field_data)
|
|
|
|
except ValidationError as error:
|
|
|
|
raise JsonableError(error.message)
|
2019-06-07 08:00:37 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-07-12 21:04:47 +02:00
|
|
|
def validate_display_in_profile_summary_field(
|
|
|
|
field_type: int, display_in_profile_summary: bool
|
|
|
|
) -> None:
|
|
|
|
if not display_in_profile_summary:
|
|
|
|
return
|
|
|
|
|
|
|
|
# The LONG_TEXT field type doesn't make sense visually for profile
|
|
|
|
# field summaries. The USER field type will require some further
|
|
|
|
# client support.
|
2023-07-22 01:15:10 +02:00
|
|
|
if field_type in (CustomProfileField.LONG_TEXT, CustomProfileField.USER):
|
2022-07-12 21:04:47 +02:00
|
|
|
raise JsonableError(_("Field type not supported for display in profile summary."))
|
|
|
|
|
|
|
|
|
2019-08-24 13:52:25 +02:00
|
|
|
def is_default_external_field(field_type: int, field_data: ProfileFieldData) -> bool:
|
|
|
|
if field_type != CustomProfileField.EXTERNAL_ACCOUNT:
|
|
|
|
return False
|
2021-02-12 08:20:45 +01:00
|
|
|
if field_data["subtype"] == "custom":
|
2019-08-24 13:52:25 +02:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
def validate_custom_profile_field(
|
2022-07-12 21:04:47 +02:00
|
|
|
name: str,
|
|
|
|
hint: str,
|
|
|
|
field_type: int,
|
|
|
|
field_data: ProfileFieldData,
|
|
|
|
display_in_profile_summary: bool,
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> None:
|
2019-08-24 13:13:48 +02:00
|
|
|
# Validate field data
|
|
|
|
validate_custom_field_data(field_type, field_data)
|
|
|
|
|
2019-08-24 13:52:25 +02:00
|
|
|
if not is_default_external_field(field_type, field_data):
|
|
|
|
# If field is default external field then we will fetch all data
|
|
|
|
# from our default field dictionary, so no need to validate name or hint
|
|
|
|
# Validate field name, hint if not default external account field
|
|
|
|
validate_field_name_and_hint(name, hint)
|
|
|
|
|
2019-08-24 13:13:48 +02:00
|
|
|
field_types = [i[0] for i in CustomProfileField.FIELD_TYPE_CHOICES]
|
|
|
|
if field_type not in field_types:
|
|
|
|
raise JsonableError(_("Invalid field type."))
|
|
|
|
|
2022-07-12 21:04:47 +02:00
|
|
|
validate_display_in_profile_summary_field(field_type, display_in_profile_summary)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2024-03-30 09:56:26 +01:00
|
|
|
def validate_custom_profile_field_update(
|
|
|
|
field: CustomProfileField,
|
2024-03-30 12:40:57 +01:00
|
|
|
display_in_profile_summary: Optional[bool] = None,
|
2024-01-20 07:18:26 +01:00
|
|
|
field_data: Optional[ProfileFieldData] = None,
|
2024-03-30 09:56:26 +01:00
|
|
|
name: Optional[str] = None,
|
|
|
|
hint: Optional[str] = None,
|
|
|
|
) -> None:
|
|
|
|
if name is None:
|
|
|
|
name = field.name
|
|
|
|
if hint is None:
|
|
|
|
hint = field.hint
|
2024-01-20 07:18:26 +01:00
|
|
|
if field_data is None:
|
|
|
|
if field.field_data == "":
|
|
|
|
# We're passing this just for validation, sinec the function won't
|
|
|
|
# accept a string. This won't change the actual value.
|
|
|
|
field_data = {}
|
|
|
|
else:
|
|
|
|
field_data = orjson.loads(field.field_data)
|
2024-03-30 12:40:57 +01:00
|
|
|
if display_in_profile_summary is None:
|
|
|
|
display_in_profile_summary = field.display_in_profile_summary
|
2024-01-20 07:18:26 +01:00
|
|
|
|
|
|
|
assert field_data is not None
|
2024-03-30 09:56:26 +01:00
|
|
|
validate_custom_profile_field(
|
2024-01-20 07:18:26 +01:00
|
|
|
name,
|
|
|
|
hint,
|
|
|
|
field.field_type,
|
|
|
|
field_data,
|
|
|
|
display_in_profile_summary,
|
2024-03-30 09:56:26 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-07-12 21:04:47 +02:00
|
|
|
def update_only_display_in_profile_summary(
|
|
|
|
existing_field: CustomProfileField,
|
2024-01-20 07:18:26 +01:00
|
|
|
requested_field_data: Optional[ProfileFieldData] = None,
|
2024-03-30 09:56:26 +01:00
|
|
|
requested_name: Optional[str] = None,
|
|
|
|
requested_hint: Optional[str] = None,
|
2022-07-12 21:04:47 +02:00
|
|
|
) -> bool:
|
|
|
|
if (
|
2024-03-30 09:56:26 +01:00
|
|
|
(requested_name is not None and requested_name != existing_field.name)
|
|
|
|
or (requested_hint is not None and requested_hint != existing_field.hint)
|
2024-01-20 07:18:26 +01:00
|
|
|
or (
|
|
|
|
requested_field_data is not None
|
|
|
|
and requested_field_data != orjson.loads(existing_field.field_data)
|
|
|
|
)
|
2022-07-12 21:04:47 +02:00
|
|
|
):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2022-10-30 11:19:51 +01:00
|
|
|
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)
|
2022-07-12 21:04:47 +02:00
|
|
|
if profile_field_id is not None:
|
|
|
|
query = query.exclude(id=profile_field_id)
|
|
|
|
return query.count() >= CustomProfileField.MAX_DISPLAY_IN_PROFILE_SUMMARY_FIELDS
|
|
|
|
|
|
|
|
|
2017-03-17 10:07:22 +01:00
|
|
|
@require_realm_admin
|
2024-06-19 18:02:21 +02:00
|
|
|
@typed_endpoint
|
2021-02-12 08:19:30 +01:00
|
|
|
def create_realm_custom_profile_field(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2024-06-19 18:02:21 +02:00
|
|
|
*,
|
|
|
|
name: Annotated[str, StringConstraints(strip_whitespace=True)] = "",
|
|
|
|
hint: str = "",
|
|
|
|
field_data: Optional[Json[ProfileFieldData]] = None,
|
|
|
|
field_type: Json[int],
|
|
|
|
display_in_profile_summary: Json[bool] = False,
|
|
|
|
required: Json[bool] = False,
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2024-06-19 18:02:21 +02:00
|
|
|
if field_data is None:
|
|
|
|
field_data = {}
|
2022-10-30 11:19:51 +01:00
|
|
|
if display_in_profile_summary and display_in_profile_summary_limit_reached(user_profile.realm):
|
2022-07-12 21:04:47 +02:00
|
|
|
raise JsonableError(
|
|
|
|
_("Only 2 custom profile fields can be displayed in the profile summary.")
|
|
|
|
)
|
|
|
|
|
|
|
|
validate_custom_profile_field(name, hint, field_type, field_data, display_in_profile_summary)
|
2017-03-17 10:07:22 +01:00
|
|
|
try:
|
2019-08-24 13:52:25 +02:00
|
|
|
if is_default_external_field(field_type, field_data):
|
2021-02-12 08:20:45 +01:00
|
|
|
field_subtype = field_data["subtype"]
|
2020-06-23 06:50:30 +02:00
|
|
|
assert isinstance(field_subtype, str)
|
2019-08-24 13:52:25 +02:00
|
|
|
field = try_add_realm_default_custom_profile_field(
|
|
|
|
realm=user_profile.realm,
|
|
|
|
field_subtype=field_subtype,
|
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,
|
2019-08-24 13:52:25 +02:00
|
|
|
)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request, data={"id": field.id})
|
2019-08-24 13:52:25 +02:00
|
|
|
else:
|
|
|
|
field = try_add_realm_custom_profile_field(
|
|
|
|
realm=user_profile.realm,
|
|
|
|
name=name,
|
|
|
|
field_data=field_data,
|
|
|
|
field_type=field_type,
|
|
|
|
hint=hint,
|
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,
|
2019-08-24 13:52:25 +02:00
|
|
|
)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request, data={"id": field.id})
|
2017-03-17 10:07:22 +01:00
|
|
|
except IntegrityError:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("A field with that label already exists."))
|
2017-03-17 10:07:22 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-03-17 10:07:22 +01:00
|
|
|
@require_realm_admin
|
2021-02-12 08:19:30 +01:00
|
|
|
def delete_realm_custom_profile_field(
|
|
|
|
request: HttpRequest, user_profile: UserProfile, field_id: int
|
|
|
|
) -> HttpResponse:
|
2017-03-17 10:07:22 +01:00
|
|
|
try:
|
|
|
|
field = CustomProfileField.objects.get(id=field_id)
|
|
|
|
except CustomProfileField.DoesNotExist:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("Field id {id} not found.").format(id=field_id))
|
2017-03-17 10:07:22 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
do_remove_realm_custom_profile_field(realm=user_profile.realm, field=field)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2017-03-17 10:07:22 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-03-17 10:07:22 +01:00
|
|
|
@require_realm_admin
|
2024-06-19 18:02:21 +02:00
|
|
|
@typed_endpoint
|
2021-02-12 08:19:30 +01:00
|
|
|
def update_realm_custom_profile_field(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2024-06-19 18:02:21 +02:00
|
|
|
*,
|
|
|
|
field_id: PathOnly[int],
|
|
|
|
name: Optional[Annotated[str, StringConstraints(strip_whitespace=True)]] = None,
|
|
|
|
hint: Optional[str] = None,
|
|
|
|
field_data: Optional[Json[ProfileFieldData]] = None,
|
|
|
|
required: Optional[Json[bool]] = None,
|
|
|
|
display_in_profile_summary: Optional[Json[bool]] = None,
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2017-03-17 10:07:22 +01:00
|
|
|
realm = user_profile.realm
|
|
|
|
try:
|
|
|
|
field = CustomProfileField.objects.get(realm=realm, id=field_id)
|
|
|
|
except CustomProfileField.DoesNotExist:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("Field id {id} not found.").format(id=field_id))
|
2017-03-17 10:07:22 +01:00
|
|
|
|
2022-10-30 11:19:51 +01:00
|
|
|
if display_in_profile_summary and display_in_profile_summary_limit_reached(
|
|
|
|
user_profile.realm, field.id
|
|
|
|
):
|
2022-07-12 21:04:47 +02:00
|
|
|
raise JsonableError(
|
|
|
|
_("Only 2 custom profile fields can be displayed in the profile summary.")
|
|
|
|
)
|
|
|
|
|
2023-01-18 02:59:37 +01:00
|
|
|
if (
|
|
|
|
field.field_type == CustomProfileField.EXTERNAL_ACCOUNT
|
2022-07-12 21:04:47 +02:00
|
|
|
# HACK: Allow changing the display_in_profile_summary property
|
|
|
|
# of default external account types, but not any others.
|
|
|
|
#
|
|
|
|
# TODO: Make the name/hint/field_data parameters optional, and
|
2024-01-20 07:18:26 +01:00
|
|
|
# explicitly require that the client passes None for all of them for this case.
|
|
|
|
# Right now, for name/hint/field_data we allow the client to send the existing
|
|
|
|
# values for the respective fields. After this TODO is done, we will only allow
|
|
|
|
# the client to pass None values if the field is unchanged.
|
2023-01-18 02:59:37 +01:00
|
|
|
and is_default_external_field(field.field_type, orjson.loads(field.field_data))
|
2024-01-20 07:18:26 +01:00
|
|
|
and not update_only_display_in_profile_summary(field, field_data, name, hint)
|
2023-01-18 02:59:37 +01:00
|
|
|
):
|
|
|
|
raise JsonableError(_("Default custom field cannot be updated."))
|
2019-08-24 13:52:25 +02:00
|
|
|
|
2024-01-20 07:18:26 +01:00
|
|
|
validate_custom_profile_field_update(field, display_in_profile_summary, field_data, name, hint)
|
2017-03-17 10:07:22 +01:00
|
|
|
try:
|
2022-07-12 21:04:47 +02:00
|
|
|
try_update_realm_custom_profile_field(
|
2024-03-30 09:56:26 +01:00
|
|
|
realm=realm,
|
|
|
|
field=field,
|
|
|
|
name=name,
|
2022-07-12 21:04:47 +02:00
|
|
|
hint=hint,
|
|
|
|
field_data=field_data,
|
|
|
|
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
|
|
|
)
|
2017-03-17 10:07:22 +01:00
|
|
|
except IntegrityError:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("A field with that label already exists."))
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2017-03-17 10:07:22 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-04-08 18:13:37 +02:00
|
|
|
@require_realm_admin
|
2024-06-19 18:02:21 +02:00
|
|
|
@typed_endpoint
|
2021-02-12 08:19:30 +01:00
|
|
|
def reorder_realm_custom_profile_fields(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2024-06-19 18:02:21 +02:00
|
|
|
*,
|
2024-07-12 02:30:17 +02:00
|
|
|
order: Json[list[int]],
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2018-04-08 18:13:37 +02:00
|
|
|
try_reorder_realm_custom_profile_fields(user_profile.realm, order)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2018-04-08 18:13:37 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-06-05 12:57:02 +02:00
|
|
|
@human_users_only
|
2024-06-19 18:02:21 +02:00
|
|
|
@typed_endpoint
|
2021-02-12 08:19:30 +01:00
|
|
|
def remove_user_custom_profile_data(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2024-06-19 18:02:21 +02:00
|
|
|
*,
|
2024-07-12 02:30:17 +02:00
|
|
|
data: Json[list[int]],
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2018-06-05 12:57:02 +02:00
|
|
|
for field_id in data:
|
2019-01-15 11:52:14 +01:00
|
|
|
check_remove_custom_profile_field_value(user_profile, field_id)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2018-06-05 12:57:02 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-07-31 20:30:08 +02:00
|
|
|
@human_users_only
|
2024-06-19 18:02:21 +02:00
|
|
|
@typed_endpoint
|
2017-03-17 10:07:22 +01:00
|
|
|
def update_user_custom_profile_data(
|
2020-06-21 03:22:21 +02:00
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2024-06-19 18:02:21 +02:00
|
|
|
*,
|
2024-07-12 02:30:17 +02:00
|
|
|
data: Json[list[ProfileDataElementUpdateDict]],
|
2020-06-21 03:22:21 +02:00
|
|
|
) -> HttpResponse:
|
2018-09-04 20:23:44 +02:00
|
|
|
validate_user_custom_profile_data(user_profile.realm.id, data)
|
2019-10-01 04:22:50 +02:00
|
|
|
do_update_user_custom_profile_data_if_changed(user_profile, data)
|
2017-03-17 10:07:22 +01:00
|
|
|
# We need to call this explicitly otherwise constraints are not check
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|