Skip to content

Commit

Permalink
Query string fix for feeds
Browse files Browse the repository at this point in the history
  • Loading branch information
manuq committed Apr 13, 2011
1 parent 1e60531 commit 5c434ba
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
1 change: 0 additions & 1 deletion TODO
@@ -1,7 +1,6 @@
TODO
----

* pagination not working in feed
* tag cloud page
* "by tag" menu item links to tag cloud page when no tag
* since tags added, edit is broken in site (not in admin)
Expand Down
34 changes: 28 additions & 6 deletions books/opds.py
Expand Up @@ -27,22 +27,44 @@ def __get_mimetype(item):
else:
return 'Unknown'

def generate_catalog(page_obj, qstring, q=None):
def page_qstring(request, page_number=None):
"""
Return the query string for the URL.
If page_number is given, modify the query for that page.
"""
qdict = dict(request.GET.items())
if page_number is not None:
qdict['page'] = str(page_number)

if len(qdict) > 0:
qstring = '?'+'&'.join(('%s=%s' % (k, v) for k, v in qdict.items()))
else:
qstring = ''

return qstring


def generate_catalog(request, page_obj):
attrs = {}
attrs[u'xmlns:dcterms'] = u'http://purl.org/dc/terms/'
attrs[u'xmlns:opds'] = u'http://opds-spec.org/'
attrs[u'xmlns:dc'] = u'http://purl.org/dc/elements/1.1/'
attrs[u'xmlns:opensearch'] = 'http://a9.com/-/spec/opensearch/1.1/'

links = []

if page_obj.has_previous():
previous_page = page_obj.previous_page_number()
links.append({'title': 'Previous results', 'type': 'application/atom+xml',
'rel': 'previous','href': qstring})

'rel': 'previous',
'href': page_qstring(request, previous_page)})

if page_obj.has_next():
next_page = page_obj.next_page_number()
links.append({'title': 'Next results', 'type': 'application/atom+xml',
'rel': 'next', 'href': qstring})
'rel': 'next',
'href': page_qstring(request, next_page)})

feed = AtomFeed(title = 'Pathagar Bookserver OPDS feed', \
atom_id = 'pathagar:full-catalog', subtitle = \
Expand Down
13 changes: 7 additions & 6 deletions books/views.py
Expand Up @@ -37,7 +37,7 @@
from langlist import langs as LANG_CHOICES
from models import *
from popuphandler import handlePopAdd
from opds import generate_catalog
from opds import page_qstring, generate_catalog

@login_required
def add_language(request):
Expand Down Expand Up @@ -79,9 +79,13 @@ def _book_list(request, queryset, qtype=None, list_by='latest', **kwargs):
search_title = request.GET.get('search-title') == 'on'
search_author = request.GET.get('search-author') == 'on'

# If no search options are specified, assumes search all, the
# advanced search will be used:
if not search_all and not search_title and not search_author:
search_all = True

# If search queried, modify the queryset with the result of the
# search:
if q is not None:
if search_all:
queryset = advanced_search(queryset, q)
Expand All @@ -100,14 +104,11 @@ def _book_list(request, queryset, qtype=None, list_by='latest', **kwargs):
page_obj = paginator.page(paginator.num_pages)

# Build the query string:
if len(request.GET) > 0:
qstring = '?'+'&'.join(('%s=%s' % (k, v) for k, v in request.GET.items()))
else:
qstring = ''
qstring = page_qstring(request)

# Return OPDS Atom Feed:
if qtype == 'feed':
catalog = generate_catalog(page_obj, qstring, q)
catalog = generate_catalog(request, page_obj)
return HttpResponse(catalog, mimetype='application/atom+xml')

# Return HTML page:
Expand Down

0 comments on commit 5c434ba

Please sign in to comment.