Пример #1
0
def listdir(request, path, chooser):
    """
    Implements directory listing (or index).

    Intended to be called via view().

    TODO: Remove?
    """
    if not request.fs.isdir(path):
        raise PopupException(_("Not a directory: %(path)s") % {'path': path})

    file_filter = request.REQUEST.get('file_filter', 'any')

    assert file_filter in ['any', 'file', 'dir']

    home_dir_path = request.user.get_home_directory()

    breadcrumbs = parse_breadcrumbs(path)

    data = {
        'path': path,
        'file_filter': file_filter,
        'breadcrumbs': breadcrumbs,
        'current_dir_path': path,
        'current_request_path': request.path,
        'home_directory': request.fs.isdir(home_dir_path) and home_dir_path or None,
        'cwd_set': True,
        'is_superuser': request.user.username == request.fs.superuser,
        'groups': request.user.username == request.fs.superuser and [str(x) for x in Group.objects.values_list('name', flat=True)] or [],
        'users': request.user.username == request.fs.superuser and [str(x) for x in User.objects.values_list('username', flat=True)] or [],
        'superuser': request.fs.superuser,
        'show_upload': (request.REQUEST.get('show_upload') == 'false' and (False,) or (True,))[0],
        'show_download_button': SHOW_DOWNLOAD_BUTTON.get(),
        'show_upload_button': SHOW_UPLOAD_BUTTON.get()
    }

    stats = request.fs.listdir_stats(path)

    # Include parent dir, unless at filesystem root.
    if not request.fs.isroot(path):
        parent_path = request.fs.join(path, "..")
        parent_stat = request.fs.stats(parent_path)
        # The 'path' field would be absolute, but we want its basename to be
        # actually '..' for display purposes. Encode it since _massage_stats expects byte strings.
        parent_stat['path'] = parent_path
        stats.insert(0, parent_stat)

    data['files'] = [_massage_stats(request, stat) for stat in stats]
    if chooser:
        return render('chooser.mako', request, data)
    else:
        return render('listdir.mako', request, data)
Пример #2
0
def listdir_paged(request, path):
    """
    A paginated version of listdir.

    Query parameters:
      pagenum           - The page number to show. Defaults to 1.
      pagesize          - How many to show on a page. Defaults to 15.
      sortby=?          - Specify attribute to sort by. Accepts:
                            (type, name, atime, mtime, size, user, group)
                          Defaults to name.
      descending        - Specify a descending sort order.
                          Default to false.
      filter=?          - Specify a substring filter to search for in
                          the filename field.
    """
    if not request.fs.isdir(path):
        raise PopupException("Not a directory: %s" % (path,))

    pagenum = int(request.GET.get('pagenum', 1))
    pagesize = int(request.GET.get('pagesize', 30))
    do_as = None
    if request.user.is_superuser or request.user.has_hue_permission(action="impersonate", app="security"):
      do_as = request.GET.get('doas', request.user.username)
    if hasattr(request, 'doas'):
      do_as = request.doas

    home_dir_path = request.user.get_home_directory()
    breadcrumbs = parse_breadcrumbs(path)

    if do_as:
      all_stats = request.fs.do_as_user(do_as, request.fs.listdir_stats, path)
    else:
      all_stats = request.fs.listdir_stats(path)


    # Filter first
    filter_str = request.GET.get('filter', None)
    if filter_str:
        filtered_stats = filter(lambda sb: filter_str in sb['name'], all_stats)
        all_stats = filtered_stats

    # Sort next
    sortby = request.GET.get('sortby', None)
    descending_param = request.GET.get('descending', None)
    if sortby is not None:
        if sortby not in ('type', 'name', 'atime', 'mtime', 'user', 'group', 'size'):
            logger.info("Invalid sort attribute '%s' for listdir." %
                        (sortby,))
        else:
            all_stats = sorted(all_stats,
                               key=operator.attrgetter(sortby),
                               reverse=coerce_bool(descending_param))


    # Do pagination
    page = paginator.Paginator(all_stats, pagesize).page(pagenum)
    shown_stats = page.object_list

    # Include parent dir always as second option, unless at filesystem root.
    if not request.fs.isroot(path):
        parent_path = request.fs.join(path, "..")
        parent_stat = request.fs.stats(parent_path)
        # The 'path' field would be absolute, but we want its basename to be
        # actually '..' for display purposes. Encode it since _massage_stats expects byte strings.
        parent_stat['path'] = parent_path
        parent_stat['name'] = ".."
        shown_stats.insert(0, parent_stat)

    # Include same dir always as first option to see stats of the current folder
    current_stat = request.fs.stats(path)
    # The 'path' field would be absolute, but we want its basename to be
    # actually '.' for display purposes. Encode it since _massage_stats expects byte strings.
    current_stat['path'] = path
    current_stat['name'] = "."
    shown_stats.insert(1, current_stat)

    page.object_list = [ _massage_stats(request, s) for s in shown_stats ]

    is_fs_superuser = _is_hdfs_superuser(request)
    data = {
        'path': path,
        'breadcrumbs': breadcrumbs,
        'current_request_path': request.path,
        'files': page.object_list,
        'page': _massage_page(page),
        'pagesize': pagesize,
        'home_directory': request.fs.isdir(home_dir_path) and home_dir_path or None,
        'sortby': sortby,
        'descending': descending_param,
        # The following should probably be deprecated
        'cwd_set': True,
        'file_filter': 'any',
        'current_dir_path': path,
        'is_fs_superuser': is_fs_superuser,
        'groups': is_fs_superuser and [str(x) for x in Group.objects.values_list('name', flat=True)] or [],
        'users': is_fs_superuser and [str(x) for x in User.objects.values_list('username', flat=True)] or [],
        'superuser': request.fs.superuser,
        'supergroup': request.fs.supergroup,
        'is_sentry_managed': request.fs.is_sentry_managed(path),
        'apps': appmanager.get_apps_dict(request.user).keys(),
        'show_download_button': SHOW_DOWNLOAD_BUTTON.get(),
        'show_upload_button': SHOW_UPLOAD_BUTTON.get()
    }
    return render('listdir.mako', request, data)