2017-03-17 10:07:22 +01:00
|
|
|
|
2018-04-24 03:47:28 +02:00
|
|
|
from typing import Union, List, Dict, Optional
|
2017-03-17 10:07:22 +01:00
|
|
|
import logging
|
2018-04-08 09:50:05 +02:00
|
|
|
import ujson
|
2017-03-17 10:07:22 +01:00
|
|
|
|
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.db import IntegrityError, connection
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
2017-10-28 00:07:31 +02:00
|
|
|
from zerver.decorator import require_realm_admin, human_users_only
|
|
|
|
from zerver.lib.request import has_request_variables, REQ
|
2017-03-17 10:07:22 +01:00
|
|
|
from zerver.lib.actions import (try_add_realm_custom_profile_field,
|
|
|
|
do_remove_realm_custom_profile_field,
|
|
|
|
try_update_realm_custom_profile_field,
|
|
|
|
do_update_user_custom_profile_data)
|
|
|
|
from zerver.lib.response import json_success, json_error
|
2018-04-08 09:50:05 +02:00
|
|
|
from zerver.lib.types import ProfileFieldData
|
|
|
|
from zerver.lib.validator import (check_dict, check_list, check_int,
|
|
|
|
validate_field_data, check_capped_string)
|
2017-03-17 10:07:22 +01:00
|
|
|
|
|
|
|
from zerver.models import (custom_profile_fields_for_realm, UserProfile,
|
|
|
|
CustomProfileField, custom_profile_fields_for_realm)
|
|
|
|
|
2017-10-27 02:18:49 +02: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)
|
|
|
|
return json_success({'custom_fields': [f.as_dict() for f in fields]})
|
|
|
|
|
2018-03-31 07:30:24 +02:00
|
|
|
hint_validator = check_capped_string(CustomProfileField.HINT_MAX_LENGTH)
|
|
|
|
|
2017-03-17 10:07:22 +01:00
|
|
|
@require_realm_admin
|
|
|
|
@has_request_variables
|
2017-12-24 16:06:24 +01:00
|
|
|
def create_realm_custom_profile_field(request: HttpRequest,
|
2018-04-24 03:47:28 +02:00
|
|
|
user_profile: UserProfile, name: str=REQ(),
|
|
|
|
hint: str=REQ(default=''),
|
2018-04-08 09:50:05 +02:00
|
|
|
field_data: ProfileFieldData=REQ(default={},
|
|
|
|
converter=ujson.loads),
|
2017-12-24 16:06:24 +01:00
|
|
|
field_type: int=REQ(validator=check_int)) -> HttpResponse:
|
2017-03-17 10:07:22 +01:00
|
|
|
if not name.strip():
|
|
|
|
return json_error(_("Name cannot be blank."))
|
|
|
|
|
2018-03-31 07:30:24 +02:00
|
|
|
error = hint_validator('hint', hint)
|
|
|
|
if error:
|
|
|
|
return json_error(error)
|
|
|
|
|
2018-04-10 06:36:36 +02:00
|
|
|
field_types = [i[0] for i in CustomProfileField.FIELD_TYPE_CHOICES]
|
|
|
|
if field_type not in field_types:
|
2017-03-17 10:07:22 +01:00
|
|
|
return json_error(_("Invalid field type."))
|
|
|
|
|
2018-04-08 09:50:05 +02:00
|
|
|
error = validate_field_data(field_data)
|
|
|
|
if error:
|
|
|
|
return json_error(error)
|
|
|
|
|
2017-03-17 10:07:22 +01:00
|
|
|
try:
|
|
|
|
field = try_add_realm_custom_profile_field(
|
|
|
|
realm=user_profile.realm,
|
|
|
|
name=name,
|
2018-04-08 09:50:05 +02:00
|
|
|
field_data=field_data,
|
2017-03-17 10:07:22 +01:00
|
|
|
field_type=field_type,
|
2018-03-31 07:30:24 +02:00
|
|
|
hint=hint,
|
2017-03-17 10:07:22 +01:00
|
|
|
)
|
|
|
|
return json_success({'id': field.id})
|
|
|
|
except IntegrityError:
|
|
|
|
return json_error(_("A field with that name already exists."))
|
|
|
|
|
|
|
|
@require_realm_admin
|
2017-10-27 02:18:49 +02: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:
|
|
|
|
return json_error(_('Field id {id} not found.').format(id=field_id))
|
|
|
|
|
|
|
|
do_remove_realm_custom_profile_field(realm=user_profile.realm,
|
|
|
|
field=field)
|
|
|
|
return json_success()
|
|
|
|
|
|
|
|
@require_realm_admin
|
|
|
|
@has_request_variables
|
2017-10-27 02:18:49 +02:00
|
|
|
def update_realm_custom_profile_field(request: HttpRequest, user_profile: UserProfile,
|
2018-04-24 03:47:28 +02:00
|
|
|
field_id: int, name: str=REQ(),
|
2018-04-08 09:50:05 +02:00
|
|
|
hint: str=REQ(default=''),
|
|
|
|
field_data: ProfileFieldData=REQ(default={},
|
|
|
|
converter=ujson.loads),
|
2018-03-31 07:30:24 +02:00
|
|
|
) -> HttpResponse:
|
2017-03-17 10:07:22 +01:00
|
|
|
if not name.strip():
|
|
|
|
return json_error(_("Name cannot be blank."))
|
|
|
|
|
2018-03-31 07:30:24 +02:00
|
|
|
error = hint_validator('hint', hint)
|
|
|
|
if error:
|
|
|
|
return json_error(error, data={'field': 'hint'})
|
|
|
|
|
2018-04-08 09:50:05 +02:00
|
|
|
error = validate_field_data(field_data)
|
|
|
|
if error:
|
|
|
|
return json_error(error)
|
|
|
|
|
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:
|
|
|
|
return json_error(_('Field id {id} not found.').format(id=field_id))
|
|
|
|
|
|
|
|
try:
|
2018-04-08 09:50:05 +02:00
|
|
|
try_update_realm_custom_profile_field(realm, field, name, hint=hint,
|
|
|
|
field_data=field_data)
|
2017-03-17 10:07:22 +01:00
|
|
|
except IntegrityError:
|
|
|
|
return json_error(_('A field with that name already exists.'))
|
|
|
|
return json_success()
|
|
|
|
|
2017-07-31 20:30:08 +02:00
|
|
|
@human_users_only
|
2017-03-17 10:07:22 +01:00
|
|
|
@has_request_variables
|
|
|
|
def update_user_custom_profile_data(
|
2017-10-27 02:18:49 +02:00
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2018-04-24 03:47:28 +02:00
|
|
|
data: List[Dict[str, Union[int, str]]]=REQ(validator=check_list(
|
2017-10-27 02:18:49 +02:00
|
|
|
check_dict([('id', check_int)])))) -> HttpResponse:
|
2017-03-17 10:07:22 +01:00
|
|
|
for item in data:
|
|
|
|
field_id = item['id']
|
|
|
|
try:
|
|
|
|
field = CustomProfileField.objects.get(id=field_id)
|
|
|
|
except CustomProfileField.DoesNotExist:
|
|
|
|
return json_error(_('Field id {id} not found.').format(id=field_id))
|
|
|
|
|
2018-04-08 09:50:05 +02:00
|
|
|
validators = CustomProfileField.FIELD_VALIDATORS
|
|
|
|
extended_validators = CustomProfileField.EXTENDED_FIELD_VALIDATORS
|
|
|
|
field_type = field.field_type
|
|
|
|
value = item['value']
|
2018-04-29 11:28:33 +02:00
|
|
|
var_name = '{}'.format(field.name)
|
2018-04-08 09:50:05 +02:00
|
|
|
if field_type in validators:
|
|
|
|
validator = validators[field_type]
|
|
|
|
result = validator(var_name, value)
|
|
|
|
else:
|
|
|
|
# Check extended validators.
|
|
|
|
extended_validator = extended_validators[field_type]
|
|
|
|
field_data = field.field_data
|
|
|
|
result = extended_validator(var_name, field_data, value)
|
|
|
|
|
2017-03-17 10:07:22 +01:00
|
|
|
if result is not None:
|
|
|
|
return json_error(result)
|
|
|
|
|
|
|
|
do_update_user_custom_profile_data(user_profile, data)
|
|
|
|
# We need to call this explicitly otherwise constraints are not check
|
|
|
|
return json_success()
|