Skip to content

Commit

Permalink
Book list view custom paginator
Browse files Browse the repository at this point in the history
  • Loading branch information
manuq committed Apr 13, 2011
1 parent 8e7cc84 commit 24782ba
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 31 deletions.
20 changes: 14 additions & 6 deletions books/opds.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,20 @@ def generate_catalog(books, q=None):
linklist = [{'rel': \
'http://opds-spec.org/acquisition', 'href': \
book.book_file.url, 'type': __get_mimetype(book)}]

feed.add_item(book.a_id, book.a_title, book.a_updated, \
content=book.a_summary, links = linklist, \
authors = [{'name' : book.a_author}], \
dc_language=book.dc_language.code, dc_publisher=book.dc_publisher, \
dc_issued=book.dc_issued, dc_identifier=book.dc_identifier)

add_kwargs = {
'content': book.a_summary,
'links': linklist,
'authors': [{'name' : book.a_author}],
'dc_publisher': book.dc_publisher,
'dc_issued': book.dc_issued,
'dc_identifier': book.dc_identifier,
}

if book.dc_language is not None:
add_kwargs['dc_language'] = book.dc_language.code

feed.add_item(book.a_id, book.a_title, book.a_updated, **add_kwargs)

s = StringIO()
feed.write(s, 'UTF-8')
Expand Down
11 changes: 6 additions & 5 deletions books/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
});
</script>
<div class="header">
<a href="{% url pathagar.books.views.book_list %}"><h1><em>Pathagar</em> Book Server</h1></a>
<a href="{% url pathagar.books.views.latest %}"><h1><em>Pathagar</em> Book Server</h1></a>
<form
{% if list_by %}
{% if list_by == 'by-title' %}
Expand All @@ -45,12 +45,12 @@
action="{% url pathagar.books.views.by_author %}"
{% endif %}
{% else %}
action="{% url pathagar.books.views.book_list %}"
action="{% url pathagar.books.views.latest %}"
{% endif %}
method="get">

<ul class="navbar">
<li><a data-name="latest" href="{% url pathagar.books.views.book_list %}">Latest</a></li>
<li><a data-name="latest" href="{% url pathagar.books.views.latest %}">Latest</a></li>
<li><a data-name="by-title" href="{% url pathagar.books.views.by_title %}">By Title</a></li>
<li><a data-name="by-author" href="{% url pathagar.books.views.by_author %}">By Author</a></li>
<li class="searchbox">
Expand All @@ -74,6 +74,7 @@
</li>
</ul>
</li>
<li><a href="./feed.atom">Subscribe to Feed</a></li>
</ul>
</form>

Expand All @@ -91,13 +92,13 @@
{% block footer %}{% endblock %}
<div class="span-12 prepend-6 footer">
<small>
This is the <em>Pathagar</em> book server. A list of books in the server can be found <a href="{% url pathagar.books.views.book_list %}">here</a>.
This is the <em>Pathagar</em> book server. A list of books in the server can be found <a href="{% url pathagar.books.views.latest %}">here</a>.
{% if user.is_authenticated %}
<br>
Books can be added to this server via this <a href="{% url pathagar.books.views.add_book %}">page</a>.
{% endif %}
<br>
An <acronym title="Open Publication Distribution System">OPDS</acronym> feed is also <a href="{% url pathagar.books.views.catalogs %}">available</a>.
An <acronym title="Open Publication Distribution System">OPDS</acronym> feed is also <a href="{% url pathagar.books.views.latest_atom %}">available</a>.
</small>
</div>
<div>
Expand Down
39 changes: 26 additions & 13 deletions books/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from django.views.generic.list_detail import object_list, object_detail
from django.views.generic.create_update import create_object, update_object, \
delete_object
from django.template import RequestContext

from django.conf import settings

Expand All @@ -37,9 +38,6 @@
from models import *
from popuphandler import handlePopAdd

def catalogs(request):
return HttpResponse(get_catalog(request), mimetype='application/atom+xml')

