def serviceDirective( _context, method, accept, factory, for_, permission, layer=IDefaultBrowserLayer, name=u"", ): _handle_for(_context, for_) media_types = parse_accept_header(accept) for media_type in media_types: service_id = register_service(method.upper(), media_type) view_name = service_id + name # We need a service for CORS preflight processing but we don't get the # desired Accept header in the preflight request. Thus we just register # the current service_id for the given method. register_method_for_preflight(method.upper(), service_id) # Create a new class. We'll execute some security declarations on it # and don't want to modify the original class. cdict = getSecurityInfo(factory) cdict["__name__"] = view_name cdict["method"] = method.upper() new_class = type(factory.__name__, (factory, BrowserView), cdict) _context.action( discriminator=("plone.rest:service", method, media_type, for_, name, layer), callable=handler, args=( "registerAdapter", new_class, (for_, layer), Interface, view_name, _context.info, ), ) _context.action( discriminator=("plone.rest:protectClass", new_class), callable=protectClass, args=(new_class, permission), ) _context.action( discriminator=("plone.rest:InitializeClass", new_class), callable=InitializeClass, args=(new_class, ), )
def serviceDirective( _context, method, accept, factory, for_, permission, layer=IDefaultBrowserLayer, name=u'', ): _handle_for(_context, for_) media_types = parse_accept_header(accept) for media_type in media_types: service_id = register_service(method.upper(), media_type) view_name = service_id + name # We need a service for CORS preflight processing but we don't get the # desired Accept header in the preflight request. Thus we just register # the current service_id for the given method. register_method_for_preflight(method.upper(), service_id) # Create a new class. We'll execute some security declarations on it # and don't want to modify the original class. cdict = getSecurityInfo(factory) cdict['__name__'] = view_name cdict['method'] = method.upper() new_class = type(factory.__name__, (factory, BrowserView), cdict) _context.action( discriminator=('plone.rest:service', method, media_type, for_, name, layer), callable=handler, args=('registerAdapter', new_class, (for_, layer), Interface, view_name, _context.info), ) _context.action( discriminator=('plone.rest:protectClass', new_class), callable=protectClass, args=(new_class, permission) ) _context.action( discriminator=('plone.rest:InitializeClass', new_class), callable=InitializeClass, args=(new_class,) )
def test_service_id_for_wildcard_subtype_is_none(self): register_service("GET", "text/xml") self.assertEqual(None, lookup_service_id("GET", "text/*"))
def test_service_id_for_multiple_media_types_is_none(self): register_service("GET", "application/json") self.assertEqual( None, lookup_service_id("GET", "application/json,application/javascipt"))
def test_service_id_for_wildcard_type_is_none(self): register_service("GET", "application/json") self.assertEqual(None, lookup_service_id("GET", "*/*"))
def test_register_wildcard_subtype(self): self.assertEqual(u"PATCH_text_*_", register_service("PATCH", ("text", "*"))) self.assertEqual(u"PATCH_text_*_", lookup_service_id("PATCH", "text/xml"))
def test_register_wilcard_type(self): self.assertEqual(u"PATCH_*_*_", register_service("PATCH", ("*", "*"))) self.assertEqual(u"PATCH_*_*_", lookup_service_id("PATCH", "foo/bar"))
def test_service_id_for_wildcard_subtype_is_none(self): register_service('GET', 'text/xml') self.assertEqual(None, lookup_service_id('GET', 'text/*'))
def test_register_media_type(self): self.assertEqual(u"GET_application_json_", register_service("GET", ("application", "json"))) self.assertEqual(u"GET_application_json_", lookup_service_id("GET", "application/json"))
def test_service_id_for_wildcard_type_is_none(self): register_service('GET', 'application/json') self.assertEqual(None, lookup_service_id('GET', '*/*'))
def test_service_id_for_multiple_media_types_is_none(self): register_service('GET', 'application/json') self.assertEqual(None, lookup_service_id( 'GET', 'application/json,application/javascipt'))
def test_register_wilcard_type(self): self.assertEqual(u'PATCH_*_*_', register_service('PATCH', ('*', '*'))) self.assertEqual(u'PATCH_*_*_', lookup_service_id('PATCH', 'foo/bar'))
def test_register_wildcard_subtype(self): self.assertEqual(u'PATCH_text_*_', register_service('PATCH', ('text', '*'))) self.assertEqual(u'PATCH_text_*_', lookup_service_id('PATCH', 'text/xml'))
def test_register_media_type(self): self.assertEqual(u'GET_application_json_', register_service('GET', ('application', 'json'))) self.assertEqual(u'GET_application_json_', lookup_service_id('GET', 'application/json'))