上传到目录时锁定目录

时间:2011-07-22 08:46:33

标签: python django

当用户上传文件时,我一直在考虑用username和datetime更新目录名。这样用户的最新上传就会显示在上面。我有一个上传功能,并重命名如下:

# Check if form is valid and upload   
    if form.is_valid():
        form.save(request.FILES, request)
    # Edit the previous folder to have new datetime and user marking if the folder has such.
    currentPath = post_data.get('path').encode("utf-8")
    prevfolder = os.path.basename(post_data.get('path').encode("utf-8"))
    try:
        casename, rest = prevfolder.split(" [",1)
    #print(casename)
        dest = renameOnUpload(request,currentPath, casename)
    except:
        dest = form.path

        return HttpResponseRedirect('/fm/list/%s' % dest)

def renameOnUpload(request,path,casename):
    datetime_string = get_currenttime()
    user_string = " ["+ request.user.username + "]"
    newcasename = casename+user_string.encode("utf-8")+datetime_string
    dest = os.path.join(os.path.dirname(path), newcasename)
    if not path == dest:
        fmoper.move(path, dest)
    else:
        dest = path
    return dest

fmoper.move

def move(src, dst, replace=False):
    ensure_dir(dst)             # Ensure that we have a destination folder, if not create it. 
    if not replace and os.path.exists(dst):
        dst = existname(dst)
    return shutil.move(src, dst)

所以它上传文件并在之后移动。当用户上传而另一个同时上传时会导致问题,在此过程中重命名文件夹会导致一个上传者上传到不存在的文件夹。

当用户上传文件夹时,我是否可以锁定文件夹? currentPath是上传到目标文件夹并转发给重命名的文件夹。

0 个答案:

没有答案