Commits

Thomas Waldmann committed f0b346a

added return_rev param to store_revision() to avoid returning a revision object without need

Comments (0)

Files changed (6)

MoinMoin/_tests/__init__.py

         meta[NAME] = name
     if CONTENTTYPE not in meta:
         meta[CONTENTTYPE] = u'application/octet-stream'
-    rev = item.store_revision(meta, StringIO(data))
+    rev = item.store_revision(meta, StringIO(data), return_rev=True)
     return rev
 
 def create_random_string_list(length=14, count=10):

MoinMoin/items/__init__.py

                                              action=unicode(action),
                                              contenttype_current=contenttype_current,
                                              contenttype_guessed=contenttype_guessed,
+                                             return_rev=True,
                                              )
         item_modified.send(app._get_current_object(), item_name=name)
         return newrev.revid, newrev.meta[SIZE]

MoinMoin/storage/middleware/_tests/test_indexing.py

         item_name = u'foo'
         data = 'bar'
         item = self.imw[item_name]
-        rev = item.store_revision(dict(name=item_name), StringIO(data))
+        rev = item.store_revision(dict(name=item_name), StringIO(data),
+                                  return_rev=True)
         revid = rev.revid
         # check if we have the revision now:
         item = self.imw[item_name]
         data = 'bar'
         newdata = 'baz'
         item = self.imw[item_name]
-        rev = item.store_revision(dict(name=item_name, comment=u'spam'), StringIO(data))
+        rev = item.store_revision(dict(name=item_name, comment=u'spam'), StringIO(data),
+                                  return_rev=True)
         revid = rev.revid
         # clear revision:
         item.store_revision(dict(name=item_name, revid=revid, comment=u'no spam'), StringIO(newdata), overwrite=True)
         item_name = u'foo'
         item = self.imw[item_name]
         rev = item.store_revision(dict(name=item_name, mtime=1),
-                                  StringIO('bar'), trusted=True)
+                                  StringIO('bar'), trusted=True, return_rev=True)
         revid0 = rev.revid
         rev = item.store_revision(dict(name=item_name, mtime=2),
-                                  StringIO('baz'), trusted=True)
+                                  StringIO('baz'), trusted=True, return_rev=True)
         revid1 = rev.revid
         rev = item.store_revision(dict(name=item_name, mtime=3),
-                                  StringIO('...'), trusted=True)
+                                  StringIO('...'), trusted=True, return_rev=True)
         revid2 = rev.revid
         print "revids:", revid0, revid1, revid2
         # destroy a non-current revision:
         item_name = u'foo'
         item = self.imw[item_name]
         rev = item.store_revision(dict(name=item_name, mtime=1),
-                                  StringIO('bar'), trusted=True)
+                                  StringIO('bar'), trusted=True, return_rev=True)
         revids.append(rev.revid)
         rev = item.store_revision(dict(name=item_name, mtime=2),
-                                  StringIO('baz'), trusted=True)
+                                  StringIO('baz'), trusted=True, return_rev=True)
         revids.append(rev.revid)
         # destroy item:
         item.destroy_all_revisions()
         item_name = u'bar'
         item = self.imw[item_name]
         item.store_revision(dict(name=item_name), StringIO('1st'))
-        expected_rev = item.store_revision(dict(name=item_name), StringIO('2nd'))
+        expected_rev = item.store_revision(dict(name=item_name), StringIO('2nd'),
+                                           return_rev=True)
         revs = list(self.imw.documents(name=item_name))
         assert len(revs) == 1  # there is only 1 latest revision
         assert expected_rev.revid == revs[0].revid  # it is really the latest one
         item_name = u'foo'
         data = 'bar'
         item = self.imw[item_name]
-        rev = item.store_revision(dict(name=item_name), StringIO(data))
+        rev = item.store_revision(dict(name=item_name), StringIO(data), return_rev=True)
         print repr(rev.meta)
         assert rev.meta[NAME] == item_name
         assert rev.meta[SIZE] == len(data)
     def test_documents(self):
         item_name = u'foo'
         item = self.imw[item_name]
