Commits

Jesper Nøhr  committed 5dd2366

cleanup: getting rid of @resource_uri, reusing djangos permalinks instead, can be overwritten with 'resource_uri' staticmethod in handler.. resource url per model, not per handler, makes more sense.. get_api_url permalink on models now respected

  • Participants
  • Parent commits ab8a03e

Comments (0)

Files changed (4)

File piston/emitters.py

 import types, decimal, yaml, copy
 from django.db.models.query import QuerySet
-from django.db.models import Model
+from django.db.models import Model, permalink
 from django.utils import simplejson
 from django.utils.xmlutils import SimplerXMLGenerator
 from django.utils.encoding import smart_unicode
             
                 for k in add_ons:
                     ret[k] = _any(getattr(data, k))
-                
+            
+            # resouce uri
+            if type(data) in self.typemapper.keys():
+                handler = self.typemapper.get(type(data))
+                if hasattr(handler, 'resource_uri'):
+                    url_id, fields = handler.resource_uri()
+                    ret['resource_uri'] = permalink( lambda: (url_id, 
+                        (getattr(data, f) for f in fields) ) )()
+            elif hasattr(data, 'get_api_url'):
+                try: ret['resource_uri'] = data.get_api_url()
+                except: pass
+            
             return ret
             
         def _list(data):
     @classmethod
     def get(cls, format):
         if format == 'xml':
-            return XMLEmitter, 'text/plain'
+            return XMLEmitter, 'text/plain; charset=utf-8'
         elif format == 'json':
-            return JSONEmitter, 'text/plain'
+            return JSONEmitter, 'text/plain; charset=utf-8'
         elif format == 'yaml':
-            return YAMLEmitter, 'text/plain'
+            return YAMLEmitter, 'text/plain; charset=utf-8'
     
 class XMLEmitter(Emitter):
     def _to_xml(self, xml, data):

File piston/handler.py

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

File piston/resource.py

         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)
         

File piston/utils.py

         request.method = "PUT"
         request.PUT = 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
+