コード例 #1
0
ファイル: views.py プロジェクト: fengyibo/django-file-manager
def delete(request, url=None):
    "Delete a file/directory"
    
    url = utils.clean_path(url)
    parent = '/'.join(url.split('/')[:-1])
    document_root = utils.get_document_root()
    full_path = os.path.join(document_root, url)
    full_parent = os.path.join(document_root, parent).rstrip('/')

    if request.method == 'POST': 
        
        # If this is a directory, do the walk
        if os.path.isdir(full_path) and not os.path.islink(full_path):
            for root, dirs, files in os.walk(full_path, topdown=False):
                for name in files:
                    os.remove(os.path.join(root, name))
                    
                    try: 
                        obj_path = os.path.join(root, name)
                        obj_path = obj_path.replace(document_root, '', 1)
                        obj = models.File.objects.get(path=obj_path) 
                        utils.log_deletion(request, obj, obj.path)
                        obj.delete()
                    except models.File.DoesNotExist:
                        pass

                for name in dirs:
                    os.rmdir(os.path.join(root, name))
                    
                    try: 
                        obj_path = os.path.join(root, name)
                        obj_path = obj_path.replace(document_root, '', 1)
                        obj = models.File.objects.get(path=obj_path) 
                        utils.log_deletion(request, obj, obj.path) 
                        obj.delete()
                    except models.File.DoesNotExist:
                        pass

            os.rmdir(full_path)

            try: 
                obj_path = full_path.replace(document_root, '', 1)
                obj = models.File.objects.get(path=obj_path) 
                utils.log_deletion(request, obj, obj.path)
                obj.delete()
            except models.File.DoesNotExist:
                pass

        else:
            os.remove(full_path)
            
            try: 
                obj_path = full_path.replace(document_root, '', 1)
                print obj_path
                obj = models.File.objects.get(path=obj_path) 
                utils.log_deletion(request, obj, obj.path)
                obj.delete()
            except models.File.DoesNotExist:
                pass

        return redirect('admin_file_manager_list', url=parent)

    filelist = []
    errorlist = []

    # If this is a directory, generate the list of files to be removed.
    if os.path.isdir(full_path) and not os.path.islink(full_path):
        filelist.append("/%s" % url)
        for root, dirs, files, in os.walk(full_path):
            for name in files: 
                f = os.path.join(root, name).replace(full_parent, '', 1)
                if not os.access(os.path.join(root), os.W_OK):
                    errorlist.append(f)
                filelist.append(f)

            for name in dirs:
                d = os.path.join(root, name).replace(full_parent, '', 1)
                if not os.access(os.path.join(root), os.W_OK):
                    errorlist.append(d)
                filelist.append(d)
    else:
        check_path = full_path

        if os.path.islink(full_path):
            check_path = full_parent
        
        if not os.access(check_path, os.W_OK):
            errorlist.append("/%s" % url)

        filelist.append("/%s" % url)

    # Because python 2.3 is ... painful.
    filelist.sort()
    errorlist.sort()
    
    return render_to_response("admin/file_manager/delete.html", 
                             # {'url': url, 'files': sorted(filelist),
                             #  'errorlist':sorted(errorlist),
                              {'url': url, 'files': filelist,
                               'errorlist': errorlist,
                               'directory': '',},
                              context_instance=template.RequestContext(request))
