Merge pull request #848 from Gluejar/onix_pagination

from krigar1184:works_pagination
pull/94/head
eshellman 2019-08-03 22:15:03 -04:00 committed by GitHub
commit 3971286366
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 46 additions and 21 deletions

View File

@ -10,7 +10,7 @@ contains four main applications: `core`, `frontend`, `api` and `payment` that ca
and configured on as many ec2 instances that are needed to support traffic.
The partitioning between these modules is not as clean as would be ideal. `payment` is particularly messy because we had to retool it twice because we had to switch from Paypal to Amazon Payments to Stripe.
regluit was originally developed on Django 1.3 (python 2.7) and currently runs on Django 1.8.
regluit was originally developed on Django 1.3 (python 2.7) and currently runs on Django 1.11.
Develop
-------

View File

@ -2,6 +2,9 @@ import datetime
import pytz
import re
from lxml import etree
from django.core.paginator import Paginator, InvalidPage
from regluit.core import models
from regluit.core.cc import ccinfo
from regluit.bisac import Bisac
@ -10,16 +13,25 @@ feed_xml = """<?xml version="1.0" encoding="UTF-8"?>
<ONIXMessage release="3.0" xmlns="http://ns.editeur.org/onix/3.0/reference" />
"""
bisac = Bisac()
WORKS_PER_PAGE = 30
def text_node(tag, text, attrib={}):
node = etree.Element(tag, attrib=attrib)
node.text = text
return node
def onix_feed(facet, max=None):
def onix_feed(facet, max=None, page_number=None):
feed = etree.fromstring(feed_xml)
feed.append(header(facet))
works = facet.works[0:max] if max else facet.works
if page_number is not None:
try:
p = Paginator(works, WORKS_PER_PAGE)
works = p.page(page_number)
except InvalidPage:
works = []
for work in works:
editions = models.Edition.objects.filter(work=work,ebooks__isnull=False)
editions = facet.facet_object.filter_model("Edition",editions).distinct()

View File

@ -89,11 +89,13 @@ XML: <a href="/api/v1/identifier/?format=xml&amp;api_key={{api_key}}&amp;usernam
<dd><code><a href="{% url 'opds_acqusition' 'kw.fiction' %}">{{base_url}}{% url 'opds_acqusition' 'kw.fiction' %}</a></code></dd>
<dt>filtered by ungluer</dt>
<dd><code><a href="{% url 'opds_acqusition' '@eric' %}">{{base_url}}{% url 'opds_acqusition' '@eric' %}</a></code></dd>
<dt>filtered by having a Project Gutenberg or DOAB identifier (doab, gtbg)</dt>
<dd><code><a href="{% url 'opds_acqusition' 'doab/-gtbg' %}">{{base_url}}{% url 'opds_acqusition' 'doab/-gtbg' %}</a></code></dd>
</p>
<p>There's also an OPDS record available for every work on unglue.it. For example, requesting, <code><a href="{% url 'opds_acqusition' 'all'%}?work=13950">{{base_url}}{% url 'opds_acqusition' 'all'%}?work=13950</a></code> get you to the web page or opds record for <i>A Christmas Carol</i>.</p>
<h3>ONIX Catalog Feeds</h3>
<p>There is an <a href="http://www.editeur.org/12/about-release-3.0/">ONIX 3.0</a> feed corresponding to every facet of our <a href="{% url 'free' %}">free ebook lists</a>. You don't need a key to use them. There is a maximum of 100 books per result you can change with the max parameter. For example, here are the <a href="{% url 'onix' 'by-nc-nd/epub' %}">first hundred CC BY-ND-ND licensed books available in EPUB.</a></p>
<p>There is an <a href="http://www.editeur.org/12/about-release-3.0/">ONIX 3.0</a> feed corresponding to every facet of our <a href="{% url 'free' %}">free ebook lists</a>. You don't need a key to use them. There is a maximum of 100 books per result you can change with the <code>max</code> parameter. For example, here are the <a href="{% url 'onix' 'by-nc-nd/epub' %}?max=20">first twenty CC BY-ND-ND licensed books available in EPUB.</a> Pages of 30 records each are available via the <code>page</code> parameter. Here's the <a href="{% url 'onix' 'doab' %}?page=1">first page of books from the Directory of Open Access Books.</a></p>
<p>There's also an ONIX record available for every free ebook on unglue.it. For example, here is <a href="{% url 'onix_all' %}?work=140086"><i>Issues in Open Research Data</i></a>.</p>
<h3>Identifiers with Content type negotiation</h3>

View File

@ -216,24 +216,35 @@ class OPDSAcquisitionView(View):
class OnixView(View):
def get(self, request, *args, **kwargs):
work = request.GET.get('work', None)
if work:
try:
work=models.safe_get_work(work)
work = models.safe_get_work(work)
except models.Work.DoesNotExist:
raise Http404
return HttpResponse(onix.onix_feed_for_work(work),
content_type="text/xml")
facet = kwargs.get('facet', 'all')
if facet:
max = request.GET.get('max', 100)
try:
max = int(max)
except:
max = None
facet_class = opds.get_facet_class(facet)()
return HttpResponse(onix.onix_feed(facet_class, max),
content_type="text/xml")
return HttpResponse(onix.onix_feed_for_work(work), content_type="text/xml")
facet = kwargs.get('facet', 'all')
if not facet:
return HttpResponseBadRequest(content='No facet provided')
max_records = request.GET.get('max', 100)
try:
max_records = int(max_records)
except Exception:
max_records = None
facet_class = opds.get_facet_class(facet)()
page = request.GET.get('page', None)
try:
page = int(page)
except:
page = None
feed = onix.onix_feed(facet_class, max_records, page_number=page)
return HttpResponse(feed, content_type="text/xml")