def _get_field_or_error(self, field_name): """ Check that the attempted filter field is valid :raises InvalidFilterError: If the filter field is not valid """ predeclared_fields = self.serializer_class._declared_fields initialized_fields = self.get_serializer().fields if hasattr( self, 'get_serializer') else {} serializer_fields = predeclared_fields.copy() # Merges fields that were declared on serializer with fields that may have been dynamically added serializer_fields.update(initialized_fields) if field_name not in serializer_fields: raise InvalidFilterError( detail="'{0}' is not a valid field for this endpoint.".format( field_name)) if field_name not in getattr(self.serializer_class, 'filterable_fields', set()): raise InvalidFilterFieldError(parameter='filter', value=field_name) field = serializer_fields[field_name] # You cannot filter on deprecated fields. if isinstance(field, ShowIfVersion) and utils.is_deprecated( self.request.version, field.min_version, field.max_version): raise InvalidFilterFieldError(parameter='filter', value=field_name) return serializer_fields[field_name]
def to_representation(self, value): old_social_string_fields = ['twitter', 'github', 'linkedIn'] request = self.context.get('request') show_old_format = request and is_deprecated(request.version, self.min_version) and request.method == 'GET' if show_old_format: social = value.copy() for key in old_social_string_fields: if social.get(key): social[key] = value[key][0] elif social.get(key) == []: social[key] = '' value = social return super(SocialField, self).to_representation(value)
def _get_field_or_error(self, field_name): """ Check that the attempted filter field is valid :raises InvalidFilterError: If the filter field is not valid """ serializer_class = self.serializer_class if field_name not in serializer_class._declared_fields: raise InvalidFilterError(detail="'{0}' is not a valid field for this endpoint.".format(field_name)) if field_name not in getattr(serializer_class, 'filterable_fields', set()): raise InvalidFilterFieldError(parameter='filter', value=field_name) field = serializer_class._declared_fields[field_name] # You cannot filter on deprecated fields. if isinstance(field, ShowIfVersion) and utils.is_deprecated(self.request.version, field.min_version, field.max_version): raise InvalidFilterFieldError(parameter='filter', value=field_name) return serializer_class._declared_fields[field_name]
def test_is_not_deprecated(self): request_version = '2.5' is_deprecated = api_utils.is_deprecated( request_version, self.min_version, self.max_version) assert_equal(is_deprecated, False)
def has_object_permission(self, request, view, obj): if is_deprecated(request.version, self.min_version, self.max_version): raise exceptions.NotFound(detail=self.deprecated_message) return True
def test_is_not_deprecated(self): request_version = '2.5' is_deprecated = api_utils.is_deprecated(request_version, self.min_version, self.max_version) assert_equal(is_deprecated, False)
def determine_version(self, request, *args, **kwargs): version, scheme = super(DeprecatedEndpointMixin, self).determine_version(request, *args, **kwargs) if utils.is_deprecated(version, self.min_version, self.max_version): raise drf_exceptions.NotFound() return version, scheme
def test_is_deprecated_larger_versions(self): request_version = '2.10' is_deprecated = api_utils.is_deprecated(request_version, self.min_version, self.max_version) assert is_deprecated is True
def test_is_deprecated(self): request_version = "2.6" is_deprecated = api_utils.is_deprecated(request_version, self.min_version, self.max_version) assert_equal(is_deprecated, True)
def test_is_deprecated_larger_versions(self): request_version = '2.10' is_deprecated = api_utils.is_deprecated( request_version, self.min_version, self.max_version ) assert is_deprecated is True