Exemple #1
0
    def make_choices(self):
 
        choices = []
        ignore = utils.get_ignore_list()

        # Make "/" valid"
        d = self.document_root
        d_short = d.replace(self.document_root, "", 1)
        if not d_short:
            d_short = '/'
        
        choices.append((d, d_short))

        for root, dirs, files in os.walk(self.document_root):
            choices.extend(utils.directory_file(self.document_root, ignore, root, dirs, self.original ))

        #return sorted(choices)      
        choices.sort()
        return choices
def index(request, url=None):
    "Show list of files in a url inside of the document root."

    if request.method == 'POST': 
        if request.POST.get('action') == 'delete_selected':
            response = delete_selected(request, url)
            if response:
                return response

    # Stuff the files in here.
    files = []
    directory = {}
    perms = [ '---', '--x', '-w-', '-wx', 'r--', 'r-x', 'rw-', 'rwx' ]

    ignore = utils.get_ignore_list()
    url = utils.clean_path(url)
    full_path = os.path.join(utils.get_document_root(), url)

    try:
        listing = os.listdir(full_path)
    except OSError:
        raise http.Http404

    directory['url'] = url
    directory['parent'] = '/'.join(url.split('/')[:-1])

    if os.access(full_path, os.R_OK):
        directory['can_read'] = True
    else:
        directory['can_read'] = False 
    
    if os.access(full_path, os.W_OK):
        directory['can_write'] = True
    else:
        directory['can_write'] = False

    for file in listing:
        if file in ignore: 
            continue

        item = {}
        dperms = '-'
        
        item['filename'] = file
        item['filepath'] = os.path.join(full_path, file)
        item['fileurl'] = os.path.join(url, file)

        # type (direcory/file/link)
        item['directory'] = os.path.isdir(item['filepath'])
        item['link'] = os.path.islink(item['filepath'])

        if item['link']:
            item['link_src'] = os.path.normpath(os.path.join(url, os.readlink(item['filepath'])))

        # Catch broken links.
        try: 
            itemstat = os.stat(item['filepath'])
            item['user'] = getpwuid(itemstat.st_uid)[0]
            item['group'] = getgrgid(itemstat.st_gid)[0]

            # size (in bytes ) for use with |filesizeformat
            item['size'] = itemstat.st_size

            # ctime, mtime
            item['ctime'] = datetime.fromtimestamp(itemstat.st_ctime)
            item['mtime'] = datetime.fromtimestamp(itemstat.st_mtime)

            # permissions (numeric)
            octs = "%04d" % int(oct(itemstat.st_mode & 0777))
            item['perms_numeric'] = octs
            item['perms'] = "%s%s%s%s" % (dperms, perms[int(octs[1])], 
                                            perms[int(octs[2])], 
                                            perms[int(octs[3])])

        except:
            # Blank out because of broken link.
            item['user'] = item['group'] = ''
            item['perms_numeric'] = item['perms'] = ''
            item['size'] = item['ctime'] = item['mtime'] = None
            item['broken_link'] = True

        mime = mimetypes.guess_type(item['filepath'], False)[0]
  
        # Assume we can't edit anything except text and unknown.
        if not mime:
            item['can_edit'] = True
        elif 'javascript' in mime:
            item['can_edit'] = True
        elif 'text' in mime:
            item['can_edit'] = True
        else:
            item['can_edit'] = False

        if item['directory']:
            item['can_edit'] = False
            dperms = 'd'
     
        if os.access(item['filepath'], os.R_OK):
            item['can_read'] = True
        else:
            item['can_read'] = False

        if os.access(item['filepath'], os.W_OK):
            item['can_write'] = True
        else:
            item['can_write'] = False

        files.append(item)
    
    return render_to_response("admin/file_manager/index.html", 
                              {'directory': directory, 'files': files,},
                              context_instance=template.RequestContext(request))
Exemple #3
0
def index(request, url=None):
    "Show list of files in a url inside of the document root."

    if request.method == 'POST':
        if request.POST.get('action') == 'delete_selected':
            response = delete_selected(request, url)
            if response:
                return response

    # Stuff the files in here.
    files = []
    directory = {}
    perms = ['---', '--x', '-w-', '-wx', 'r--', 'r-x', 'rw-', 'rwx']

    ignore = utils.get_ignore_list()
    url = utils.clean_path(url)
    full_path = os.path.join(utils.get_document_root(), url)

    try:
        listing = os.listdir(full_path)
    except OSError:
        raise http.Http404

    directory['url'] = url
    directory['parent'] = '/'.join(url.split('/')[:-1])

    if os.access(full_path, os.R_OK):
        directory['can_read'] = True
    else:
        directory['can_read'] = False

    if os.access(full_path, os.W_OK):
        directory['can_write'] = True
    else:
        directory['can_write'] = False

    for file in listing:
        if file in ignore:
            continue

        item = {}
        dperms = '-'

        item['filename'] = file
        item['filepath'] = os.path.join(full_path, file)
        item['fileurl'] = os.path.join(url, file)

        # type (direcory/file/link)
        item['directory'] = os.path.isdir(item['filepath'])
        item['link'] = os.path.islink(item['filepath'])

        if item['link']:
            item['link_src'] = os.path.normpath(
                os.path.join(url, os.readlink(item['filepath'])))

        # Catch broken links.
        try:
            itemstat = os.stat(item['filepath'])
            item['user'] = getpwuid(itemstat.st_uid)[0]
            item['group'] = getgrgid(itemstat.st_gid)[0]

            # size (in bytes ) for use with |filesizeformat
            item['size'] = itemstat.st_size

            # ctime, mtime
            item['ctime'] = datetime.fromtimestamp(itemstat.st_ctime)
            item['mtime'] = datetime.fromtimestamp(itemstat.st_mtime)

            # permissions (numeric)
            octs = "%04d" % int(oct(itemstat.st_mode & 0777))
            item['perms_numeric'] = octs
            item['perms'] = "%s%s%s%s" % (dperms, perms[int(
                octs[1])], perms[int(octs[2])], perms[int(octs[3])])

        except:
            # Blank out because of broken link.
            item['user'] = item['group'] = ''
            item['perms_numeric'] = item['perms'] = ''
            item['size'] = item['ctime'] = item['mtime'] = None
            item['broken_link'] = True

        mime = mimetypes.guess_type(item['filepath'], False)[0]

        # Assume we can't edit anything except text and unknown.
        if not mime:
            item['can_edit'] = True
        elif 'javascript' in mime:
            item['can_edit'] = True
        elif 'text' in mime:
            item['can_edit'] = True
        else:
            item['can_edit'] = False

        if item['directory']:
            item['can_edit'] = False
            dperms = 'd'

        if os.access(item['filepath'], os.R_OK):
            item['can_read'] = True
        else:
            item['can_read'] = False

        if os.access(item['filepath'], os.W_OK):
            item['can_write'] = True
        else:
            item['can_write'] = False

        files.append(item)

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