diff --git a/app.py b/app.py index cf85123..9f6b722 100755 --- a/app.py +++ b/app.py @@ -87,6 +87,95 @@ def static(filename): return bottle.static_file(filename, root='./static/') +@bottle.route('/library', method=['GET', 'POST'], name='library') +def library(): + """ + Library index + """ + + # Get a list of authors for searching + authors = query(""" + PREFIX library: + PREFIX license: + + SELECT DISTINCT ?name + WHERE { + [] library:author ?name + } + ORDER BY ?name + """)['results']['bindings'] + + # Get a list of licenses for searching + licenses = query(""" + PREFIX library: + PREFIX license: + + SELECT DISTINCT ?id + WHERE { + [] library:license [ + license:id ?id + ] . + } + ORDER BY ?id + """)['results']['bindings'] + + # Retrieve filters selected by the user + filters_author = [] + filters_license = [] + query_filters = '' + if request.method == 'POST': + filters_author = request.forms.getall('author') + filters_license = request.forms.getall('license') + + if len(filters_author) > 0: + query_filters_author = ','.join([ '"'+i.replace('"', '\\"')+'"' for i in filters_author ]) + query_filters += f'FILTER(?author IN ({query_filters_author}))' + + if len(filters_license) > 0: + query_filters_license = ','.join([ '"'+i.replace('"', '\\"')+'"' for i in filters_license ]) + query_filters += f'FILTER(?license_id IN ({query_filters_license}))' + + items = query(f""" + PREFIX library: + PREFIX license: + + CONSTRUCT {{ + ?item library:title ?title; + library:author ?author ; + library:license ?license . + ?license license:id ?license_id ; + license:name ?license_name . + }} + WHERE {{ + ?item library:title ?title ; + library:author ?author ; + library:license ?license . + + OPTIONAL {{ + ?license license:id ?license_id_optional ; + license:name ?license_name_optional . + }} + + BIND(COALESCE(?license_id_optional, SUBSTR(STR(?license), 14)) AS ?license_id) + BIND(COALESCE(?license_name_optional, SUBSTR(STR(?license), 14)) AS ?license_name) + + {query_filters} + }} + ORDER BY UCASE(?title) + """, + { + '@context': { + 'library': 'dokk:library:', + 'license': 'dokk:license:', + 'library:author': { '@container': '@set' }, + 'library:license': { '@container': '@set' } + }, + 'library:title': {} + }) + + return template('library.html', authors=authors, licenses=licenses, items=items, + filters_author=filters_author, filters_license=filters_license) + @bottle.get('/library/opds.xml', name='library_opds') def library_opds(): """ @@ -329,6 +418,7 @@ def articles(): return template('templates/articles.tpl', pages=pages) +# TODO Make this obsolete. Replace with controllers @bottle.get('/', name='homepage') @bottle.get('/', name='page') def article(page=''): diff --git a/pages/library.html b/pages/library.html index 9b23f71..570f7d1 100644 --- a/pages/library.html +++ b/pages/library.html @@ -2,43 +2,6 @@ {% block title %}Library{% endblock %} -{% set data = query(""" - PREFIX library: - PREFIX license: - - CONSTRUCT { - ?item library:title ?title; - library:author ?author ; - library:license ?license . - ?license license:id ?license_id ; - license:name ?license_name . - } - WHERE { - ?item library:title ?title ; - library:author ?author ; - library:license ?license . - - OPTIONAL { - ?license license:id ?license_id_optional ; - license:name ?license_name_optional . - } - - BIND(COALESCE(?license_id_optional, SUBSTR(STR(?license), 14)) AS ?license_id) - BIND(COALESCE(?license_name_optional, SUBSTR(STR(?license), 14)) AS ?license_name) - } - ORDER BY UCASE(?title) - """, - { - '@context': { - 'library': 'dokk:library:', - 'license': 'dokk:license:', - 'library:author': { '@container': '@set' }, - 'library:license': { '@container': '@set' } - }, - 'library:title': {} - }) -%} - {% block body %}
@@ -46,7 +9,36 @@ The library is also available via OPDS. Add this link to your OPDS client: https://dokk.org/library/opds.xml


-{% for item in data["@graph"]|sort(attribute="library:title") %} +
+ Filters + +
+
+ + Author: +
+ + +

+ + License: +
+ + +

+ +
+
+ +{% for item in items["@graph"]|sort(attribute="library:title") %}