diff options
Diffstat (limited to 'app')
-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 |
5 files changed, 55 insertions, 50 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) |