custom profile fields: Extract check_remove_user_custom_profile_value.

This moves the logic for deleting the user's custom profile field
value in the remove_user_custom_profile_data view function to a method
named check_remove_user_custom_profile_value in actions.py, so that we
can reuse it in the next commit.
This commit is contained in:
Hemanth V. Alluri
2019-01-15 16:22:14 +05:30
committed by Tim Abbott
parent 03d66abd9e
commit 716bcad393
2 changed files with 18 additions and 15 deletions

View File

@@ -5133,6 +5133,22 @@ def do_update_user_custom_profile_data(user_profile: UserProfile,
"rendered_value": field_value.rendered_value, "rendered_value": field_value.rendered_value,
"type": field_value.field.field_type}) "type": field_value.field.field_type})
def check_remove_custom_profile_field_value(user_profile: UserProfile,
field_id: Union[int, str, List[int]]
) -> None:
try:
field = CustomProfileField.objects.get(realm=user_profile.realm, id=field_id)
field_value = CustomProfileFieldValue.objects.get(field=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': field.field_type})
except CustomProfileField.DoesNotExist:
raise JsonableError(_('Field id {id} not found.').format(id=field_id))
except CustomProfileFieldValue.DoesNotExist:
pass
def do_send_create_user_group_event(user_group: UserGroup, members: List[UserProfile]) -> None: def do_send_create_user_group_event(user_group: UserGroup, members: List[UserProfile]) -> None:
event = dict(type="user_group", event = dict(type="user_group",
op="add", op="add",

View File

@@ -15,6 +15,7 @@ from zerver.lib.actions import (try_add_realm_custom_profile_field,
try_update_realm_custom_profile_field, try_update_realm_custom_profile_field,
do_update_user_custom_profile_data, do_update_user_custom_profile_data,
try_reorder_realm_custom_profile_fields, try_reorder_realm_custom_profile_fields,
check_remove_custom_profile_field_value,
notify_user_update_custom_profile_data) notify_user_update_custom_profile_data)
from zerver.lib.response import json_success, json_error from zerver.lib.response import json_success, json_error
from zerver.lib.types import ProfileFieldData from zerver.lib.types import ProfileFieldData
@@ -132,21 +133,7 @@ def remove_user_custom_profile_data(request: HttpRequest, user_profile: UserProf
data: List[int]=REQ(validator=check_list( data: List[int]=REQ(validator=check_list(
check_int))) -> HttpResponse: check_int))) -> HttpResponse:
for field_id in data: for field_id in data:
try: check_remove_custom_profile_field_value(user_profile, field_id)
field = CustomProfileField.objects.get(realm=user_profile.realm, id=field_id)
except CustomProfileField.DoesNotExist:
return json_error(_('Field id {id} not found.').format(id=field_id))
try:
field_value = CustomProfileFieldValue.objects.get(field=field, user_profile=user_profile)
except CustomProfileFieldValue.DoesNotExist:
continue
field_value.delete()
notify_user_update_custom_profile_data(user_profile, {'id': field_id,
'value': None,
'rendered_value': None,
'type': field.field_type})
return json_success() return json_success()
@human_users_only @human_users_only