changeset 228:77029ecf9817

added auto-create support
author Goffi <goffi@goffi.org>
date Thu, 10 May 2012 22:41:34 +0200
parents 8540825f85e0
children f23fcdc19f7f
files idavoll/backend.py
diffstat 1 files changed, 19 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/idavoll/backend.py	Thu May 17 00:31:36 2012 +0200
+++ b/idavoll/backend.py	Thu May 10 22:41:34 2012 +0200
@@ -288,6 +288,8 @@
     def getSubscriptions(self, entity):
         return self.storage.getSubscriptions(entity)
 
+    def supportsAutoCreate(self):
+        return True
 
     def supportsInstantNodes(self):
         return True
@@ -536,6 +538,9 @@
         self.backend.registerNotifier(self._notify)
         self.backend.registerPreDelete(self._preDelete)
 
+        if self.backend.supportsAutoCreate():
+            self.features.append("auto-create")
+
         if self.backend.supportsInstantNodes():
             self.features.append("instant-nodes")
 
@@ -625,11 +630,25 @@
     def getConfigurationOptions(self):
         return self.backend.nodeOptions
 
+    def _publish_errb(self, failure, request):
+        if failure.type == error.NodeNotFound and self.backend.supportsAutoCreate():
+            d = self.backend.createNode(request.nodeIdentifier,
+                                        request.sender)
+            d.addCallback(lambda ignore,
+                                 request: self.backend.publish(request.nodeIdentifier,
+                                                               request.items,
+                                                               request.sender),
+                          request)
+            return d
+
+        raise failure
+
 
     def publish(self, request):
         d = self.backend.publish(request.nodeIdentifier,
                                  request.items,
                                  request.sender)
+        d.addErrback(self._publish_errb, request)
         return d.addErrback(self._mapErrors)