1. Roberto De Almeida
  2. JSONStore

Commits

Roberto De Almeida  committed 5fa6cd2

Added and initial support for PATCH, by reusing PUT.

  • Participants
  • Parent commits 94f17f0
  • Branches default

Comments (0)

Files changed (1)

File jsonstore/rest.py

View file
                 content_type='application/json',
                 charset='utf8',
                 headerlist=[('etag', etag)])
-        
+
+    def PATCH(self, req):
+        """
+        PATCH an entry, replacing only part of the document.
+
+        We simple load the full entry, apply the patch and PUT it.
+
+        """
+        patch = load_entry(req.body)
+        id_ = req.path_info.lstrip('/')
+
+        if '__id__' in patch and patch['__id__'] != id_:
+            return Response(status='409 Conflict')
+
+        old = self.em.search(__id__=id_)[0]
+        new = old.copy()
+
+        def replace(entry, patch):
+            for k, v in patch.items():
+                if isinstance(v, dict):
+                    replace(entry[k], v)
+                else:
+                    entry[k] = v
+        replace(new, patch)
+        req.body = dumps(new, cls=DatetimeEncoder)
+        return self.PUT(req)
+
     def DELETE(self, req):
         """
         DELETE a single entry.