def _get_request_representer(self): try: mime_type = \ get_registered_mime_type_for_string(self.request.content_type) except KeyError: # The client requested a content type we do not support (415). raise HTTPUnsupportedMediaType() return as_representer(self.context, mime_type)
def _get_request_representer(self): """ Returns a representer for the content type specified in the request. :raises HTTPUnsupportedMediaType: If the specified content type is not supported. """ try: mime_type = \ get_registered_mime_type_for_string(self.request.content_type) except KeyError: # The client sent a content type we do not support (415). raise HTTPUnsupportedMediaType() return as_representer(self.context, mime_type)
def _get_response_representer(self): """ Creates a representer for this view. :raises: :class:`pyramid.httpexceptions.HTTPNotAcceptable` if the MIME content type(s) the client specified can not be handled by the view. :returns: :class:`everest.representers.base.ResourceRepresenter` """ view_name = self.request.view_name if view_name != '': mime_type = get_registered_mime_type_for_name(view_name) rpr = as_representer(self.context, mime_type) else: mime_type = None acc = None for acc in self.request.accept: if acc == '*/*': # The client does not care; use the default. mime_type = self.__get_default_response_mime_type() break try: mime_type = \ get_registered_mime_type_for_string(acc.lower()) except KeyError: pass else: break if mime_type is None: if not acc is None: # The client specified a MIME type we can not handle; this # is a 406 exxception. We supply allowed MIME content # types in the body of the response. headers = \ [('Location', self.request.path_url), ('Content-Type', TextPlainMime.mime_type_string), ] mime_strings = get_registered_mime_strings() exc = HTTPNotAcceptable( 'Requested MIME content type(s) ' 'not acceptable.', body=','.join(mime_strings), headers=headers) raise exc mime_type = self.__get_default_response_mime_type() rpr = as_representer(self.context, mime_type) return rpr
def _get_response_representer(self): """ Creates a representer for this view. :raises: :class:`pyramid.httpexceptions.HTTPNotAcceptable` if the MIME content type(s) the client specified can not be handled by the view. :returns: :class:`everest.representers.base.ResourceRepresenter` """ view_name = self.request.view_name if view_name != '': mime_type = get_registered_mime_type_for_name(view_name) rpr = as_representer(self.context, mime_type) else: mime_type = None acc = None for acc in self.request.accept: if acc == '*/*': # The client does not care; use the default. mime_type = self.__get_default_response_mime_type() break try: mime_type = \ get_registered_mime_type_for_string(acc.lower()) except KeyError: pass else: break if mime_type is None: if not acc is None: # The client specified a MIME type we can not handle; this # is a 406 exxception. We supply allowed MIME content # types in the body of the response. headers = \ [('Location', self.request.path_url), ('Content-Type', TextPlainMime.mime_type_string), ] mime_strings = get_registered_mime_strings() exc = HTTPNotAcceptable('Requested MIME content type(s) ' 'not acceptable.', body=','.join(mime_strings), headers=headers) raise exc mime_type = self.__get_default_response_mime_type() rpr = as_representer(self.context, mime_type) return rpr
def test_registry(self): self.assert_raises(ValueError, register_mime_type, MimeNotImplementingIMime) self.assert_raises(ValueError, register_mime_type, MimeWithDuplicateTypeString) self.assert_raises(ValueError, register_mime_type, MimeWithDuplicateNameString) self.assert_raises(ValueError, register_mime_type, MimeWithDuplicateFileExtensionString) self.assert_true(XmlMime.mime_type_string in get_registered_mime_strings()) self.assert_true(XmlMime.representer_name in get_registered_representer_names()) self.assert_true(XmlMime in get_registered_mime_types()) self.assert_true( get_registered_mime_type_for_string(XmlMime.mime_type_string) is XmlMime) self.assert_true( get_registered_mime_type_for_name(XmlMime.representer_name) is XmlMime) self.assert_equal( get_registered_mime_type_for_extension(XmlMime.file_extension), XmlMime)