From 8f3bf4191a58b408e8d27b79fb2c9de0d08146ca Mon Sep 17 00:00:00 2001 From: eric Date: Wed, 16 Jul 2014 21:00:16 -0700 Subject: [PATCH] make the feeds into classes --- api/opds.py | 35 ++++++++++++++++++++++------------- api/templates/opds.xml | 21 ++++++++------------- api/views.py | 6 +++--- 3 files changed, 33 insertions(+), 29 deletions(-) diff --git a/api/opds.py b/api/opds.py index 8f7aad7a..a6e0de7a 100644 --- a/api/opds.py +++ b/api/opds.py @@ -17,6 +17,9 @@ FORMAT_TO_MIMETYPE = {'pdf':"application/pdf", 'html':"text/html", 'text':"text/html"} facets = ["creative_commons","active_campaigns"] +def feeds(): + for facet in facets: + yield globals()[facet]() def text_node(tag, text): node = etree.Element(tag) @@ -110,24 +113,30 @@ def work_node(work, domain="unglue.it", protocol="https"): return node -def creative_commons(domain="unglue.it", protocol="https"): +class Facet: + title = '' + works = None + feed_path = '' + description = '' + + def feed(self): + return opds_feed_for_works(self.works, self.feed_path, title=self.title, domain="unglue.it", protocol="https") - licenses = cc.LICENSE_LIST - ccworks = models.Work.objects.filter(editions__ebooks__isnull=False, - editions__ebooks__rights__in=licenses).distinct().order_by('-created') - - return opds_feed_for_works(ccworks, "creative_commons", "Unglue.it Catalog: Creative Commons Books", - domain, protocol) +class creative_commons(Facet): + title = "Unglue.it Catalog: Creative Commons Books" + feed_path = "creative_commons" + works = models.Work.objects.filter(editions__ebooks__isnull=False, + editions__ebooks__rights__in=cc.LICENSE_LIST).distinct().order_by('-created') + description= "These Creative Commons licensed ebooks are ready to read - the people who created them want you to read and share them." -def active_campaigns(domain="unglue.it", protocol="https"): +class active_campaigns(Facet): """ return opds feed for works associated with active campaigns """ - # campaigns = models.Campaign.objects.filter(status='ACTIVE').order_by('deadline') - campaign_works = models.Work.objects.filter(campaigns__status='ACTIVE') - return opds_feed_for_works(campaign_works, "active_campaigns", - "Unglue.it Catalog: Books under Active Campaign", - domain, protocol) + title = "Unglue.it Catalog: Books under Active Campaign" + feed_path = "active_campaigns" + works = models.Work.objects.filter(campaigns__status='ACTIVE') + 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", domain="unglue.it", protocol="https"): diff --git a/api/templates/opds.xml b/api/templates/opds.xml index b88bf681..c7718b2c 100644 --- a/api/templates/opds.xml +++ b/api/templates/opds.xml @@ -3,25 +3,20 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.kbcafe.com/rss/atom.xsd.xml"> Unglue.it Catalog - https://unglue.it/opds + https://unglue.it/api/opds/ 2014-06-13T21:48:34Z unglue.it https://unglue.it - + +{% for feed in feeds %} - Creative Commons - https://unglue.it/creativecommons/ + {{ feed.title }} + https://unglue.it/{{ feed.feed_path }}/ 2014-06-13T00:00:00Z - - These Creative Commons licensed ebooks are ready to read - the people who created them want you to read and share them.. - - - Active Campaigns - https://unglue.it/campaigns/ending#2 - 2014-06-13T00:00:00Z - - With your help we're raising money to make these books free to the world. + + +{% endfor %} \ No newline at end of file diff --git a/api/views.py b/api/views.py index 4d47477e..fb3bd18d 100755 --- a/api/views.py +++ b/api/views.py @@ -94,7 +94,7 @@ class OPDSNavigationView(TemplateView): def get_context_data(self, **kwargs): context = super(OPDSNavigationView, self).get_context_data(**kwargs) - context["opds"] = opds + context["feeds"] = opds.feeds() return context class OPDSAcquisitionView(View): @@ -102,8 +102,8 @@ class OPDSAcquisitionView(View): def get(self, request, *args, **kwargs): facet = kwargs.get('facet') if facet in opds.facets: - facet_method = getattr(opds, facet) - return HttpResponse(facet_method(), + facet_class = getattr(opds, facet)() + return HttpResponse(facet_class.feed(), content_type="application/atom+xml;profile=opds-catalog;kind=acquisition") else: return HttpResponseNotFound("invalid facet")