for resource in self.ext_mgr.get_resources():
LOG.debug(_('Extended resource: %s'),
resource.collection)
+ for action, method in resource.collection_actions.iteritems():
+ path_prefix = ""
+ parent = resource.parent
+ conditions = dict(method=[method])
+ path = "/%s/%s" % (resource.collection, action)
+ if parent:
+ path_prefix = "/%s/{%s_id}" % (parent["collection_name"],
+ parent["member_name"])
+ with mapper.submapper(controller=resource.controller,
+ action=action,
+ path_prefix=path_prefix,
+ conditions=conditions) as submap:
+ submap.connect(path)
+ submap.connect("%s.:(format)" % path)
mapper.resource(resource.collection, resource.collection,
controller=resource.controller,
- collection=resource.collection_actions,
member=resource.member_actions,
parent_resource=resource.parent)
def custom_member_action(self, request, id):
return {'member_action': 'value'}
- def custom_collection_action(self, request):
+ def custom_collection_action(self, request, **kwargs):
return {'collection': 'value'}
def test_resource_can_be_added_as_extension(self):
self.assertEqual(200, response.status_int)
self.assertEqual(json.loads(response.body)['member_action'], "value")
- def test_resource_extension_with_custom_collection_action(self):
+ def test_resource_extension_for_get_custom_collection_action(self):
controller = self.ResourceExtensionController()
collections = {'custom_collection_action': "GET"}
res_ext = extensions.ResourceExtension('tweedles', controller,
self.assertEqual(200, response.status_int)
self.assertEqual(json.loads(response.body)['collection'], "value")
+ def test_resource_extension_for_put_custom_collection_action(self):
+ controller = self.ResourceExtensionController()
+ collections = {'custom_collection_action': "PUT"}
+ res_ext = extensions.ResourceExtension('tweedles', controller,
+ collection_actions=collections)
+ test_app = setup_extensions_test_app(SimpleExtensionManager(res_ext))
+
+ response = test_app.put("/tweedles/custom_collection_action")
+
+ self.assertEqual(200, response.status_int)
+ self.assertEqual(json.loads(response.body)['collection'], 'value')
+
+ def test_resource_extension_for_post_custom_collection_action(self):
+ controller = self.ResourceExtensionController()
+ collections = {'custom_collection_action': "POST"}
+ res_ext = extensions.ResourceExtension('tweedles', controller,
+ collection_actions=collections)
+ test_app = setup_extensions_test_app(SimpleExtensionManager(res_ext))
+
+ response = test_app.post("/tweedles/custom_collection_action")
+
+ self.assertEqual(200, response.status_int)
+ self.assertEqual(json.loads(response.body)['collection'], 'value')
+
+ def test_resource_extension_for_delete_custom_collection_action(self):
+ controller = self.ResourceExtensionController()
+ collections = {'custom_collection_action': "DELETE"}
+ res_ext = extensions.ResourceExtension('tweedles', controller,
+ collection_actions=collections)
+ test_app = setup_extensions_test_app(SimpleExtensionManager(res_ext))
+
+ response = test_app.delete("/tweedles/custom_collection_action")
+
+ self.assertEqual(200, response.status_int)
+ self.assertEqual(json.loads(response.body)['collection'], 'value')
+
+ def test_resource_ext_for_formatted_req_on_custom_collection_action(self):
+ controller = self.ResourceExtensionController()
+ collections = {'custom_collection_action': "GET"}
+ res_ext = extensions.ResourceExtension('tweedles', controller,
+ collection_actions=collections)
+ test_app = setup_extensions_test_app(SimpleExtensionManager(res_ext))
+
+ response = test_app.get("/tweedles/custom_collection_action.json")
+
+ self.assertEqual(200, response.status_int)
+ self.assertEqual(json.loads(response.body)['collection'], "value")
+
+ def test_resource_ext_for_nested_resource_custom_collection_action(self):
+ controller = self.ResourceExtensionController()
+ collections = {'custom_collection_action': "GET"}
+ parent = dict(collection_name='beetles', member_name='beetle')
+ res_ext = extensions.ResourceExtension('tweedles', controller,
+ collection_actions=collections,
+ parent=parent)
+ test_app = setup_extensions_test_app(SimpleExtensionManager(res_ext))
+
+ response = test_app.get("/beetles/beetle_id"
+ "/tweedles/custom_collection_action")
+
+ self.assertEqual(200, response.status_int)
+ self.assertEqual(json.loads(response.body)['collection'], "value")
+
def test_returns_404_for_non_existant_extension(self):
test_app = setup_extensions_test_app(SimpleExtensionManager(None))