171 if oldpath != fpath: |
171 if oldpath != fpath: |
172 # register the new location for the file. |
172 # register the new location for the file. |
173 entity.cw_edited.edited_attribute(attr, Binary(fpath)) |
173 entity.cw_edited.edited_attribute(attr, Binary(fpath)) |
174 # Mark the old file as useless so the file will be removed at |
174 # Mark the old file as useless so the file will be removed at |
175 # commit. |
175 # commit. |
176 DeleteFileOp.get_instance(entity._cw).add_data(oldpath) |
176 if oldpath is not None: |
|
177 DeleteFileOp.get_instance(entity._cw).add_data(oldpath) |
177 return binary |
178 return binary |
178 |
179 |
179 def entity_deleted(self, entity, attr): |
180 def entity_deleted(self, entity, attr): |
180 """an entity using this storage for attr has been deleted""" |
181 """an entity using this storage for attr has been deleted""" |
181 fpath = self.current_fs_path(entity, attr) |
182 fpath = self.current_fs_path(entity, attr) |
182 DeleteFileOp.get_instance(entity._cw).add_data(fpath) |
183 if fpath is not None: |
|
184 DeleteFileOp.get_instance(entity._cw).add_data(fpath) |
183 |
185 |
184 def new_fs_path(self, entity, attr): |
186 def new_fs_path(self, entity, attr): |
185 # We try to get some hint about how to name the file using attribute's |
187 # We try to get some hint about how to name the file using attribute's |
186 # name metadata, so we use the real file name and extension when |
188 # name metadata, so we use the real file name and extension when |
187 # available. Keeping the extension is useful for example in the case of |
189 # available. Keeping the extension is useful for example in the case of |
197 self.default_directory, '_'.join(basename)) |
199 self.default_directory, '_'.join(basename)) |
198 raise ValidationError(entity.eid, {role_name(attr, 'subject'): msg}) |
200 raise ValidationError(entity.eid, {role_name(attr, 'subject'): msg}) |
199 return fspath |
201 return fspath |
200 |
202 |
201 def current_fs_path(self, entity, attr): |
203 def current_fs_path(self, entity, attr): |
|
204 """return the current fs_path of the tribute. |
|
205 |
|
206 Return None is the attr is not stored yet.""" |
202 sysource = entity._cw.pool.source('system') |
207 sysource = entity._cw.pool.source('system') |
203 cu = sysource.doexec(entity._cw, |
208 cu = sysource.doexec(entity._cw, |
204 'SELECT cw_%s FROM cw_%s WHERE cw_eid=%s' % ( |
209 'SELECT cw_%s FROM cw_%s WHERE cw_eid=%s' % ( |
205 attr, entity.__regid__, entity.eid)) |
210 attr, entity.__regid__, entity.eid)) |
206 rawvalue = cu.fetchone()[0] |
211 rawvalue = cu.fetchone()[0] |
207 if rawvalue is None: # no previous value |
212 if rawvalue is None: # no previous value |
208 return self.new_fs_path(entity, attr) |
213 return None |
209 return sysource._process_value(rawvalue, cu.description[0], |
214 return sysource._process_value(rawvalue, cu.description[0], |
210 binarywrap=str) |
215 binarywrap=str) |
211 |
216 |
212 def migrate_entity(self, entity, attribute): |
217 def migrate_entity(self, entity, attribute): |
213 """migrate an entity attribute to the storage""" |
218 """migrate an entity attribute to the storage""" |