Beispiel #1
0
    def copy(self,src,dst):
        """ copy a resource from src to dst """
        if self.User == None:
            raise DAV_Error( 401 )
        
        sess=self.Session()
        source = self.uri2obj(src, sess)
        destination = self.uri2obj(dst, sess)
        
        if destination == None :
            path = urlparse.urlparse(destination)[2]
            path_array = path.split('/')
            name = path_array[-1]
            parent_path = string.join(path_array[:-1],'/')+'/'
            if parent_path == '':            
                raise DAV_Forbidden
            
            parent = sess.query(TreeObject).filter_by(path=parent_path).first()
    
            if parent == None :
                raise DAV_Error

            if self.User.groups == []:
                destination = TreeObject(name,TreeObject.TYPE_FILE,parent,self.User.id,None,0,0,path)
            else:                
                destination = TreeObject(name,TreeObject.TYPE_FILE,parent,self.User.id,self.User.groups[0].id,0,0,path)
        
            sess.add(destination)
            
            sess.commit()
            
            rest = sess.query(ActionRestrict).filter_by(actor_id=self.User.id, object_id=parent.id )
            
            for r in rest:
                sess.add(ActionRestrict(self.User.id, r.actor_type, destination.id, r.action ))
            
            rev = ObjectRevision()
            rev.mod_time = source.mod_time
            rev.revision = 1
            rev.content = source.content
            
            destination.revisions.append(rev)
                            
            sess.commit()
        else:
            old_rev = destination.last_revision   
                     
            try:
                prop = filter(lambda pr: pr, destination.properties)[0]            
            except IndexError:
                prop = None
                
            if prop != None:
                rev = ObjectRevision()
                rev.mod_time = source.mod_time
            
                if old_rev == None:
                    rev.revision = 1            
                else:
                    hist = self.uri2obj(string.join([destination.path[:-1],".history",""],'/'), sess)                           
                    
                    if hist != None:                    
                        prev_rev = ObjectRevision()
                        prev_rev.content = old_rev.content
                        prev_rev.revision = old_rev.revision
                        prev_rev.mod_time = old_rev.mod_time
                        rev.revision = old_rev.revision + 1
                        
                        prev_name = "%s_%s" % (destination.name, datetime.fromtimestamp(old_rev.mod_time).strftime("%Y-%m-%d-%H-%M-%S"))    
                        prev = TreeObject(prev_name,TreeObject.TYPE_REV_FILE,hist,self.User.id,None,0,0,string.join([hist.path[:-1],prev_name],'/'))
                       
                        sess.add(prev)
                        prev.revisions.append(prev_rev)                
                        
                        sess.commit() 
                        rest = sess.query(ActionRestrict).filter_by(actor_id=self.User.id, object_id=hist.id )       
                        sess.add(ActionRestrict(self.User.id, '1', prev.id, user_hist_acts ))
                        
                        sess.commit()
            else :
                if old_rev == None:
                    rev = ObjectRevision()
                    rev.mod_time = source.mod_time
                    rev.revision = 1
                else:
                    rev = old_rev
            
            rev.content = source.content
        
            destination.revisions.append(rev)                
            
            sess.add(destination)        
            sess.commit()    
            pass
        sess.close()
Beispiel #2
0
    def put(self, uri, data, content_type='application/octet-stream'):
        """ put the object into the filesystem """
        if self.User == None:
            raise DAV_Error( 401 )
        
        sess = self.Session()
        
        self.User = sess.merge(self.User)
        
        path = urlparse.urlparse(uri)[2]
        path_array = path.split('/')
        name = path_array[-1]
        parent_path = string.join(path_array[:-1],'/')+'/'
        if parent_path == '':            
            raise DAV_Forbidden
        
        parent = sess.query(TreeObject).filter_by(path=parent_path).first()

        if parent == None :
            raise DAV_Error

        obj = self.uri2obj(uri, sess)
        
        if obj == None:
            if self.User.groups == []:
                obj = TreeObject(name,TreeObject.TYPE_FILE,parent,self.User.id,None,0,0,path)
            else:                
                obj = TreeObject(name,TreeObject.TYPE_FILE,parent,self.User.id,self.User.groups[0].id,0,0,path)
        
            sess.add(obj)
            
            sess.commit()
            
            rest = sess.query(ActionRestrict).filter_by(actor_id=self.User.id, object_id=parent.id )
            
            for r in rest:
                sess.add(ActionRestrict(self.User.id, r.actor_type, obj.id, r.action ))
                
            sess.commit()
        
        obj.mod_time = time.time()
        
        try:
            prop = filter(lambda pr: pr, parent.properties)[0]            
        except IndexError:
            prop = None
        
        old_rev = obj.last_revision
        #obj.revisions
        if prop != None:
            rev = ObjectRevision()
            rev.mod_time = obj.mod_time
        
            if old_rev == None:
                rev.revision = 1            
            else:
                hist = self.uri2obj(string.join([parent.path[:-1],".history",""],'/'), sess)                           
                
                if hist != None:                    
                    prev_rev = ObjectRevision()
                    prev_rev.content = old_rev.content
                    prev_rev.revision = old_rev.revision
                    prev_rev.mod_time = old_rev.mod_time
                    rev.revision = old_rev.revision + 1
                    
                    prev_name = "%s_%s" % (name, datetime.fromtimestamp(old_rev.mod_time).strftime("%Y-%m-%d-%H-%M-%S"))    
                    prev = TreeObject(prev_name,TreeObject.TYPE_REV_FILE,hist,self.User.id,None,0,0,string.join([hist.path[:-1],prev_name],'/'))
#                    prev_file_path = "%s_%i_%i" % (name, obj.id, prev_rev.revision)
#                    prev_file_link = "%s_%i_%i" % (prev_name, obj.id, prev_rev.revision)
#                    
#                    os.symlink(prev_file_path, prev_file_link)
                    
                    sess.add(prev)
                    prev.revisions.append(prev_rev)                
                    
                    sess.commit() 
                    rest = sess.query(ActionRestrict).filter_by(actor_id=self.User.id, object_id=hist.id )       
                    sess.add(ActionRestrict(self.User.id, '1', prev.id, user_hist_acts ))
                    
                    sess.commit()
        else :
            if old_rev == None:
                rev = ObjectRevision()
                rev.mod_time = obj.mod_time
                rev.revision = 1
            else:
                rev = old_rev           
        if content_type == None:
            content_type = "application/octet-stream"
        
        if obj.id != None:
            file_path = "%s_%i_%i" % (name, obj.id, rev.revision)
        else:
            file_path = "%s_1_%i" % (name, rev.revision)
        
        try:
            fp=open("%s/%s" % (self.directory, file_path), "w+")
            if isinstance(data, types.GeneratorType):
                for d in data:
                    fp.write(d)
            else:
                if data:
                    fp.write(data)
            fp.close()
            log.info('put: Created %s' % uri)
        except:
            log.info('put: Could not create %s' % uri)
            raise DAV_Error, 424
        
        rev.content = Content(file_path, content_type) 
        
        obj.revisions.append(rev)                
        
        sess.add(obj)        
        sess.commit()
        sess.close()