server/sources/storages.py
changeset 4349 48dadeeacfa5
parent 4329 815e08c53548
child 4512 e7ac20bf3629
equal deleted inserted replaced
4348:6bf1e46be6ae 4349:48dadeeacfa5
     1 """custom storages for the system source"""
     1 """custom storages for the system source"""
     2 from os import unlink, path as osp
     2 from os import unlink, path as osp
     3 
     3 
       
     4 from cubicweb import Binary
     4 from cubicweb.server.hook import Operation
     5 from cubicweb.server.hook import Operation
     5 
     6 
     6 
     7 
     7 ETYPE_ATTR_STORAGE = {}
     8 ETYPE_ATTR_STORAGE = {}
     8 def set_attribute_storage(repo, etype, attr, storage):
     9 def set_attribute_storage(repo, etype, attr, storage):
    75     def entity_deleted(self, entity, attr):
    76     def entity_deleted(self, entity, attr):
    76         """an entity using this storage for attr has been deleted"""
    77         """an entity using this storage for attr has been deleted"""
    77         DeleteFileOp(entity._cw, filepath=self.current_fs_path(entity, attr))
    78         DeleteFileOp(entity._cw, filepath=self.current_fs_path(entity, attr))
    78 
    79 
    79     def new_fs_path(self, entity, attr):
    80     def new_fs_path(self, entity, attr):
    80         fpath = osp.join(self.default_directory, '%s_%s_%s' % (
    81         fspath = osp.join(self.default_directory, '%s_%s' % (entity.eid, attr))
    81             self.default_directory, entity.eid, attr))
       
    82         while osp.exists(fspath):
    82         while osp.exists(fspath):
    83             fspath = '_' + fspath
    83             fspath = '_' + fspath
    84         return fspath
    84         return fspath
    85 
    85 
    86     def current_fs_path(self, entity, attr):
    86     def current_fs_path(self, entity, attr):
    87         cu = entity._cw.system_sql('SELECT cw_%s.%s WHERE cw_eid=%s' %
    87         sysource = entity._cw.pool.source('system')
    88                                    (entity.__regid__, attr, entity.eid))
    88         cu = sysource.doexec(entity._cw,
    89         return cu.fetchone()[0]
    89                              'SELECT cw_%s FROM cw_%s WHERE cw_eid=%s' % (
       
    90                                  attr, entity.__regid__, entity.eid))
       
    91         dbmod = sysource.dbapi_module
       
    92         return dbmod.process_value(cu.fetchone()[0], [None, dbmod.BINARY],
       
    93                                    binarywrap=str)
    90 
    94 
    91 
    95 
    92 class AddFileOp(Operation):
    96 class AddFileOp(Operation):
    93     def rollback_event(self):
    97     def rollback_event(self):
    94         try:
    98         try:
   105 
   109 
   106 class UpdateFileOp(Operation):
   110 class UpdateFileOp(Operation):
   107     def precommit_event(self):
   111     def precommit_event(self):
   108         try:
   112         try:
   109             file(self.filepath, 'w').write(self.filedata)
   113             file(self.filepath, 'w').write(self.filedata)
   110         except:
   114         except Exception, ex:
   111             pass
   115             self.exception(str(ex))