- Added librarian as a submodule.
[wolnelektury.git] / apps / piston / handler.py
index 2d28bb3..a128d7a 100644 (file)
@@ -19,17 +19,17 @@ class HandlerMetaClass(type):
             for k, (m, a) in typemapper.iteritems():
                 if model == m and anon == a:
                     return k
             for k, (m, a) in typemapper.iteritems():
                 if model == m and anon == a:
                     return k
-        
+
         if hasattr(new_cls, 'model'):
             if already_registered(new_cls.model, new_cls.is_anonymous):
                 if not getattr(settings, 'PISTON_IGNORE_DUPE_MODELS', False):
                     warnings.warn("Handler already registered for model %s, "
                         "you may experience inconsistent results." % new_cls.model.__name__)
         if hasattr(new_cls, 'model'):
             if already_registered(new_cls.model, new_cls.is_anonymous):
                 if not getattr(settings, 'PISTON_IGNORE_DUPE_MODELS', False):
                     warnings.warn("Handler already registered for model %s, "
                         "you may experience inconsistent results." % new_cls.model.__name__)
-                
+
             typemapper[new_cls] = (new_cls.model, new_cls.is_anonymous)
         else:
             typemapper[new_cls] = (None, new_cls.is_anonymous)
             typemapper[new_cls] = (new_cls.model, new_cls.is_anonymous)
         else:
             typemapper[new_cls] = (None, new_cls.is_anonymous)
-        
+
         if name not in ('BaseHandler', 'AnonymousBaseHandler'):
             handler_tracker.append(new_cls)
 
         if name not in ('BaseHandler', 'AnonymousBaseHandler'):
             handler_tracker.append(new_cls)
 
@@ -40,43 +40,43 @@ class BaseHandler(object):
     Basehandler that gives you CRUD for free.
     You are supposed to subclass this for specific
     functionality.
     Basehandler that gives you CRUD for free.
     You are supposed to subclass this for specific
     functionality.
-    
+
     All CRUD methods (`read`/`update`/`create`/`delete`)
     receive a request as the first argument from the
     resource. Use this for checking `request.user`, etc.
     """
     __metaclass__ = HandlerMetaClass
     All CRUD methods (`read`/`update`/`create`/`delete`)
     receive a request as the first argument from the
     resource. Use this for checking `request.user`, etc.
     """
     __metaclass__ = HandlerMetaClass
-    
+
     allowed_methods = ('GET', 'POST', 'PUT', 'DELETE')
     anonymous = is_anonymous = False
     exclude = ( 'id', )
     fields =  ( )
     allowed_methods = ('GET', 'POST', 'PUT', 'DELETE')
     anonymous = is_anonymous = False
     exclude = ( 'id', )
     fields =  ( )
-    
+
     def flatten_dict(self, dct):
         return dict([ (str(k), dct.get(k)) for k in dct.keys() ])
     def flatten_dict(self, dct):
         return dict([ (str(k), dct.get(k)) for k in dct.keys() ])
-    
+
     def has_model(self):
         return hasattr(self, 'model') or hasattr(self, 'queryset')
 
     def queryset(self, request):
         return self.model.objects.all()
     def has_model(self):
         return hasattr(self, 'model') or hasattr(self, 'queryset')
 
     def queryset(self, request):
         return self.model.objects.all()
-    
+
     def value_from_tuple(tu, name):
         for int_, n in tu:
             if n == name:
                 return int_
         return None
     def value_from_tuple(tu, name):
         for int_, n in tu:
             if n == name:
                 return int_
         return None
-    
+
     def exists(self, **kwargs):
         if not self.has_model():
             raise NotImplementedError
     def exists(self, **kwargs):
         if not self.has_model():
             raise NotImplementedError
-        
+
         try:
             self.model.objects.get(**kwargs)
             return True
         except self.model.DoesNotExist:
             return False
         try:
             self.model.objects.get(**kwargs)
             return True
         except self.model.DoesNotExist:
             return False
-    
+
     def read(self, request, *args, **kwargs):
         if not self.has_model():
             return rc.NOT_IMPLEMENTED
     def read(self, request, *args, **kwargs):
         if not self.has_model():
             return rc.NOT_IMPLEMENTED
@@ -92,13 +92,13 @@ class BaseHandler(object):
                 return rc.BAD_REQUEST
         else:
             return self.queryset(request).filter(*args, **kwargs)
                 return rc.BAD_REQUEST
         else:
             return self.queryset(request).filter(*args, **kwargs)
-    
+
     def create(self, request, *args, **kwargs):
         if not self.has_model():
             return rc.NOT_IMPLEMENTED
     def create(self, request, *args, **kwargs):
         if not self.has_model():
             return rc.NOT_IMPLEMENTED
-        
+
         attrs = self.flatten_dict(request.POST)
         attrs = self.flatten_dict(request.POST)
-        
+
         try:
             inst = self.queryset(request).get(**attrs)
             return rc.DUPLICATE_ENTRY
         try:
             inst = self.queryset(request).get(**attrs)
             return rc.DUPLICATE_ENTRY
@@ -108,7 +108,7 @@ class BaseHandler(object):
             return inst
         except self.model.MultipleObjectsReturned:
             return rc.DUPLICATE_ENTRY
             return inst
         except self.model.MultipleObjectsReturned:
             return rc.DUPLICATE_ENTRY
-    
+
     def update(self, request, *args, **kwargs):
         if not self.has_model():
             return rc.NOT_IMPLEMENTED
     def update(self, request, *args, **kwargs):
         if not self.has_model():
             return rc.NOT_IMPLEMENTED
@@ -132,7 +132,7 @@ class BaseHandler(object):
 
         inst.save()
         return rc.ALL_OK
 
         inst.save()
         return rc.ALL_OK
-    
+
     def delete(self, request, *args, **kwargs):
         if not self.has_model():
             raise NotImplementedError
     def delete(self, request, *args, **kwargs):
         if not self.has_model():
             raise NotImplementedError
@@ -147,7 +147,7 @@ class BaseHandler(object):
             return rc.DUPLICATE_ENTRY
         except self.model.DoesNotExist:
             return rc.NOT_HERE
             return rc.DUPLICATE_ENTRY
         except self.model.DoesNotExist:
             return rc.NOT_HERE
-        
+
 class AnonymousBaseHandler(BaseHandler):
     """
     Anonymous handler.
 class AnonymousBaseHandler(BaseHandler):
     """
     Anonymous handler.