2 --------------------------------- |
2 --------------------------------- |
3 |
3 |
4 Step 1: configuring the BytesFileSystem storage |
4 Step 1: configuring the BytesFileSystem storage |
5 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
5 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
6 |
6 |
7 To avoid cluttering my database, and to ease file manipulation, I don't want |
7 To avoid cluttering my database, and to ease file manipulation, I don't want them |
8 them to be stored in the database. I want to be able create File/Image entities |
8 to be stored in the database. I want to be able create File entities for some |
9 for some files on the server file system, where those file will be accessed to |
9 files on the server file system, where those file will be accessed to get |
10 get entities data. To do so I've to set a custom :class:`BytesFileSystemStorage` storage |
10 entities data. To do so I've to set a custom :class:`BytesFileSystemStorage` |
11 for the File/Image 'data' attribute, which hold the actual file's content. |
11 storage for the File 'data' attribute, which hold the actual file's content. |
12 |
12 |
13 Since the function to register a custom storage needs to have a repository |
13 Since the function to register a custom storage needs to have a repository |
14 instance as first argument, we've to call it in a server startup hook. So I added |
14 instance as first argument, we've to call it in a server startup hook. So I added |
15 in `cubes/sytweb/hooks.py` : |
15 in `cubes/sytweb/hooks.py` : |
16 |
16 |
31 if not exists(bfssdir): |
31 if not exists(bfssdir): |
32 makedirs(bfssdir) |
32 makedirs(bfssdir) |
33 print 'created', bfssdir |
33 print 'created', bfssdir |
34 storage = storages.BytesFileSystemStorage(bfssdir) |
34 storage = storages.BytesFileSystemStorage(bfssdir) |
35 set_attribute_storage(self.repo, 'File', 'data', storage) |
35 set_attribute_storage(self.repo, 'File', 'data', storage) |
36 set_attribute_storage(self.repo, 'Image', 'data', storage) |
|
37 |
36 |
38 .. Note:: |
37 .. Note:: |
39 |
38 |
40 * how we built the hook's registry identifier (_`_regid__`): you can introduce |
39 * how we built the hook's registry identifier (_`_regid__`): you can introduce |
41 'namespaces' by using there python module like naming identifiers. This is |
40 'namespaces' by using there python module like naming identifiers. This is |
50 |
49 |
51 * the path given to the storage is the place where file added through the ui |
50 * the path given to the storage is the place where file added through the ui |
52 (or in the database before migration) will be located |
51 (or in the database before migration) will be located |
53 |
52 |
54 * be ware that by doing this, you can't anymore write queries that will try to |
53 * be ware that by doing this, you can't anymore write queries that will try to |
55 restrict on File and Image `data` attribute. Hopefuly we don't do that usually |
54 restrict on File `data` attribute. Hopefuly we don't do that usually |
56 on file's content or more generally on attributes for the Bytes type |
55 on file's content or more generally on attributes for the Bytes type |
57 |
56 |
58 Now, if you've already added some photos through the web ui, you'll have to |
57 Now, if you've already added some photos through the web ui, you'll have to |
59 migrate existing data so file's content will be stored on the file-system instead |
58 migrate existing data so file's content will be stored on the file-system instead |
60 of the database. There is a migration command to do so, let's run it in the |
59 of the database. There is a migration command to do so, let's run it in the |
66 $ cubicweb-ctl shell sytweb |
65 $ cubicweb-ctl shell sytweb |
67 entering the migration python shell |
66 entering the migration python shell |
68 just type migration commands or arbitrary python code and type ENTER to execute it |
67 just type migration commands or arbitrary python code and type ENTER to execute it |
69 type "exit" or Ctrl-D to quit the shell and resume operation |
68 type "exit" or Ctrl-D to quit the shell and resume operation |
70 >>> storage_changed('File', 'data') |
69 >>> storage_changed('File', 'data') |
71 [........................] |
|
72 >>> storage_changed('Image', 'data') |
|
73 [........................] |
70 [........................] |
74 |
71 |
75 |
72 |
76 That's it. Now, file added through the web ui will have their content stored on |
73 That's it. Now, file added through the web ui will have their content stored on |
77 the file-system, and you'll also be able to import files from the file-system as |
74 the file-system, and you'll also be able to import files from the file-system as |