-        rev1 = item.store_revision(dict(name=item_name), StringIO('x'))
-        rev2 = item.store_revision(dict(name=item_name), StringIO('xx'))
-        rev3 = item.store_revision(dict(name=item_name), StringIO('xxx'))
+        rev1 = item.store_revision(dict(name=item_name), StringIO('x'), return_rev=True)
+        rev2 = item.store_revision(dict(name=item_name), StringIO('xx'), return_rev=True)
+        rev3 = item.store_revision(dict(name=item_name), StringIO('xxx'), return_rev=True)
         rev = self.imw.document(idx_name=ALL_REVS, size=2)
         assert rev
         assert rev.revid == rev2.revid
         item_name = u'foo'
         item = self.imw[item_name]
         r = item.store_revision(dict(name=item_name, mtime=1),
-                                StringIO('does not count, different name'), trusted=True)
+                                StringIO('does not count, different name'),
+                                trusted=True, return_rev=True)
         expected_latest_revids.append(r.revid)
         item_name = u'bar'
         item = self.imw[item_name]
         item.store_revision(dict(name=item_name, mtime=1),
                             StringIO('1st'), trusted=True)
         r = item.store_revision(dict(name=item_name, mtime=2),
-                                StringIO('2nd'), trusted=True)
+                                StringIO('2nd'), trusted=True, return_rev=True)
         expected_latest_revids.append(r.revid)
 
         # now we remember the index contents built that way:
         item_name = u'updated'
         item = self.imw[item_name]
         r = item.store_revision(dict(name=item_name, mtime=1),
-                                StringIO('updated 1st'), trusted=True)
+                                StringIO('updated 1st'),
+                                trusted=True, return_rev=True)
         expected_all_revids.append(r.revid)
         # we update this item below, so we don't add it to expected_latest_revids
         item_name = u'destroyed'
         item = self.imw[item_name]
         r = item.store_revision(dict(name=item_name, mtime=1),
-                                StringIO('destroyed 1st'), trusted=True)
+                                StringIO('destroyed 1st'),
+                                trusted=True, return_rev=True)
         destroy_revid = r.revid
         # we destroy this item below, so we don't add it to expected_all_revids
         # we destroy this item below, so we don't add it to expected_latest_revids
         item_name = u'stayssame'
         item = self.imw[item_name]
         r = item.store_revision(dict(name=item_name, mtime=1),
-                                StringIO('stayssame 1st'), trusted=True)
+                                StringIO('stayssame 1st'),
+                                trusted=True, return_rev=True)
         expected_all_revids.append(r.revid)
         # we update this item below, so we don't add it to expected_latest_revids
         r = item.store_revision(dict(name=item_name, mtime=2),
-                                StringIO('stayssame 2nd'), trusted=True)
+                                StringIO('stayssame 2nd'),
+                                trusted=True, return_rev=True)
         expected_all_revids.append(r.revid)
         expected_latest_revids.append(r.revid)
 
         item_name = u'updated'
         item = self.imw[item_name]
         r = item.store_revision(dict(name=item_name, mtime=2),
-                                StringIO('updated 2nd'), trusted=True)
+                                StringIO('updated 2nd'), trusted=True,
+                                return_rev=True)
         expected_all_revids.append(r.revid)
         expected_latest_revids.append(r.revid)
         missing_revids.append(r.revid)
         item_name = u'added'
         item = self.imw[item_name]
         r = item.store_revision(dict(name=item_name, mtime=1),
-                                StringIO('added 1st'), trusted=True)
+                                StringIO('added 1st'),
+                                trusted=True, return_rev=True)
         expected_all_revids.append(r.revid)
         expected_latest_revids.append(r.revid)
         missing_revids.append(r.revid)
         data = 'some test content'
         item = self.imw[item_name]
         data_file = StringIO(data)
-        with item.store_revision(meta, data_file) as rev:
+        with item.store_revision(meta, data_file, return_rev=True) as rev:
             assert rev.data.read() == data
             revid = rev.revid
         with pytest.raises(ValueError):
         data = 'some test content\n'
         item = self.imw[item_name]
         data_file = StringIO(data)
-        with item.store_revision(meta, data_file) as rev:
+        with item.store_revision(meta, data_file, return_rev=True) as rev:
             expected_revid = rev.revid
         doc = self.imw._document(content=u'test')
         assert doc is not None
     def test_documents(self):
         item_name = u'public'
         item = self.imw[item_name]
-        r = item.store_revision(dict(name=item_name, acl=u'joe:read'), StringIO('public content'))
+        r = item.store_revision(dict(name=item_name, acl=u'joe:read'),
+                                StringIO('public content'), return_rev=True)
         revid_public = r.revid
         revids = [rev.revid for rev in self.imw.documents()
                   if rev.meta[NAME] != u'joe'] # the user profile is a revision in the backend
     def test_getitem(self):
         item_name = u'public'
         item = self.imw[item_name]
-        r = item.store_revision(dict(name=item_name, acl=u'joe:read'), StringIO('public content'))
+        r = item.store_revision(dict(name=item_name, acl=u'joe:read'),
+                                StringIO('public content'), return_rev=True)
         revid_public = r.revid
         # now testing:
         item_name = u'public'

MoinMoin/storage/middleware/_tests/test_protecting.py

         revids = []
         for item_name, acl, content in items:
             item = self.imw[item_name]
-            r = item.store_revision(dict(name=item_name, acl=acl), StringIO(content))
+            r = item.store_revision(dict(name=item_name, acl=acl),
+                                    StringIO(content), return_rev=True)
             revids.append(r.revid)
         return revids
 

MoinMoin/storage/middleware/indexing.py

                        contenttype_current=None,
                        contenttype_guessed=None,
                        acl_parent=None,
+                       return_rev=False,
                        ):
         """
         Store a revision into the backend, write metadata and data to it.
         :type meta: dict
         :type data: open file (file must be closed by caller)
         :param overwrite: if True, allow overwriting of existing revs.
-        :returns: a Revision instance of the just created revision
+        :param return_rev: if True, return a Revision instance of the just created revision
+        :returns: a Revision instance or None
         """
         if remote_addr is None:
             try:
         self.indexer.index_revision(meta, content)
         if not overwrite:
             self._current = self.indexer._document(revid=revid)
-        return Revision(self, revid)
+        if return_rev:
+            return Revision(self, revid)
 
     def store_all_revisions(self, meta, data):
         """

MoinMoin/storage/middleware/protecting.py

     def get_revision(self, revid):
         return self[revid]
 
-    def store_revision(self, meta, data, overwrite=False, **kw):
+    def store_revision(self, meta, data, overwrite=False, return_rev=False, **kw):
         self.require(WRITE)
         if not self:
             self.require(CREATE)
         if overwrite:
             self.require(DESTROY)
-        rev = self.item.store_revision(meta, data, overwrite=overwrite, **kw)
-        return ProtectedRevision(self.protector, rev, p_item=self)
+        rev = self.item.store_revision(meta, data, overwrite=overwrite, return_rev=return_rev, **kw)
+        if return_rev:
+            return ProtectedRevision(self.protector, rev, p_item=self)
 
     def store_all_revisions(self, meta, data):
         self.require(DESTROY)