Merge pull request #92 from gazpachoking/tree_index

Implements a tree view index
This commit is contained in:
Matthew Scragg 2015-12-13 10:18:08 -06:00
commit 766a782484
2 changed files with 49 additions and 8 deletions

View file

@ -1,3 +1,5 @@
import itertools
import sys
from flask import abort, g, render_template, request, redirect, Blueprint, flash, url_for, current_app from flask import abort, g, render_template, request, redirect, Blueprint, flash, url_for, current_app
from flask.ext.login import login_required, current_user from flask.ext.login import login_required, current_user
from realms.lib.util import to_canonical, remove_ext from realms.lib.util import to_canonical, remove_ext
@ -103,17 +105,47 @@ def create(name):
info={}) info={})
def _get_subdir(path, depth):
parts = path.split('/', depth)
if len(parts) > depth:
return parts[-2]
def _tree_index(items, path=""):
depth = len(path.split("/"))
items = filter(lambda x: x['name'].startswith(path), items)
items = sorted(items, key=lambda x: x['name'])
for subdir, items in itertools.groupby(items, key=lambda x: _get_subdir(x['name'], depth)):
if not subdir:
for item in items:
yield dict(item, dir=False)
else:
size = 0
ctime = sys.maxint
mtime = 0
for item in items:
size += item['size']
ctime = min(item['ctime'], ctime)
mtime = max(item['mtime'], mtime)
yield dict(name=path + subdir + "/",
mtime=mtime,
ctime=ctime,
size=size,
dir=True)
@blueprint.route("/_index", defaults={"path": ""}) @blueprint.route("/_index", defaults={"path": ""})
@blueprint.route("/_index/<path:path>") @blueprint.route("/_index/<path:path>")
def index(path): def index(path):
items = g.current_wiki.get_index()
if path:
path = to_canonical(path) + "/"
items = (i for i in items if i['name'].startswith(path))
if current_app.config.get('PRIVATE_WIKI') and current_user.is_anonymous(): if current_app.config.get('PRIVATE_WIKI') and current_user.is_anonymous():
return current_app.login_manager.unauthorized() return current_app.login_manager.unauthorized()
return render_template('wiki/index.html', index=items, path=path) items = g.current_wiki.get_index()
if path:
path = to_canonical(path) + "/"
return render_template('wiki/index.html', index=_tree_index(items, path=path), path=path)
@blueprint.route("/<path:name>", methods=['POST', 'PUT', 'DELETE']) @blueprint.route("/<path:name>", methods=['POST', 'PUT', 'DELETE'])

View file

@ -9,19 +9,28 @@ $(document).ready(function() {
{% endblock %} {% endblock %}
{% block body %} {% block body %}
<h2>Index of /{{ path }}</h2> <h2>Index of <a href="{{ url_for('wiki.index') }}">/</a>
{%- set parts = path.split('/') -%}
{%- for dir in parts if dir -%}
<a href="{{ url_for('wiki.index', path='/'.join(parts[:loop.index])) }}">{{ dir }}/</a>
{%- endfor -%}
</h2>
<table class="table table-bordered data-table"> <table class="table table-bordered data-table">
<thead> <thead>
<tr> <tr>
<th>Name</th> <th>Name</th>
<th class="hidden-xs">Bytes</th> <th class="hidden-xs">Bytes</th>
<th>Modified</th> <th>Created</th>
<th class="hidden-xs hidden-sm">Created</th> <th class="hidden-xs hidden-sm">Modified</th>
</tr> </tr>
</thead> </thead>
{% for file in index %} {% for file in index %}
<tr> <tr>
{% if file['dir'] %}
<td><a href="{{ url_for('wiki.index', path=file['name']) }}">{{ file['name'][path|length:] }}</a></td>
{% else %}
<td><a href="{{ url_for('wiki.page', name=file['name']) }}">{{ file['name'][path|length:] }}</a></td> <td><a href="{{ url_for('wiki.page', name=file['name']) }}">{{ file['name'][path|length:] }}</a></td>
{% endif %}
<td class="hidden-xs">{{ file['size'] }}</td> <td class="hidden-xs">{{ file['size'] }}</td>
<td>{{ file['ctime']|datetime }}</td> <td>{{ file['ctime']|datetime }}</td>
<td class="hidden-xs hidden-sm">{{ file['mtime']|datetime }}</td> <td class="hidden-xs hidden-sm">{{ file['mtime']|datetime }}</td>