Commits

Jesper Nøhr  committed 3f25b46

disabling update in basehandler, is not smart enough yet. enabling delete.

  • Participants
  • Parent commits d1be4f5

Comments (0)

Files changed (1)

File piston/handler.py

+from piston.utils import rc
+
 typemapper = { }
 
 class HandlerMetaClass(type):
     
     def read(self, request, *args, **kwargs):
         if not self.has_model():
-            raise NotImplementedError
+            return rc.NOT_IMPLEMENTED
         
         return self.model.objects.filter(*args, **kwargs)
     
     def create(self, request, *args, **kwargs):
         if not self.has_model():
-            raise NotImplementedError
+            return rc.NOT_IMPLEMENTED
         
         attrs = self.flatten_dict(request.POST)
         
         try:
             inst = self.model.objects.get(**attrs)
-            raise ValueError("Already exists.")
+            return rc.DUPLICATE_ENTRY
         except self.model.DoesNotExist:
             inst = self.model(attrs)
             inst.save()
             return inst
     
     def update(self, request, *args, **kwargs):
-        if not self.has_model():
-            raise NotImplementedError
-        
-        inst = self.model.objects.get(*args, **kwargs)
-        print "must update instance", inst, "with", request.PUT
-        
-        return "I can't do this yet."
+        # TODO: This doesn't work automatically yet.
+        return rc.NOT_IMPLEMENTED
     
     def delete(self, request, *args, **kwargs):
         if not self.has_model():
             raise NotImplementedError
+
+        try:
+            inst = self.model.objects.get(*args, **kwargs)
+
+            inst.delete()
+
+            return rc.DELETED
+        except self.model.MultipleObjectsReturned:
+            return rc.DUPLICATE_ENTRY
+        except self.model.DoesNotExist:
+            return rc.NOT_HERE
         
-        return "I can't do this yet."
-
 class AnonymousBaseHandler(BaseHandler):
     """
     Anonymous handler.