コード例 #2
0
ファイル: views.py プロジェクト: xzGithu/django-file-manager
def delete(request, url=None):
    "Delete a file/directory"

    url = utils.clean_path(url)
    parent = '/'.join(url.split('/')[:-1])
    document_root = utils.get_document_root()
    full_path = os.path.join(document_root, url)
    full_parent = os.path.join(document_root, parent).rstrip('/')

    if request.method == 'POST':

        # If this is a directory, do the walk
        if os.path.isdir(full_path) and not os.path.islink(full_path):
            for root, dirs, files in os.walk(full_path, topdown=False):
                for name in files:
                    os.remove(os.path.join(root, name))

                    try:
                        obj_path = os.path.join(root, name)
                        obj_path = obj_path.replace(document_root, '', 1)
                        obj = models.File.objects.get(path=obj_path)
                        utils.log_deletion(request, obj, obj.path)
                        obj.delete()
                    except models.File.DoesNotExist:
                        pass

                for name in dirs:
                    os.rmdir(os.path.join(root, name))

                    try:
                        obj_path = os.path.join(root, name)
                        obj_path = obj_path.replace(document_root, '', 1)
                        obj = models.File.objects.get(path=obj_path)
                        utils.log_deletion(request, obj, obj.path)
                        obj.delete()
                    except models.File.DoesNotExist:
                        pass

            os.rmdir(full_path)

            try:
                obj_path = full_path.replace(document_root, '', 1)
                obj = models.File.objects.get(path=obj_path)
                utils.log_deletion(request, obj, obj.path)
                obj.delete()
            except models.File.DoesNotExist:
                pass

        else:
            os.remove(full_path)

            try:
                obj_path = full_path.replace(document_root, '', 1)
                print obj_path
                obj = models.File.objects.get(path=obj_path)
                utils.log_deletion(request, obj, obj.path)
                obj.delete()
            except models.File.DoesNotExist:
                pass

        return redirect('admin_file_manager_list', url=parent)

    filelist = []
    errorlist = []

    # If this is a directory, generate the list of files to be removed.
    if os.path.isdir(full_path) and not os.path.islink(full_path):
        filelist.append("/%s" % url)
        for root, dirs, files, in os.walk(full_path):
            for name in files:
                f = os.path.join(root, name).replace(full_parent, '', 1)
                if not os.access(os.path.join(root), os.W_OK):
                    errorlist.append(f)
                filelist.append(f)

            for name in dirs:
                d = os.path.join(root, name).replace(full_parent, '', 1)
                if not os.access(os.path.join(root), os.W_OK):
                    errorlist.append(d)
                filelist.append(d)
    else:
        check_path = full_path

        if os.path.islink(full_path):
            check_path = full_parent

        if not os.access(check_path, os.W_OK):
            errorlist.append("/%s" % url)

        filelist.append("/%s" % url)

    # Because python 2.3 is ... painful.
    filelist.sort()
    errorlist.sort()

    return render_to_response(
        "admin/file_manager/delete.html",
        # {'url': url, 'files': sorted(filelist),
        #  'errorlist':sorted(errorlist),
        {
            'url': url,
            'files': filelist,
            'errorlist': errorlist,
            'directory': '',
        },
        context_instance=template.RequestContext(request))
コード例 #3
0
ファイル: views.py プロジェクト: fengyibo/django-file-manager
def delete_selected(request, url=None):
    "Delete selected files and directories. (Called from index)"
   
    # Files to remove.
    selected = request.POST.getlist('_selected_action')
    
    url = utils.clean_path(url)
    parent = '/'.join(url.split('/')[:-1])
    document_root = utils.get_document_root()
    full_path = os.path.join(document_root, url)
    full_parent = os.path.join(document_root, parent).rstrip('/')

    if request.method == 'POST' and request.POST.get('post') == 'yes':
        # Files selected to remove.
       
        for item in selected:
            full_path = os.path.join(full_parent, item)

            # If this is a directory, do the walk
            if os.path.isdir(full_path) and not os.path.islink(full_path):
                for root, dirs, files in os.walk(full_path, topdown=False):
                    for name in files:
                        os.remove(os.path.join(root, name))

                        try: 
                            obj_path = os.path.join(root, name)
                            obj_path = obj_path.replace(document_root, '', 1)
                            obj = models.File.objects.get(path=obj_path) 
                            utils.log_deletion(request, obj, obj.path)
                            obj.delete()
                        except models.File.DoesNotExist:
                            pass

                    for name in dirs:
                        os.rmdir(os.path.join(root, name))

                        try: 
                            obj_path = os.path.join(root, name)
                            obj_path = obj_path.replace(document_root, '', 1)
                            obj = models.File.objects.get(path=obj_path) 
                            utils.log_deletion(request, obj, obj.path)
                            obj.delete()
                        except models.File.DoesNotExist:
                            pass

                os.rmdir(full_path)

                try: 
                    obj_path = os.path.join(url, item)
                    obj = models.File.objects.get(path=obj_path) 
                    utils.log_deletion(request, obj, obj.path)
                    obj.delete()
                except models.File.DoesNotExist:
                    pass

            else:
                os.remove(full_path)

                try: 
                    obj_path = os.path.join(url, item)
                    obj = models.File.objects.get(path=obj_path) 
                    utils.log_deletion(request, obj, obj.path)
                    obj.delete()
                except models.File.DoesNotExist:
                    pass

        return None

    filelist = []
    errorlist = []

    for item in selected:
        full_path = os.path.join(full_parent, item)

        # If this is a directory, generate the list of files to be removed.
        if os.path.isdir(full_path) and not os.path.islink(full_path):
            filelist.append("/%s" % item)
            for root, dirs, files, in os.walk(full_path):
                for name in files: 
                    f = os.path.join(root, name).replace(full_parent, '', 1)
                    if not os.access(os.path.join(root), os.W_OK):
                        errorlist.append(f)
                    filelist.append(f)

                for name in dirs:
                    d = os.path.join(root, name).replace(full_parent, '', 1)
                    if not os.access(os.path.join(root), os.W_OK):
                        errorlist.append(d)
                    filelist.append(d)
        else:
            if not os.access(full_path, os.W_OK):
                errorlist.append("/%s" % item)

            filelist.append("/%s" % item)

    # Because python 2.3 is ... painful.
    filelist.sort()
    errorlist.sort()
    
    return render_to_response("admin/file_manager/delete_selected.html", 
                              {'files': filelist,
                               'errorlist': errorlist,
                               'selected': selected,
                               'directory': '',},
                              context_instance=template.RequestContext(request))
