Commits

Jesper Nøhr committed ab8a03e

resource identifiers, old testcases fied, a few new ones, automatic endpoint stuff

  • Participants
  • Parent commits 8424986

Comments (0)

Files changed (4)

piston/emitters.py

         self.typemapper = typemapper
         self.data = payload
     
+        if isinstance(self.data, Exception):
+            raise
+    
     def construct(self):
         
         def _any(thing):
                             
                 # try to get the remainder of fields
                 for maybe_field in want_fields:
-                    pass # do something here?
+                    maybe = getattr(data, maybe_field, None)
+
+                    if maybe:
+                        if isinstance(maybe, (int, basestring)):
+                            ret[maybe_field] = _any(maybe)
                             
             else:
 

piston/handler.py

             raise NotImplementedError
 
         return "I can't do this yet."
+        
+    def get_resource_uri(self, *args, **kwargs):
+        return '', ''

piston/resource.py

             return HttpResponse("errors: %r" % errors)
         except Exception, e:
             result = e
-            
+        
+        resource_uri = self.handler.get_resource_uri(result)
+
+        # TODO: Make much more robust
+        if resource_uri: 
+            if isinstance(result, dict):
+                result['resource_uri'] = resource_uri
+            elif not isinstance(result, basestring):
+                setattr(result, 'resource_uri', resource_uri)
+        
         emitter, ct = Emitter.get(format)
         srl = emitter(result, typemapper)
         
 from django.http import HttpResponseNotAllowed, HttpResponseForbidden
+from django.core.urlresolvers import reverse
 
 class HttpForbidden(HttpResponseForbidden):
     pass
         request._load_post_and_files()
         request.method = "PUT"
         request.PUT = request.POST
-        del request._post
+        del request._post
+        
+def resource_uri(f):
+    def wrap(self, instance):
+        url_id, args = f(self, instance)
+        try:
+            return reverse(url_id, args=args)
+        except Exception, e:
+            return None
+    return wrap