1 | waeup.sirp.image fields |
---|
2 | ================ |
---|
3 | |
---|
4 | The 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 | |
---|
18 | We 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 | |
---|
35 | The WAeUPImageFile object normally stores the file data using ZODB |
---|
36 | persistence. Files can however also be stored by tramline. If |
---|
37 | tramline is installed in Apache, the Tramline takes care of generating |
---|
38 | ids for files and storing the file on the filesystem directly. The ids |
---|
39 | are then passed as file data to be stored in the ZODB. |
---|
40 | |
---|
41 | Let's first enable tramline. |
---|
42 | |
---|
43 | The tramline directory structure is a directory with two subdirectories, |
---|
44 | one 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 | |
---|
53 | We create a TramlineFileRetrieval object knowing about this directory, |
---|
54 | and 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 | |
---|
63 | Now 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 | |
---|
69 | The file with underlying name '1' (the data stored in the ZODB will be |
---|
70 | just '1') will now be created:: |
---|
71 | |
---|
72 | >>> file = WAeUPImageFile('foo.jpg', '1') |
---|
73 | |
---|
74 | The data is now '1', referring to the real file:: |
---|
75 | |
---|
76 | >>> file.data |
---|
77 | '1' |
---|
78 | |
---|
79 | Retrieving the file results in the real file:: |
---|
80 | |
---|
81 | >>> f = file.file |
---|
82 | >>> f.read() |
---|
83 | 'test data' |
---|
84 | |
---|
85 | We can also retrieve its size:: |
---|
86 | |
---|
87 | >>> file.size |
---|
88 | 9L |
---|
89 | |
---|
90 | Now 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 | |
---|
99 | We 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 | |
---|
108 | Clean up:: |
---|
109 | |
---|
110 | >>> import shutil |
---|
111 | >>> shutil.rmtree(dirpath) |
---|