@login_required
def add_language(request):
return handlePopAdd(request, AddLanguageForm, 'language')
Expand Down Expand Up @@ -87,21 +85,36 @@ def _book_list(request, queryset, list_by='latest'):
search_title, search_author)

all_books = Book.objects.all()
extra_context = {'total_books': len(all_books), 'q': q,
'search_all': search_all, 'search_title': search_title,
'search_author': search_author, 'list_by': list_by}
return object_list(
request,
queryset = queryset,
paginate_by = settings.ITEMS_PER_PAGE,
template_object_name = 'book',
extra_context = extra_context,

paginator = Paginator(queryset, settings.ITEMS_PER_PAGE)
page = int(request.GET.get('page', '1'))

try:
page_obj = paginator.page(page)
except (EmptyPage, InvalidPage):
page_obj = paginator.page(paginator.num_pages)

extra_context = {
'book_list': page_obj.object_list,
'total_books': len(all_books), 'q': q,
'paginator': paginator,
'page_obj': page_obj,
'search_all': search_all, 'search_title': search_title,
'search_author': search_author, 'list_by': list_by,
}
return render_to_response(
'books/book_list.html',
extra_context,
context_instance = RequestContext(request),
)

def book_list(request):
def latest(request):
queryset = Book.objects.all()
return _book_list(request, queryset, list_by='latest')

def latest_atom(request):
return HttpResponse(get_catalog(request), mimetype='application/atom+xml')

def by_title(request):
queryset = Book.objects.all().order_by('a_title')
return _book_list(request, queryset, list_by='by-title')
Expand Down
7 changes: 4 additions & 3 deletions static_media/style/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ a[href$=".epub"] {

.header h1 {
color: white;
size: 120%;
font-size: 200%;
font-weight: bold;
position: relative;
top: 20px;
Expand All @@ -43,7 +43,7 @@ a[href$=".epub"] {

.header .navbar {
position: absolute;
right: 35px;
right: 0px;
top: 25px;
}

Expand Down Expand Up @@ -113,7 +113,7 @@ a[href$=".epub"] {
color: white;
background-color: gray;
position: absolute;
right: 0;
right: 185px;
top: 35px;
width: 150px;
-moz-border-radius-bottomright: 10px;
Expand Down Expand Up @@ -143,6 +143,7 @@ div.hidden_body {
}

.bookname a {
display: inline-block;
margin-left: -0.4em;
padding: 0.2em 0.4em;
-moz-border-radius: 10px;
Expand Down
12 changes: 8 additions & 4 deletions urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,24 @@

urlpatterns = patterns('',

# Index page:
(r'^$', 'pathagar.books.views.book_list'),
# Book list:
(r'^$', 'pathagar.books.views.latest'),
(r'^by-title/$', 'pathagar.books.views.by_title'),
(r'^by-author/$', 'pathagar.books.views.by_author'),
(r'^tags/(?P<tag>[-\w]+)/$', 'pathagar.books.views.book_list_tag'),

# Book list Atom:
(r'^feed.atom', 'pathagar.books.views.latest_atom'),
# (r'^by-title/feed.atom$', 'pathagar.books.views.by_title_atom'),
# (r'^by-author/feed.atom$', 'pathagar.books.views.by_author_atom'),
# (r'^tags/(?P<tag>[-\w]+)/feed.atom$', 'pathagar.books.views.book_list_tag_atom'),

# Add, view, edit and remove books:
(r'^add/book/?$', 'pathagar.books.views.add_book'),
(r'^view/book/(?P<book_id>\d+)/$', 'pathagar.books.views.book_detail'),
(r'^edit/book/(?P<book_id>\d+)/?$', 'pathagar.books.views.edit_book'),
(r'^remove/book/(?P<book_id>\d+)/?$', 'pathagar.books.views.remove_book'),

# Atom books catalogs:
(r'^catalogs/', 'pathagar.books.views.catalogs'),

# Add language:
(r'^add/dc_language|language/?$', 'pathagar.books.views.add_language'),
Expand Down

0 comments on commit 24782ba

Please sign in to comment.