Commits

Jesper Nøhr committed ac2ebcf

fallback handler for unauthenticated access works now

  • Participants
  • Parent commits 2fe6f47

Comments (0)

Files changed (3)

piston/emitters.py

     usually the only method you want to use in your
     emitter. See below for examples.
     """
-    def __init__(self, payload, typemapper):
+    def __init__(self, payload, typemapper, fields=()):
         self.typemapper = typemapper
         self.data = payload
+        self.fields = fields
         
         if isinstance(self.data, Exception):
             raise
             elif isinstance(thing, decimal.Decimal):
                 ret = str(thing)
             elif isinstance(thing, Model):
-                ret = _model(thing, fields=())
+                ret = _model(thing, fields=fields)
             elif isinstance(thing, types.FunctionType):
                 pass
             else:
             """
             return dict([ (k, _any(v)) for k, v in data.iteritems() ])
             
-        return _any(self.data)
+        return _any(self.data, self.fields)
     
     def render(self):
         """

piston/handler.py

         
         return new_cls
 
+class AnonymousBaseHandler(object):
+    """
+    Anonymous handler.
+    """
+    allowed_methods = ('GET',)
+    
 class BaseHandler(object):
     """
     Basehandler that gives you CRUD for free.
     __metaclass__ = HandlerMetaClass
     
     allowed_methods = ('GET', 'POST', 'PUT', 'DELETE')
+    anonymous = False
     exclude = ( 'id' )
     fields =  ( )
     

piston/resource.py

         that are different (OAuth stuff in `Authorization` header.)
         """
         if not self.authentication.is_authenticated(request):
-            return self.authentication.challenge()
+            if self.handler.anonymous and callable(self.handler.anonymous):
+                handler = self.handler.anonymous()
+            else:
+                return self.authentication.challenge()
+        else:
+            handler = self.handler
         
         rm = request.method.upper()
         
         if rm == "PUT":
             coerce_put_post(request)
         
-        if not rm in self.handler.allowed_methods:
-            return HttpResponseNotAllowed(self.handler.allowed_methods)
+        if not rm in handler.allowed_methods:
+            return HttpResponseNotAllowed(handler.allowed_methods)
         
-        meth = getattr(self.handler, Resource.callmap.get(rm), None)
+        meth = getattr(handler, Resource.callmap.get(rm), None)
         
         if not meth:
             raise Http404
             result = e
         
         emitter, ct = Emitter.get(request.GET.get('format', 'json'))
-        srl = emitter(result, typemapper)
+        srl = emitter(result, typemapper, handler.fields)
         
         return HttpResponse(srl.render(), mimetype=ct)