def validate_custom_field_data(field_type: int, field_data: ProfileFieldData) -> None: try: if field_type == CustomProfileField.CHOICE: # Choice type field must have at least have one choice if len(field_data) < 1: raise JsonableError(_("Field must have at least one choice.")) validate_choice_field_data(field_data) elif field_type == CustomProfileField.EXTERNAL_ACCOUNT: validate_external_account_field_data(field_data) except ValidationError as error: raise JsonableError(error.message)
def create_realm_custom_profile_field( request: HttpRequest, user_profile: UserProfile, name: str = REQ(), hint: str = REQ(default=''), field_data: ProfileFieldData = REQ(default={}, converter=ujson.loads), field_type: int = REQ(validator=check_int) ) -> HttpResponse: validate_field_name_and_hint(name, hint) field_types = [i[0] for i in CustomProfileField.FIELD_TYPE_CHOICES] if field_type not in field_types: return json_error(_("Invalid field type.")) # Choice type field must have at least have one choice if field_type == CustomProfileField.CHOICE and len(field_data) < 1: return json_error(_("Field must have at least one choice.")) error = validate_choice_field_data(field_data) if error: return json_error(error) try: field = try_add_realm_custom_profile_field( realm=user_profile.realm, name=name, field_data=field_data, field_type=field_type, hint=hint, ) return json_success({'id': field.id}) except IntegrityError: return json_error(_("A field with that name already exists."))
def update_realm_custom_profile_field( request: HttpRequest, user_profile: UserProfile, field_id: int, name: str = REQ(), hint: str = REQ(default=''), field_data: ProfileFieldData = REQ(default={}, converter=ujson.loads), ) -> HttpResponse: validate_field_name_and_hint(name, hint) error = validate_choice_field_data(field_data) if error: return json_error(error) 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: try_update_realm_custom_profile_field(realm, field, name, hint=hint, field_data=field_data) except IntegrityError: return json_error(_('A field with that name already exists.')) return json_success()
def validate_custom_field_data(field_type: int, field_data: ProfileFieldData) -> None: error = None if field_type == CustomProfileField.CHOICE: # Choice type field must have at least have one choice if len(field_data) < 1: raise JsonableError(_("Field must have at least one choice.")) error = validate_choice_field_data(field_data) if error: raise JsonableError(error)