From b356699c9b61d72d9a5fd3db86d525482ddf47d9 Mon Sep 17 00:00:00 2001 From: Raymond Yee Date: Wed, 25 Jun 2014 18:05:57 -0700 Subject: [PATCH] First pass at /opds/{facet} --> now with active_campaigns too --- core/opds.py | 40 +++++++++++++++++++++-------- frontend/urls.py | 2 +- frontend/views.py | 13 +++++++++- notebooks/opds.ipynb | 61 ++++++++++++++++++++++++++++++++++++-------- 4 files changed, 92 insertions(+), 24 deletions(-) diff --git a/core/opds.py b/core/opds.py index 5e2c2be0..1dd20705 100644 --- a/core/opds.py +++ b/core/opds.py @@ -32,7 +32,9 @@ def map_to_domain(url, domain="unglue.it", protocol="https"): return urlparse.urlunparse(m) def add_query_component(url, qc): - """ """ + """ + add component qc to the querystring of url + """ m = list(urlparse.urlparse(url)) if len(m[4]): m[4] = "&".join([m[4],qc]) @@ -107,7 +109,26 @@ def work_node(work, domain="unglue.it", protocol="https"): return node -def creativecommons(domain="unglue.it", protocol="https"): +def creative_commons(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) + +def active_campaigns(domain="unglue.it", protocol="https"): + """ + 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) + +def opds_feed_for_works(works, feed_path, title="Unglue.it Catalog", domain="unglue.it", protocol="https"): feed_xml = """[^/]+)/$", OPDSAcquisitionView.as_view(), name="opds_acqusition") ) if settings.DEBUG: diff --git a/frontend/views.py b/frontend/views.py index 5034871a..3e6df0d9 100755 --- a/frontend/views.py +++ b/frontend/views.py @@ -3195,5 +3195,16 @@ class OPDSAcquisitionView(View): else: protocol = "https" - return HttpResponse(opds.creativecommons(domain=site.domain, protocol=protocol), + logger.info("request.path: {0}".format(request.path)) + logger.info("facet: {0}".format(kwargs.get('facet'))) + + facet = kwargs.get('facet') + if facet == 'creative_commons': + return HttpResponse(opds.creative_commons(domain=site.domain, protocol=protocol), content_type="application/atom+xml;profile=opds-catalog;kind=acquisition") + elif facet == 'active_campaigns': + return HttpResponse(opds.active_campaigns(domain=site.domain, protocol=protocol), + content_type="application/atom+xml;profile=opds-catalog;kind=acquisition") + else: + return HttpResponseNotFound("invalid facet") + diff --git a/notebooks/opds.ipynb b/notebooks/opds.ipynb index 4c649369..fa52d4f7 100644 --- a/notebooks/opds.ipynb +++ b/notebooks/opds.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:6bd6c988240f3f5084034a9aca36b1e1f5de3799a7b1a46c9624e8de724010f2" + "signature": "sha256:9c22749755ac63be304b4d1975df24fa2f5ae6afb32dd56374a060df17645c96" }, "nbformat": 3, "nbformat_minor": 0, @@ -657,16 +657,6 @@ "Tacking on a query component to a URL" ] }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "urlli" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, { "cell_type": "code", "collapsed": false, @@ -697,6 +687,55 @@ ], "prompt_number": 30 }, + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Getting works of active campaigns " + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "campaigns = models.Campaign.objects.filter(status='ACTIVE').order_by('deadline')\n", + "campaigns" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 32, + "text": [ + "[, , , , , , , , , , , ]" + ] + } + ], + "prompt_number": 32 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "models.Work.objects.filter(campaigns__status='ACTIVE')" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 34, + "text": [ + "[, , , , , , , , , , , ]" + ] + } + ], + "prompt_number": 34 + }, { "cell_type": "heading", "level": 1,