source: main/waeup.sirp/trunk/src/waeup/sirp/image/README.txt @ 6018

Last change on this file since 6018 was 5997, checked in by uli, 14 years ago

Fix test failure.

File size: 2.9 KB
Line 
1waeup.sirp.image fields
2================
3
4The image file widget is built on top of the ImageFile object::
5
6  >>> from waeup.sirp.image import WAeUPImageFile
7  >>> file = WAeUPImageFile('foo.jpg', 'mydata')
8  >>> file.filename
9  'foo.jpg'
10  >>> file.data
11  'mydata'
12  >>> file.size
13  6
14  >>> f = file.file
15  >>> f.read()
16  'mydata'
17
18We can also create WAeUPImageFile objects from file-like objects::
19
20  >>> from StringIO import StringIO
21  >>> from zope import component
22  >>> from hurry.file.interfaces import IFileRetrieval
23  >>> fileretrieval = component.getUtility(IFileRetrieval)
24  >>> file = fileretrieval.createFile('bar.jpg', StringIO('test data'))
25  >>> file.filename
26  'bar.jpg'
27  >>> file.size
28  9
29  >>> file.data
30  'test data'
31  >>> f = file.file
32  >>> f.read()
33  'test data'
34
35The WAeUPImageFile object normally stores the file data using ZODB
36persistence. Files can however also be stored by tramline.  If
37tramline is installed in Apache, the Tramline takes care of generating
38ids for files and storing the file on the filesystem directly. The ids
39are then passed as file data to be stored in the ZODB.
40
41Let's first enable tramline.
42
43The tramline directory structure is a directory with two subdirectories,
44one called 'repository' and the other called 'upload'::
45
46  >>> import tempfile, os
47  >>> dirpath = tempfile.mkdtemp()
48  >>> repositorypath = os.path.join(dirpath, 'repository')
49  >>> uploadpath = os.path.join(dirpath, 'upload')
50  >>> os.mkdir(repositorypath)
51  >>> os.mkdir(uploadpath)
52
53We create a TramlineFileRetrieval object knowing about this directory,
54and register it as a utility::
55
56  >>> from hurry.file.file import TramlineFileRetrievalBase
57  >>> class TramlineFileRetrieval(TramlineFileRetrievalBase):
58  ...    def getTramlinePath(self):
59  ...        return dirpath
60  >>> retrieval = TramlineFileRetrieval()
61  >>> component.provideUtility(retrieval, IFileRetrieval)
62
63Now let's store a file the way tramline would during upload::
64
65  >>> f = open(os.path.join(repositorypath, '1'), 'wb')
66  >>> f.write('test data')
67  >>> f.close()
68
69The file with underlying name '1' (the data stored in the ZODB will be
70just '1') will now be created::
71
72  >>> file = WAeUPImageFile('foo.jpg', '1')
73
74The data is now '1', referring to the real file::
75
76  >>> file.data
77  '1'
78
79Retrieving the file results in the real file::
80 
81  >>> f = file.file
82  >>> f.read()
83  'test data'
84
85We can also retrieve its size::
86
87  >>> file.size
88  9L
89
90Now let's disable tramline in our utility::
91
92  >>> class TramlineFileRetrieval(TramlineFileRetrievalBase):
93  ...     def getTramlinePath(self):
94  ...        return dirpath
95  ...     def isTramlineEnabled(self):
96  ...        return False
97  >>> component.provideUtility(TramlineFileRetrieval(), IFileRetrieval)
98
99We expect the same behavior as when tramline is not installed::
100
101  >>> file = WAeUPImageFile('foo.jpg', 'data')
102  >>> f = file.file
103  >>> f.read()
104  'data'
105  >>> file.size
106  4
107
108Clean up::
109
110  >>> import shutil
111  >>> shutil.rmtree(dirpath)
Note: See TracBrowser for help on using the repository browser.