Commits

Jesper Nøhr  committed 6558b3e

adding a few checks to see whether people can create private repositories
if a handler specifies a function and that function name is in the list of fields,
attempt to call the handler function with the data as the first argument (this is usually the model)
this allows for some pretty cool arbitrary "columns" on top of handlers attached to models

  • Participants
  • Parent commits 5dda441

Comments (0)

Files changed (2)

File piston/emitters.py

-import types, decimal, yaml, types, re
+import types, decimal, yaml, types, re, inspect
 from django.db.models.query import QuerySet
 from django.db.models import Model, permalink
 from django.utils import simplejson
     usually the only method you want to use in your
     emitter. See below for examples.
     """
-    def __init__(self, payload, typemapper, fields=()):
+    def __init__(self, payload, typemapper, handler, fields=()):
         self.typemapper = typemapper
         self.data = payload
+        self.handler = handler
         self.fields = fields
         
         if isinstance(self.data, Exception):
             elif isinstance(thing, HttpResponse):
                 raise HttpStatusCode(thing.content, code=thing.status_code)
             elif isinstance(thing, types.FunctionType):
-                pass
+                if not inspect.getargspec(thing)[0]:
+                    ret = _any(thing())
             else:
                 ret = smart_unicode(thing, strings_only=True)
 
                                     get_fields.discard(field)
                                     
                 else:
-
                     get_fields = set(fields)
 
                 for f in data._meta.local_fields:
                         if maybe:
                             if isinstance(maybe, (int, basestring)):
                                 ret[maybe_field] = _any(maybe)
+                        else:
+                            handler_f = getattr(self.handler, maybe_field, None)
+
+                            if handler_f:
+                                ret[maybe_field] = handler_f(self.data)                            
 
             else:
                 for f in data._meta.fields:

File piston/resource.py

             result = e
         
         emitter, ct = Emitter.get(request.GET.get('format', 'json'))
-        srl = emitter(result, typemapper, handler.fields)
+        srl = emitter(result, typemapper, handler, handler.fields)
 
         try:
             return HttpResponse(srl.render(request), mimetype=ct)