diff --git a/app.py b/app.py index fb729d5..67be47b 100755 --- a/app.py +++ b/app.py @@ -4,6 +4,7 @@ from flask import Flask, render_template, url_for, redirect, request, g, jsonify from depicts import (utils, wdqs, commons, mediawiki, painting, saam, database, dia, rijksmuseum, npg, museodelprado, barnesfoundation, wd_catalog) +from depicts.pager import Pagination, init_pager from depicts.model import DepictsItem, DepictsItemAltLabel, Edit, PaintingItem from requests_oauthlib import OAuth1Session from urllib.parse import urlencode @@ -24,6 +25,7 @@ user_agent = 'Mozilla/5.0 (X11; Linux i586; rv:32.0) Gecko/20160101 Firefox/32.0 app = Flask(__name__) app.config.from_object('config.default') database.init_db(app.config['DB_URL']) +init_pager(app) find_more_props = { 'P135': 'movement', @@ -664,20 +666,24 @@ def browse_page(): page_size = 45 item_map = wdqs.build_browse_item_map(bindings) - items = [] + + all_items = [] for item in item_map.values(): if len(item['image_filename']) != 1: continue item['image_filename'] = item['image_filename'][0] - items.append(item) - if len(items) >= page_size: - break + all_items.append(item) + + page = utils.get_int_arg('page') or 1 + pager = Pagination(page, page_size, len(all_items)) + + items = pager.slice(all_items) filenames = [cur['image_filename'] for cur in items] thumbwidth = app.config['THUMBWIDTH'] - filename = f'cache/{flat}_{page_size}_images.json' + filename = f'cache/{flat}_{page}_{page_size}_images.json' if os.path.exists(filename): detail = json.load(open(filename)) else: @@ -694,6 +700,8 @@ def browse_page(): facets=facets, prop_labels=find_more_props, label=title, + pager=pager, + page=page, labels=find_more_props, bindings=bindings, total=len(bindings), diff --git a/depicts/pager.py b/depicts/pager.py new file mode 100644 index 0000000..16846ed --- /dev/null +++ b/depicts/pager.py @@ -0,0 +1,47 @@ +from math import ceil +from flask import request, url_for + +class Pagination(object): + def __init__(self, page, per_page, total_count): + self.page = page + self.per_page = per_page + self.total_count = total_count + + @property + def pages(self): + return int(ceil(self.total_count / float(self.per_page))) + + @property + def has_prev(self): + return self.page > 1 + + @property + def has_next(self): + return self.page < self.pages + + def slice(self, items): + first = ((self.page - 1) * self.per_page) + last = self.page * self.per_page + return items[first:last] + + def iter_pages(self, left_edge=2, left_current=6, + right_current=6, right_edge=2): + last = 0 + for num in range(1, self.pages + 1): + if num <= left_edge or \ + (num > self.page - left_current - 1 and \ + num < self.page + right_current) or \ + num > self.pages - right_edge: + if last + 1 != num: + yield None + yield num + last = num + +def url_for_other_page(page): + args = request.view_args.copy() + args.update(request.args) + args['page'] = page + return url_for(request.endpoint, **args) + +def init_pager(app): + app.jinja_env.globals['url_for_other_page'] = url_for_other_page diff --git a/depicts/utils.py b/depicts/utils.py index 5b8d4b9..132386f 100644 --- a/depicts/utils.py +++ b/depicts/utils.py @@ -1,3 +1,4 @@ +from flask import request from itertools import islice import urllib.parse import inflect @@ -70,4 +71,6 @@ def wiki_url(title, site, ns=None): return f'https://{host}/wiki/' + url_ns + urllib.parse.quote(title.replace(' ', '_')) - +def get_int_arg(name): + if name in request.args and request.args[name].isdigit(): + return int(request.args[name]) diff --git a/templates/find_more.html b/templates/find_more.html index ea99304..5d9fc42 100644 --- a/templates/find_more.html +++ b/templates/find_more.html @@ -1,3 +1,4 @@ +{% from "macro.html" import render_pagination %} {% extends "base.html" %} {% block title %}{{ label }}{% endblock %} @@ -51,11 +52,7 @@ {% endfor %} -{# - {% for item in items %} -
{{ item | pprint }}
- {% endfor %} -#} +{{ render_pagination(pager) }} {% endblock %} diff --git a/templates/macro.html b/templates/macro.html new file mode 100644 index 0000000..faf8686 --- /dev/null +++ b/templates/macro.html @@ -0,0 +1,29 @@ +{% macro render_pagination(pagination) %} +{% if pagination.pages > 1 %} + +{% endif %} +{% endmacro %}