def patch(self, request, field_id): """Updates the field if the user belongs to the group.""" field = (FieldHandler().get_field( field_id, base_queryset=Field.objects.select_for_update()).specific) type_name = type_from_data_or_registry(request.data, field_type_registry, field) field_type = field_type_registry.get(type_name) data = validate_data_custom_fields( type_name, field_type_registry, request.data, base_serializer_class=UpdateFieldSerializer, ) # Because each field type can raise custom exceptions at while updating the # field we need to be able to map those to the correct API exceptions which are # defined in the type. with field_type.map_api_exceptions(): field = FieldHandler().update_field(request.user, field, type_name, **data) serializer = field_type_registry.get_serializer(field, FieldSerializer) return Response(serializer.data)
def patch(self, request, view_id): """Updates the view if the user belongs to the group.""" view = ViewHandler().get_view(request.user, view_id).specific view_type = view_type_registry.get_by_model(view) data = validate_data_custom_fields( view_type.type, view_type_registry, request.data, base_serializer_class=UpdateViewSerializer ) view = ViewHandler().update_view(request.user, view, **data) serializer = view_type_registry.get_serializer(view, ViewSerializer) return Response(serializer.data)
def patch(self, request, field_id): """Updates the field if the user belongs to the group.""" field = FieldHandler().get_field( request.user, field_id, base_queryset=Field.objects.select_for_update()).specific type_name = type_from_data_or_registry(request.data, field_type_registry, field) data = validate_data_custom_fields( type_name, field_type_registry, request.data, base_serializer_class=UpdateFieldSerializer) field = FieldHandler().update_field(request.user, field, type_name, **data) serializer = field_type_registry.get_serializer(field, FieldSerializer) return Response(serializer.data)
def test_validate_data_custom_fields(): registry = TemporaryTypeRegistry() registry.register(TemporaryInstanceType1()) registry.register(TemporaryInstanceType2()) with pytest.raises(APIException) as api_exception: validate_data_custom_fields('NOT_EXISTING', registry, {}) assert api_exception.value.detail[ 'error'] == 'ERROR_REQUEST_BODY_VALIDATION' assert api_exception.value.detail['detail']['type'][0]['error'] == \ '"NOT_EXISTING" is not a valid choice.' assert api_exception.value.detail['detail']['type'][0][ 'code'] == 'invalid_choice' assert api_exception.value.status_code == status.HTTP_400_BAD_REQUEST with pytest.raises(APIException) as api_exception_2: validate_data_custom_fields('temporary_2', registry, {}) assert api_exception_2.value.detail[ 'error'] == 'ERROR_REQUEST_BODY_VALIDATION' assert api_exception_2.value.detail['detail']['name'][0]['error'] == \ 'This field is required.' assert api_exception_2.value.detail['detail']['name'][0][ 'code'] == 'required' assert api_exception_2.value.status_code == status.HTTP_400_BAD_REQUEST with pytest.raises(APIException) as api_exception_3: validate_data_custom_fields('temporary_2', registry, {'name': 'test1'}) assert api_exception_3.value.detail[ 'error'] == 'ERROR_REQUEST_BODY_VALIDATION' assert api_exception_3.value.detail['detail']['name'][0]['error'] == \ 'A valid integer is required.' assert api_exception_3.value.detail['detail']['name'][0][ 'code'] == 'invalid' assert api_exception_3.value.status_code == status.HTTP_400_BAD_REQUEST data = validate_data_custom_fields('temporary_2', registry, {'name': 123}) assert data['name'] == 123
def test_validate_data_custom_fields(): registry = TemporaryTypeRegistry() registry.register(TemporaryInstanceType1()) registry.register(TemporaryInstanceType2()) with pytest.raises(APIException) as api_exception: validate_data_custom_fields("NOT_EXISTING", registry, {}) assert api_exception.value.detail[ "error"] == "ERROR_REQUEST_BODY_VALIDATION" assert api_exception.value.detail["detail"]["type"][0]["error"] == ( '"NOT_EXISTING" is not a valid choice.') assert api_exception.value.detail["detail"]["type"][0][ "code"] == "invalid_choice" assert api_exception.value.status_code == status.HTTP_400_BAD_REQUEST with pytest.raises(APIException) as api_exception_2: validate_data_custom_fields("temporary_2", registry, {}) assert api_exception_2.value.detail[ "error"] == "ERROR_REQUEST_BODY_VALIDATION" assert api_exception_2.value.detail["detail"]["name"][0]["error"] == ( "This field is required.") assert api_exception_2.value.detail["detail"]["name"][0][ "code"] == "required" assert api_exception_2.value.status_code == status.HTTP_400_BAD_REQUEST with pytest.raises(APIException) as api_exception_3: validate_data_custom_fields("temporary_2", registry, {"name": "test1"}) assert api_exception_3.value.detail[ "error"] == "ERROR_REQUEST_BODY_VALIDATION" assert api_exception_3.value.detail["detail"]["name"][0]["error"] == ( "A valid integer is required.") assert api_exception_3.value.detail["detail"]["name"][0][ "code"] == "invalid" assert api_exception_3.value.status_code == status.HTTP_400_BAD_REQUEST data = validate_data_custom_fields("temporary_2", registry, {"name": 123}) assert data["name"] == 123