Repository: simphony/tornado-webapi

Issue:
Add Exists exception
In case we generate a resource that already exists for whatever reason

Comments:

Patch:
diff --git a/tornadowebapi/exceptions.py b/tornadowebapi/exceptions.py
index 60bd639..64a3a2e 100644
--- a/tornadowebapi/exceptions.py
+++ b/tornadowebapi/exceptions.py
@@ -44,6 +44,19 @@ class NotFound(WebAPIException):
         return None
 
 
+class Exists(WebAPIException):
+    """Represents a case where the resource could not be created
+    because it already exists. This is generally raised in the
+    create() method if the resource has uniqueness constraints on
+    things other than the exposed id."""
+
+    http_code = httpstatus.CONFLICT
+
+    def representation(self):
+        """Exists does not have a representation, just an error status"""
+        return None
+
+
 class BadRepresentation(WebAPIException):
     """Exception raised when the resource representation is
     invalid or does not contain the appropriate keys.
diff --git a/tornadowebapi/handler.py b/tornadowebapi/handler.py
index 609a1c5..6b78df0 100644
--- a/tornadowebapi/handler.py
+++ b/tornadowebapi/handler.py
@@ -102,7 +102,7 @@ class CollectionHandler(BaseHandler):
 
         self.set_status(httpstatus.OK)
         # Need to convert into a dict for security issue tornado/1009
-        self.write({"items": list(items)})
+        self.write({"items": [str(item) for item in items]})
         self.flush()
 
     @gen.coroutine
@@ -134,7 +134,7 @@ class CollectionHandler(BaseHandler):
             raise web.HTTPError(httpstatus.INTERNAL_SERVER_ERROR)
 
         location = with_end_slash(
-            url_path_join(self.request.full_url(), resource_id))
+            url_path_join(self.request.full_url(), str(resource_id)))
 
         self.set_status(httpstatus.CREATED)
         self.set_header("Location", location)


Test patch:
diff --git a/tornadowebapi/tests/test_webapi.py b/tornadowebapi/tests/test_webapi.py
index e3f3b8b..3c707a1 100644
--- a/tornadowebapi/tests/test_webapi.py
+++ b/tornadowebapi/tests/test_webapi.py
@@ -26,8 +26,8 @@ class Student(Resource):
 
     @gen.coroutine
     def create(self, representation):
-        id = str(type(self).id)
-        self.collection[id] = representation
+        id = type(self).id
+        self.collection[str(id)] = representation
         type(self).id += 1
         return id
 
@@ -107,6 +107,12 @@ class Broken(Resource):
     items = boom
 
 
+class AlreadyPresent(Resource):
+    @gen.coroutine
+    def create(self, *args):
+        raise exceptions.Exists()
+
+
 class TestREST(AsyncHTTPTestCase):
     def setUp(self):
         super().setUp()
@@ -120,6 +126,7 @@ class TestREST(AsyncHTTPTestCase):
         registry.registry.register(Unprocessable)
         registry.registry.register(UnsupportsCollection)
         registry.registry.register(Broken)
+        registry.registry.register(AlreadyPresent)
         app = web.Application(handlers=handlers)
         app.hub = mock.Mock()
         return app
@@ -138,7 +145,7 @@ class TestREST(AsyncHTTPTestCase):
         res = self.fetch("/api/v1/students/")
         self.assertEqual(res.code, httpstatus.OK)
         self.assertEqual(escape.json_decode(res.body),
-                         {"items": [1, 2, 3]})
+                         {"items": ["1", "2", "3"]})
 
     def test_create(self):
         res = self.fetch(
@@ -402,6 +409,12 @@ class TestREST(AsyncHTTPTestCase):
             method="GET")
         self.assertEqual(res.code, httpstatus.METHOD_NOT_ALLOWED)
 
+    def test_exists(self):
+        collection_url = "/api/v1/alreadypresents/"
+
+        res = self.fetch(collection_url, method="POST", body="{}")
+        self.assertEqual(res.code, httpstatus.CONFLICT)
+
 
 class TestRESTFunctions(unittest.TestCase):
     def test_api_handlers(self):