From b3f6c311b33dfbed658f2fd9271066cc45b69be1 Mon Sep 17 00:00:00 2001 From: Chase Sterling Date: Sat, 9 Jul 2016 15:50:07 -0400 Subject: [PATCH] Switch page history view to use jquery datatable --- realms/modules/wiki/models.py | 14 ++++++++ realms/modules/wiki/views.py | 38 +++++++++++++-------- realms/templates/wiki/history.html | 55 +++++++++++------------------- 3 files changed, 57 insertions(+), 50 deletions(-) diff --git a/realms/modules/wiki/models.py b/realms/modules/wiki/models.py index aa276a3..3003978 100644 --- a/realms/modules/wiki/models.py +++ b/realms/modules/wiki/models.py @@ -142,6 +142,20 @@ class WikiPage(object): finally: cache.set(self._cache_key('history'), cached_revs) + @property + def history_cache(self): + """Get info about the history cache. + + :return: tuple -- (cached items, cache complete?) + """ + cache_complete = False + cached_revs = cache.get(self._cache_key('history')) or [] + if cached_revs: + if cached_revs[-1] == 'TAIL': + del cached_revs[-1] + cache_complete = True + return len(cached_revs), cache_complete + @property def partials(self): data = self.data diff --git a/realms/modules/wiki/views.py b/realms/modules/wiki/views.py index f7c474b..0dbc304 100644 --- a/realms/modules/wiki/views.py +++ b/realms/modules/wiki/views.py @@ -64,22 +64,32 @@ def revert(): def history(name): if current_app.config.get('PRIVATE_WIKI') and current_user.is_anonymous(): return current_app.login_manager.unauthorized() - items_per_page = 15 - page = int(request.args.get('page', 1)) - start_index = (page - 1) * items_per_page - hist = g.current_wiki.get_page(name).history - # Grab one extra item to see if there is a next page - items = list(itertools.islice(hist, start_index, start_index+items_per_page+1)) - more = False - if len(items) > items_per_page: - more = True - items.pop() - if page > 1 and not items: - abort(404, 'Page is past end of history.') + return render_template('wiki/history.html', name=name) + + +@blueprint.route("/_history_data/") +def history_data(name): + """Ajax provider for paginated history data.""" + if current_app.config.get('PRIVATE_WIKI') and current_user.is_anonymous(): + return current_app.login_manager.unauthorized() + draw = int(request.args.get('draw', 0)) + start = int(request.args.get('start', 0)) + length = int(request.args.get('length', 10)) + page = g.current_wiki.get_page(name) + items = list(itertools.islice(page.history, start, start + length)) for item in items: item['gravatar'] = gravatar_url(item['author_email']) - return render_template('wiki/history.html', name=name, history=items, - pagination={'page': page, 'more': more}) + total_records, hist_complete = page.history_cache + if not hist_complete: + # Force datatables to fetch more data when it gets to the end + total_records += 1 + return { + 'draw': draw, + 'recordsTotal': total_records, + 'recordsFiltered': total_records, + 'data': items + } + @blueprint.route("/_edit/") diff --git a/realms/templates/wiki/history.html b/realms/templates/wiki/history.html index c80ccd5..8595554 100644 --- a/realms/templates/wiki/history.html +++ b/realms/templates/wiki/history.html @@ -6,49 +6,14 @@ Compare Revisions

- +
- - {% if pagination.page > 1 or pagination.more %} - - - - {% endif %} - - {% for h in history %} - - - - - - - {% endfor %} -
Name Revision Message Date
-
    -
  • - Previous -
  • - {% for p in range(1, pagination.page + 1) %} -
  • - {{ p }} -
  • - {% endfor %} - {% if pagination.more %}
  • {% endif %} -
  • - Next -
  • -
-
- {% if h.type != 'delete' %} - - {% endif %} - {{ h.author }}View {{ h.message }} {{ h.time|datetime }}

Compare Revisions @@ -58,6 +23,24 @@ {% block js %}