You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@chemistry.apache.org by jp...@apache.org on 2011/06/01 00:31:19 UTC
svn commit: r1129945 - in /chemistry/cmislib/trunk/src: cmislib/model.py
tests/cmislibtest.py
Author: jpotts
Date: Tue May 31 22:31:19 2011
New Revision: 1129945
URL: http://svn.apache.org/viewvc?rev=1129945&view=rev
Log:
Resolving CMIS-230 by adding createDocumentFromString convenience methods to both Repository and Folder in cmislib.
Modified:
chemistry/cmislib/trunk/src/cmislib/model.py
chemistry/cmislib/trunk/src/tests/cmislibtest.py
Modified: chemistry/cmislib/trunk/src/cmislib/model.py
URL: http://svn.apache.org/viewvc/chemistry/cmislib/trunk/src/cmislib/model.py?rev=1129945&r1=1129944&r2=1129945&view=diff
==============================================================================
--- chemistry/cmislib/trunk/src/cmislib/model.py (original)
+++ chemistry/cmislib/trunk/src/cmislib/model.py Tue May 31 22:31:19 2011
@@ -35,6 +35,7 @@ from xml.parsers.expat import ExpatError
import datetime
import time
import iso8601
+import StringIO
# would kind of like to not have any parsing logic in this module,
# but for now I'm going to put the serial/deserialization in methods
@@ -1088,6 +1089,40 @@ class Repository(object):
# return the result set
return ChangeEntryResultSet(self._cmisClient, self, result)
+ def createDocumentFromString(self,
+ name,
+ properties={},
+ parentFolder=None,
+ contentString=None,
+ contentType=None,
+ contentEncoding=None):
+
+ """
+ Creates a new document setting the content to the string provided. If
+ the repository supports unfiled objects, you do not have to pass in
+ a parent :class:`Folder` otherwise it is required.
+
+ This method is essentially a convenience method that wraps your string
+ with a StringIO and then calls createDocument.
+
+ >>> repo.createDocumentFromString('testdoc5', parentFolder=testFolder, contentString='Hello, World!', contentType='text/plain')
+ <cmislib.model.Document object at 0x101352ed0>
+ """
+
+ # if you didn't pass in a parent folder
+ if parentFolder == None:
+ # if the repository doesn't require fileable objects to be filed
+ if self.getCapabilities()['Unfiling']:
+ # has not been implemented
+ #postUrl = self.getCollectionLink(UNFILED_COLL)
+ raise NotImplementedError
+ else:
+ # this repo requires fileable objects to be filed
+ raise InvalidArgumentException
+
+ return parentFolder.createDocument(name, properties, StringIO.StringIO(contentString),
+ contentType, contentEncoding)
+
def createDocument(self,
name,
properties={},
@@ -2593,6 +2628,27 @@ class Folder(CmisObject):
# so use it to instantiate a new folder then return it
return Folder(self._cmisClient, self._repository, xmlDoc=result)
+ def createDocumentFromString(self,
+ name,
+ properties={},
+ contentString=None,
+ contentType=None,
+ contentEncoding=None):
+
+ """
+ Creates a new document setting the content to the string provided. If
+ the repository supports unfiled objects, you do not have to pass in
+ a parent :class:`Folder` otherwise it is required.
+
+ This method is essentially a convenience method that wraps your string
+ with a StringIO and then calls createDocument.
+
+ >>> testFolder.createDocumentFromString('testdoc3', contentString='hello, world', contentType='text/plain')
+ """
+
+ return self._repository.createDocumentFromString(name, properties,
+ self, contentString, contentType, contentEncoding)
+
def createDocument(self, name, properties={}, contentFile=None,
contentType=None, contentEncoding=None):
Modified: chemistry/cmislib/trunk/src/tests/cmislibtest.py
URL: http://svn.apache.org/viewvc/chemistry/cmislib/trunk/src/tests/cmislibtest.py?rev=1129945&r1=1129944&r2=1129945&view=diff
==============================================================================
--- chemistry/cmislib/trunk/src/tests/cmislibtest.py (original)
+++ chemistry/cmislib/trunk/src/tests/cmislibtest.py Tue May 31 22:31:19 2011
@@ -254,6 +254,17 @@ class RepositoryTest(CmisTestBase):
newDoc = self._repo.createDocument(documentName, parentFolder=self._testFolder)
self.assertEquals(documentName, newDoc.getName())
+ def testCreateDocumentFromString(self):
+ '''Create a new document from a string'''
+ documentName = 'testDocument'
+ contentString = 'Test content string'
+ newDoc = self._repo.createDocumentFromString(documentName,
+ parentFolder=self._testFolder,
+ contentString=contentString,
+ contentType='text/plain')
+ self.assertEquals(documentName, newDoc.getName())
+ self.assertEquals(newDoc.getContentStream().read(), contentString)
+
# CMIS-279
def testCreateDocumentUnicode(self):
'''Create a new doc with unicode characters in the name'''
@@ -1087,6 +1098,15 @@ class DocumentTest(CmisTestBase):
# cleanup
os.remove(exportFilename)
+ def testCreateDocumentFromString(self):
+ '''Create a new document from a string'''
+ documentName = 'testDocument'
+ contentString = 'Test content string'
+ newDoc = self._testFolder.createDocumentFromString(documentName,
+ contentString=contentString, contentType='text/plain')
+ self.assertEquals(documentName, newDoc.getName())
+ self.assertEquals(newDoc.getContentStream().read(), contentString)
+
def testCreateDocumentPlain(self):
'''Create a plain document using a file from the file system'''
testFilename = 'plain.txt'
@@ -1340,7 +1360,8 @@ if __name__ == "__main__":
tts.addTests(TestLoader().loadTestsFromTestCase(ACLTest))
tts.addTests(TestLoader().loadTestsFromTestCase(ChangeEntryTest))
-# tts.addTests(TestLoader().loadTestsFromName('testCreateDocument', RepositoryTest))
+# tts.addTests(TestLoader().loadTestsFromName('testCreateDocumentFromString', RepositoryTest))
+# tts.addTests(TestLoader().loadTestsFromName('testCreateDocumentFromString', DocumentTest))
# tts.addTests(TestLoader().loadTestsFromName('testMoveDocument', RepositoryTest))
# tts.addTests(TestLoader().loadTestsFromName('testCreateDocumentBinary', DocumentTest))
# tts.addTests(TestLoader().loadTestsFromName('testCreateDocumentPlain', DocumentTest))