本文整理汇总了Python中rest_framework.exceptions.NotAcceptable方法的典型用法代码示例。如果您正苦于以下问题:Python exceptions.NotAcceptable方法的具体用法?Python exceptions.NotAcceptable怎么用?Python exceptions.NotAcceptable使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rest_framework.exceptions
的用法示例。
在下文中一共展示了exceptions.NotAcceptable方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle_exception
# 需要导入模块: from rest_framework import exceptions [as 别名]
# 或者: from rest_framework.exceptions import NotAcceptable [as 别名]
def handle_exception(self, exc):
response = super(ResponseExceptionMixin, self).handle_exception(exc)
renderers = tuple(api_settings.DEFAULT_RENDERER_CLASSES)
accepted = getattr(self.request, 'accepted_renderer', None)
if (response.exception and accepted
and not isinstance(accepted, renderers)):
conneg = self.get_content_negotiator()
try:
render_cls, mtype = conneg.select_renderer(
self.request, renderers, self.format_kwarg)
except exceptions.NotAcceptable:
render_cls = TemplateHTMLRenderer
mtype = render_cls.media_type
self.request.accepted_renderer = render_cls()
self.request.accepted_media_type = mtype
return response
示例2: validate_answer
# 需要导入模块: from rest_framework import exceptions [as 别名]
# 或者: from rest_framework.exceptions import NotAcceptable [as 别名]
def validate_answer(self, request, **kwargs):
submission = Submission.objects.get(pk=kwargs["pk"])
answer_pk = request.query_params.get("answer")
if answer_pk is None:
raise exceptions.NotAcceptable("No answer specified")
try:
answer = submission.answers.get(pk=answer_pk)
except Answer.DoesNotExist:
raise exceptions.NotFound("Answer not found")
if answer.question.question_type != TEXT_FIELD:
raise exceptions.NotAcceptable("Only text answers can be hidden")
return submission, answer
示例3: options
# 需要导入模块: from rest_framework import exceptions [as 别名]
# 或者: from rest_framework.exceptions import NotAcceptable [as 别名]
def options(self, request, *args, **kwargs):
if isinstance(self.request.accepted_renderer,
renderers.gdal.BaseGDALRenderer):
raise exceptions.NotAcceptable
return super(BaseRasterView, self).options(request, *args, **kwargs)
示例4: raise_exception_treat
# 需要导入模块: from rest_framework import exceptions [as 别名]
# 或者: from rest_framework.exceptions import NotAcceptable [as 别名]
def raise_exception_treat(func):
@functools.wraps(func)
def inner(self, request, *args, **kwargs):
try:
return func(self, request, *args, **kwargs)
except ValidationError, error:
log.error(error)
raise rest_exceptions.ValidationExceptionJson(error)
except (exceptions_api.APIException, exceptions_api.AuthenticationFailed,
exceptions_api.MethodNotAllowed, exceptions_api.NotAcceptable,
exceptions_api.NotAuthenticated, exceptions_api.ParseError,
exceptions_api.PermissionDenied, exceptions_api.Throttled,
exceptions_api.UnsupportedMediaType, rest_exceptions.ValidationAPIException), error:
log.error(error)
raise error
示例5: determine_version
# 需要导入模块: from rest_framework import exceptions [as 别名]
# 或者: from rest_framework.exceptions import NotAcceptable [as 别名]
def determine_version(self, request, *args, **kwargs):
media_type = _MediaType(request.accepted_media_type)
version = media_type.params.get(self.version_param, self.default_version)
version = unicode_http_header(version)
if not self.is_allowed_version(version):
raise exceptions.NotAcceptable(self.invalid_version_message)
return version
# We don't need to implement `reverse`, as the versioning is based
# on the `Accept` header, not on the request URL.
示例6: clear
# 需要导入模块: from rest_framework import exceptions [as 别名]
# 或者: from rest_framework.exceptions import NotAcceptable [as 别名]
def clear(self, request, *args, **kwargs):
'''
Clear history output.
'''
default_message = "Output trancated.\n"
obj = self.get_object()
if obj.status in ["RUN", "DELAY"] or obj.raw_stdout == default_message: # nocv
raise excepts.NotAcceptable(
"Job is running or already trancated"
)
obj.raw_stdout = default_message
result = self.get_serializer(obj).get_raw(request)
return base.Response(result, status.HTTP_204_NO_CONTENT).resp
示例7: select_renderer
# 需要导入模块: from rest_framework import exceptions [as 别名]
# 或者: from rest_framework.exceptions import NotAcceptable [as 别名]
def select_renderer(self, request, renderers, format_suffix=None):
"""
Given a request and a list of renderers, return a two-tuple of:
(renderer, media type).
"""
# Allow URL style format override. eg. "?format=json
format_query_param = self.settings.URL_FORMAT_OVERRIDE
format = format_suffix or request.query_params.get(format_query_param)
if format:
renderers = self.filter_renderers(renderers, format)
accepts = self.get_accept_list(request)
# Check the acceptable media types against each renderer,
# attempting more specific media types first
# NB. The inner loop here isn't as bad as it first looks :)
# Worst case is we're looping over len(accept_list) * len(self.renderers)
for media_type_set in order_by_precedence(accepts):
for renderer in renderers:
for media_type in media_type_set:
if media_type_matches(renderer.media_type, media_type):
# Return the most specific media type as accepted.
media_type_wrapper = _MediaType(media_type)
if (
_MediaType(renderer.media_type).precedence >
media_type_wrapper.precedence
):
# Eg client requests '*/*'
# Accepted media type is 'application/json'
full_media_type = ';'.join(
(renderer.media_type,) +
tuple('{0}={1}'.format(
key, value.decode(HTTP_HEADER_ENCODING))
for key, value in media_type_wrapper.params.items()))
return renderer, full_media_type
else:
# Eg client requests 'application/json; indent=8'
# Accepted media type is 'application/json; indent=8'
return renderer, media_type
raise exceptions.NotAcceptable(available_renderers=renderers)