コード例 #1
0
def update_realm_custom_profile_field(
    request: HttpRequest,
    user_profile: UserProfile,
    field_id: int,
    name: str = REQ(default="", converter=lambda x: x.strip()),
    hint: str = REQ(default=""),
    field_data: ProfileFieldData = REQ(default={}, converter=orjson.loads),
) -> HttpResponse:
    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))

    if field.field_type == CustomProfileField.EXTERNAL_ACCOUNT:
        if is_default_external_field(field.field_type,
                                     orjson.loads(field.field_data)):
            return json_error(_("Default custom field cannot be updated."))

    validate_custom_profile_field(name, hint, field.field_type, field_data)
    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 label already exists."))
    return json_success()
コード例 #2
0
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:
    if not name.strip():
        return json_error(_("Name cannot be blank."))

    error = hint_validator('hint', hint)
    if error:
        return json_error(error, data={'field': 'hint'})

    error = validate_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()
コード例 #3
0
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:
    if not name.strip():
        return json_error(_("Name cannot be blank."))

    error = hint_validator('hint', hint)
    if error:
        return json_error(error, data={'field': 'hint'})

    error = validate_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()
コード例 #4
0
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)

    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))

    validate_custom_field_data(field.field_type, field_data)
    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()
コード例 #5
0
def update_realm_custom_profile_field(request: HttpRequest, user_profile: UserProfile,
                                      field_id: int, name: Text=REQ()) -> HttpResponse:
    if not name.strip():
        return json_error(_("Name cannot be blank."))

    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)
    except IntegrityError:
        return json_error(_('A field with that name already exists.'))
    return json_success()
コード例 #6
0
def update_realm_custom_profile_field(request,
                                      user_profile,
                                      field_id,
                                      name=REQ()):
    # type: (HttpRequest, UserProfile, int, Text) -> HttpResponse
    if not name.strip():
        return json_error(_("Name cannot be blank."))

    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)
    except IntegrityError:
        return json_error(_('A field with that name already exists.'))
    return json_success()