コード例 #4
0
ファイル: views.py プロジェクト: xzGithu/django-file-manager
def delete_selected(request, url=None):
    "Delete selected files and directories. (Called from index)"

    # Files to remove.
    selected = request.POST.getlist('_selected_action')

    url = utils.clean_path(url)
    parent = '/'.join(url.split('/')[:-1])
    document_root = utils.get_document_root()
    full_path = os.path.join(document_root, url)
    full_parent = os.path.join(document_root, parent).rstrip('/')

    if request.method == 'POST' and request.POST.get('post') == 'yes':
        # Files selected to remove.

        for item in selected:
            full_path = os.path.join(full_parent, item)

            # If this is a directory, do the walk
            if os.path.isdir(full_path) and not os.path.islink(full_path):
                for root, dirs, files in os.walk(full_path, topdown=False):
                    for name in files:
                        os.remove(os.path.join(root, name))

                        try:
                            obj_path = os.path.join(root, name)
                            obj_path = obj_path.replace(document_root, '', 1)
                            obj = models.File.objects.get(path=obj_path)
                            utils.log_deletion(request, obj, obj.path)
                            obj.delete()
                        except models.File.DoesNotExist:
                            pass

                    for name in dirs:
                        os.rmdir(os.path.join(root, name))

                        try:
                            obj_path = os.path.join(root, name)
                            obj_path = obj_path.replace(document_root, '', 1)
                            obj = models.File.objects.get(path=obj_path)
                            utils.log_deletion(request, obj, obj.path)
                            obj.delete()
                        except models.File.DoesNotExist:
                            pass

                os.rmdir(full_path)

                try:
                    obj_path = os.path.join(url, item)
                    obj = models.File.objects.get(path=obj_path)
                    utils.log_deletion(request, obj, obj.path)
                    obj.delete()
                except models.File.DoesNotExist:
                    pass

            else:
                os.remove(full_path)

                try:
                    obj_path = os.path.join(url, item)
                    obj = models.File.objects.get(path=obj_path)
                    utils.log_deletion(request, obj, obj.path)
                    obj.delete()
                except models.File.DoesNotExist:
                    pass

        return None

    filelist = []
    errorlist = []

    for item in selected:
        full_path = os.path.join(full_parent, item)

        # If this is a directory, generate the list of files to be removed.
        if os.path.isdir(full_path) and not os.path.islink(full_path):
            filelist.append("/%s" % item)
            for root, dirs, files, in os.walk(full_path):
                for name in files:
                    f = os.path.join(root, name).replace(full_parent, '', 1)
                    if not os.access(os.path.join(root), os.W_OK):
                        errorlist.append(f)
                    filelist.append(f)

                for name in dirs:
                    d = os.path.join(root, name).replace(full_parent, '', 1)
                    if not os.access(os.path.join(root), os.W_OK):
                        errorlist.append(d)
                    filelist.append(d)
        else:
            if not os.access(full_path, os.W_OK):
                errorlist.append("/%s" % item)

            filelist.append("/%s" % item)

    # Because python 2.3 is ... painful.
    filelist.sort()
    errorlist.sort()

    return render_to_response(
        "admin/file_manager/delete_selected.html", {
            'files': filelist,
            'errorlist': errorlist,
            'selected': selected,
            'directory': '',
        },
        context_instance=template.RequestContext(request))