Commits

Jesper Nøhr committed a90c5bf

typemap refactor, distinguishes between authed/anonymous handlers now

  • Participants
  • Parent commits 93a79cd

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, handler, fields=()):
+    def __init__(self, payload, typemapper, handler, fields=(), anonymous=True):
         self.typemapper = typemapper
         self.data = payload
         self.handler = handler
         self.fields = fields
+        self.anonymous = anonymous
         
         if isinstance(self.data, Exception):
             raise
             """
             ret = { }
             
-            if type(data) in self.typemapper.keys() or fields:
+            if self.in_typemapper(type(data), self.anonymous) or fields:
 
                 v = lambda f: getattr(data, f.attname)
 
                 if not fields:
-                    get_fields = set(self.typemapper.get(type(data)).fields)
-                    exclude_fields = set(self.typemapper.get(type(data)).exclude)
+                    """
+                    Fields was not specified, try to find teh correct
+                    version in the typemapper we were sent.
+                    """
+                    mapped = self.in_typemapper(type(data), self.anonymous)
+                    get_fields = set(mapped.fields)
+                    exclude_fields = set(mapped.exclude)
                 
                     if not get_fields:
                         get_fields = set([ f.attname.replace("_id", "", 1)
                             handler_f = getattr(self.handler, maybe_field, None)
 
                             if handler_f:
-                                ret[maybe_field] = handler_f(self.data)                            
+                                ret[maybe_field] = handler_f(data)
 
             else:
                 for f in data._meta.fields:
             """
             return dict([ (k, _any(v)) for k, v in data.iteritems() ])
             
+        # Kickstart the seralizin'.
         return _any(self.data, self.fields)
     
+    def in_typemapper(self, model, anonymous):
+        for klass, (km, is_anon) in self.typemapper.iteritems():
+            if model is km and is_anon is anonymous:
+                return klass
+        
     def render(self):
         """
         This super emitter does not implement `render`,

piston/handler.py

         new_cls = type.__new__(cls, name, bases, attrs)
         
         if hasattr(new_cls, 'model'):
-            typemapper[new_cls.model] = new_cls
+            typemapper[new_cls] = (new_cls.model, new_cls.is_anonymous)
         
         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
+    anonymous = is_anonymous = False
     exclude = ( 'id' )
     fields =  ( )
     
         
         return "I can't do this yet."
 
+class AnonymousBaseHandler(BaseHandler):
+    """
+    Anonymous handler.
+    """
+    is_anonymous = True
+    allowed_methods = ('GET',)

piston/resource.py

         if not self.authentication.is_authenticated(request):
             if self.handler.anonymous and callable(self.handler.anonymous):
                 handler = self.handler.anonymous()
+                anonymous = True
             else:
                 return self.authentication.challenge()
         else:
             handler = self.handler
+            anonymous = False
         
         rm = request.method.upper()
         
         try:
             result = meth(request, *args, **kwargs)
         except FormValidationError, form:
+            # TODO: Use rc.BAD_REQUEST here
             return HttpResponse("Bad Request: %s" % form.errors, status=400)
         except TypeError, e:
             result = rc.BAD_REQUEST
                 raise
 
         emitter, ct = Emitter.get(request.GET.get('format', 'json'))
-        srl = emitter(result, typemapper, handler, handler.fields)
-
+        srl = emitter(result, typemapper, handler, handler.fields, anonymous)
+        
         try:
             return HttpResponse(srl.render(request), mimetype=ct)
         except HttpStatusCode, e: