diff options
-rw-r--r-- | app/books/models.py | 14 | ||||
-rw-r--r-- | app/books/retriever.py | 19 | ||||
-rw-r--r-- | app/books/urls.py | 27 | ||||
-rw-r--r-- | app/books/views.py | 44 | ||||
-rw-r--r-- | app/utils/views.py | 1 | ||||
-rw-r--r-- | config/base_urls.py | 25 | ||||
-rw-r--r-- | design/templates/archives/books.html | 2 | ||||
-rw-r--r-- | design/templates/details/book.html | 2 |
8 files changed, 77 insertions, 57 deletions
diff --git a/app/books/models.py b/app/books/models.py index 1464c78..f5a4a04 100644 --- a/app/books/models.py +++ b/app/books/models.py @@ -1,12 +1,13 @@ from django.db import models +from django.core.urlresolvers import reverse from django.utils.encoding import force_text from django.conf import settings from django.template.defaultfilters import slugify -from taggit.managers import TaggableManager # http://freewisdom.org/projects/python-markdown/ import markdown + class Book(models.Model): title = models.CharField(max_length=200) author_name = models.CharField(max_length=200) @@ -39,14 +40,18 @@ class Book(models.Model): return self.title def get_absolute_url(self): - return "/books/%s" % (self.slug) + return reverse("books:detail", kwargs={"slug": self.slug}) def get_image_url(self): return '%sbook-covers/%s.jpg' % (settings.IMAGES_URL, self.slug) - def get_rating (self): + def get_rating(self): return int(self.rating) + @property + def ratings_range(cls): + return range(1, 6) + def admin_thumbnail(self): return force_text('<a href=""><img src="%s" width="100" style="width:100px"></a>' % (self.get_image_url())) admin_thumbnail.allow_tags = True @@ -58,12 +63,11 @@ class Book(models.Model): self.slug = slugify(self.title[:50]) super(Book, self).save() -#class Book class BookHighlight(models.Model): book = models.ForeignKey(Book) page = models.PositiveSmallIntegerField(null=True) - location = models.CharField(max_length=200, blank=True, null=True) + location = models.CharField(max_length=200, blank=True, null=True) date_added = models.DateTimeField() body_markdown = models.TextField() diff --git a/app/books/retriever.py b/app/books/retriever.py index e98f46f..302009f 100644 --- a/app/books/retriever.py +++ b/app/books/retriever.py @@ -1,6 +1,12 @@ +import io import requests +import json +from django.conf import settings + + url = "https://www.googleapis.com/books/v1/volumes?q=isbn:%s&key=AIzaSyCCYn5v58R08msS06pRfOzTHANUuG2tSHI" % (book.isbn) + def get_book_isbn(book): print(book.title) if book.author_name != "Instapaper": @@ -20,7 +26,7 @@ def get_book_isbn(book): return isbn10 break -import json + def get_book_data(book): if book.author_name != "Instapaper": url = "https://openlibrary.org/api/books?bibkeys=ISBN:%s&format=json&jscmd=data" % book.isbn @@ -29,19 +35,19 @@ def get_book_data(book): obj = "ISBN:%s" % book.isbn try: pages = j[obj]['number_of_pages'] - except: + except: pages = '' try: publish_date = j[obj]['publish_date'] - except: + except: publish_date = '' try: publish_places = j[obj]['publish_places'][0]['name'] - except: + except: publish_places = '' try: openlib_url = j[obj]['url'] - except: + except: openlib_url = '' print("pages " + str(pages)) print("date " + publish_date) @@ -53,8 +59,9 @@ def get_book_data(book): book.openlib_url = openlib_url book.save() + def fetch_image(book): - path = '%s/%s/%s.jpg' %(settings.IMAGES_ROOT, 'book-covers', book.slug) + path = '%s/%s/%s.jpg' % (settings.IMAGES_ROOT, 'book-covers', book.slug) r = requests.get(book.image, timeout=6.001) im = io.StringIO(r.content) # constructs a StringIO holding the image im.save(path) diff --git a/app/books/urls.py b/app/books/urls.py index 3efd730..6c172aa 100644 --- a/app/books/urls.py +++ b/app/books/urls.py @@ -1,9 +1,22 @@ -from django.conf.urls import * +from django.conf.urls import url from django.views.generic.base import RedirectView -urlpatterns = patterns('', - #url(r'(?P<slug>[-\w]+)/(?P<page>\d+)/$', 'blog.views.entry_list_by_area'), - url(r'(?P<page>\d+)/$', 'books.views.book_list'), - url(r'(?P<slug>[-\w]+)/$', 'books.views.book_detail'), - url(r'^$', RedirectView.as_view(url="/books/1/", permanent=False)), -) +from . import views + +urlpatterns = [ + url( + regex=r'(?P<page>\d+)/$', + view=views.BookListView.as_view(), + name='list', + ), + url( + regex=r'(?P<slug>[-\w]+)/$', + view=views.BookDetailView.as_view(), + name='detail', + ), + url( + regex=r'^$', + view=RedirectView.as_view(url="/books/1/", permanent=False), + name="live-redirect" + ), +] diff --git a/app/books/views.py b/app/books/views.py index 21f271f..ef1d5f4 100644 --- a/app/books/views.py +++ b/app/books/views.py @@ -1,37 +1,17 @@ -from django.shortcuts import render_to_response, get_object_or_404 -from django.template import RequestContext -from django.http import Http404 -from django.conf import settings -# from django.views.generic import ListView +from django.views.generic.detail import DetailView +from utils.views import PaginatedListView -from books.models import Book -from locations.models import Region, Country -from photos.models import Photo +from .models import Book +class BookListView(PaginatedListView): + template_name = 'archives/books.html' -def book_detail(request, slug): - context = { - 'object': get_object_or_404(Book, slug__exact=slug), - 'ratings_range' : range(1, 6) - } - return render_to_response('details/book.html', context, context_instance=RequestContext(request)) + def get_queryset(self): + return Book.objects.all().order_by('-read_date').select_related() -def book_list(request, page): - request.page_url = '/books/%d/' - request.page = int(page) - try: - is_build = request.POST['builder'] - extra_context={ - 'page':page, - 'MEDIA_URL': settings.BAKED_MEDIA_URL, - 'ratings_range': range(1, 6) - } - except: - extra_context={'page':page} - context = { - 'object_list': Book.objects.order_by('-read_date').select_related(), - 'page': page, - 'ratings_range' : range(1, 6) - } - return render_to_response("archives/books.html", context, context_instance=RequestContext(request)) + +class BookDetailView(DetailView): + model = Book + template_name = "details/book.html" + slug_field = "slug" diff --git a/app/utils/views.py b/app/utils/views.py index 910f9b3..f28a8cd 100644 --- a/app/utils/views.py +++ b/app/utils/views.py @@ -14,5 +14,6 @@ class PaginatedListView(ListView): request.page_url = "/" + path + '/%d/' else: request.page_url = request.path + '%d/' + print(request.page_url) request.page = int(self.kwargs['page']) return super(PaginatedListView, self).dispatch(request, *args, **kwargs) diff --git a/config/base_urls.py b/config/base_urls.py index c47f5b5..e5aa79d 100644 --- a/config/base_urls.py +++ b/config/base_urls.py @@ -46,15 +46,30 @@ urlpatterns += patterns('', (r'^googleb11655cd59dacf3c.html$', TemplateView.as_view(template_name='static/gverify.html')), #(r'^contact/', TemplateView.as_view(template_name='details/contact.html')), (r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}), - (r'^jrnl/', include('jrnl.urls', namespace='jrnl')), + url( + regex=r'^jrnl/', + view=include('jrnl.urls', namespace='jrnl') + ), (r'^projects/', include('projects.urls')), (r'^locations/', include('locations.urls')), (r'^expenses/', include('expenses.urls')), (r'^photos/', include('photos.urls')), - (r'^books/', include('books.urls')), - (r'^field-notes/', include('notes.urls')), - (r'^photo/', include('photos.detail_urls')), - (r'^birds/', include('birds.urls', namespace='birds')), + url( + regex=r'^books/', + view=include('books.urls', namespace='books') + ), + url( + regex=r'^field-notes/', + view=include('notes.urls', namespace='notes') + ), + url( + regex=r'^photo/', + view=include('photos.detail_urls', namespace='photos') + ), + url( + regex=r'^birds/', + view=include('birds.urls', namespace='birds') + ), (r'^travel-guide/', include('guide.urls')), (r'^src/', include('src.urls', namespace='src')), (r'^figments/', include('figments.urls')), diff --git a/design/templates/archives/books.html b/design/templates/archives/books.html index 980a4b7..1c139e6 100644 --- a/design/templates/archives/books.html +++ b/design/templates/archives/books.html @@ -25,7 +25,7 @@ <h2 itemprop="name"><a href="{{object.get_absolute_url}}">{{object.title|amp|smartypants|widont|safe}}</a></h2> <h4 itemprop="author">{{object.author_name}}</h4> <div itemprop="review" itemscope itemtype="http://schema.org/Review"> - {% if object.rating %}<span class="book-stars" itemprop="reviewRating">{% for i in ratings_range %}{% if i <= object.get_rating%}★{%else%}☆{%endif%}{%endfor%}</span>{%endif%} + {% if object.rating %}<span class="book-stars" itemprop="reviewRating">{% for i in object.ratings_range %}{% if i <= object.get_rating%}★{%else%}☆{%endif%}{%endfor%}</span>{%endif%} <span class="hide" itemprop="author">Scott Gilbertson</span> <meta itemprop="datePublished" content="{{object.read_date|date:'F Y'}}"><span class="read-in">Read in: {{object.read_date|date:"F Y"}}</span></div> </article> diff --git a/design/templates/details/book.html b/design/templates/details/book.html index 6524885..e1d211a 100644 --- a/design/templates/details/book.html +++ b/design/templates/details/book.html @@ -20,7 +20,7 @@ <dd>{{object.isbn}}</dd>{%endif%} {% if object.isbn %}<dt>Borrow</dt> <dd><a href="http://worldcat.org/isbn/{{object.isbn}}" title="find {{object.title}} in your local library">WorldCat</a></dd>{%endif%} - {% if object.rating %}<dt>Rating</dt><dd class="book-stars" itemprop="reviewRating"> {% for i in ratings_range %}{% if i <= object.get_rating%}★{%else%}☆{%endif%}{%endfor%}</dd>{%endif%} + {% if object.rating %}<dt>Rating</dt><dd class="book-stars" itemprop="reviewRating"> {% for i in object.ratings_range %}{% if i <= object.get_rating%}★{%else%}☆{%endif%}{%endfor%}</dd>{%endif%} {% if object.read_in %}<dt>Read</dt> <dd>{{object.read_in}}</dd>{%endif%} </dl> |