From 5b2147958b107c35f54c11c2d0b0b43a71855ad8 Mon Sep 17 00:00:00 2001 From: eric Date: Mon, 3 Nov 2014 19:36:26 -0500 Subject: [PATCH] paginate feeds --- api/opds.py | 45 +++++++++++++++++++++++++++++++++------------ api/views.py | 7 ++++++- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/api/opds.py b/api/opds.py index dfc784e6..ade9e561 100644 --- a/api/opds.py +++ b/api/opds.py @@ -19,6 +19,7 @@ FORMAT_TO_MIMETYPE = {'pdf':"application/pdf", facets = ["creative_commons","active_campaigns"] UNGLUEIT_URL= 'https://unglue.it' +NAVIGATION = "application/atom+xml;profile=opds-catalog;kind=navigation" def feeds(): for facet in facets: yield globals()[facet]() @@ -117,8 +118,8 @@ class Facet: feed_path = '' description = '' - def feed(self): - return opds_feed_for_works(self.works, self.feed_path, title=self.title) + def feed(self, page=None): + return opds_feed_for_works(self.works, self.feed_path, title=self.title, page=page) def updated(self): # return the creation date for most recently added item @@ -145,7 +146,7 @@ class active_campaigns(Facet): editions__ebooks__isnull=False).distinct().order_by('-created') description= "With your help we're raising money to make these books free to the world." -def opds_feed_for_works(works, feed_path, title="Unglue.it Catalog"): +def opds_feed_for_works(works, feed_path, title="Unglue.it Catalog", page=None): feed_xml = """ 0: + append_navlink(feed, 'previous', feed_path, page-1) + for work in works: node = work_node(work) feed.append(node) - return etree.tostring(feed, pretty_print=True) \ No newline at end of file + return etree.tostring(feed, pretty_print=True) + +def append_navlink(feed, rel, path, page): + link = etree.Element("link") + link.attrib.update({"rel":rel, + "href": UNGLUEIT_URL + "/api/opds/" + path + ('/?page=' + unicode(page) if page!=None else '/'), + "type": NAVIGATION, + }) + feed.append(link) \ No newline at end of file diff --git a/api/views.py b/api/views.py index fb3bd18d..b742f303 100755 --- a/api/views.py +++ b/api/views.py @@ -101,9 +101,14 @@ class OPDSAcquisitionView(View): def get(self, request, *args, **kwargs): facet = kwargs.get('facet') + page = request.GET.get('page', None) + try: + page = int(page) + except: + page = None if facet in opds.facets: facet_class = getattr(opds, facet)() - return HttpResponse(facet_class.feed(), + return HttpResponse(facet_class.feed(page), content_type="application/atom+xml;profile=opds-catalog;kind=acquisition") else: return HttpResponseNotFound("invalid facet")