book_tag_categories = ['author', 'epoch', 'kind', 'genre']
+book_list_fields = book_tag_categories + [
+ 'href', 'title', 'url', 'cover', 'cover_thumb', 'slug', 'simple_thumb', 'has_audio', 'cover_color']
+
def read_tags(tags, request, allowed):
""" Reads a path of filtering tags.
@classmethod
def cover_color(cls, book):
- return WLCover.epoch_colors.get(book.extra_info['epoch'], '#000000')
+ return WLCover.epoch_colors.get(book.extra_info.get('epoch'), '#000000')
class BookDetailHandler(BaseHandler, BookDetails):
"""
allowed_methods = ('GET',)
model = Book
- fields = book_tag_categories + [
- 'href', 'title', 'url', 'cover', 'cover_thumb', 'slug', 'simple_thumb', 'has_audio', 'cover_color']
+ fields = book_list_fields
@classmethod
def genres(cls, book):
class BooksHandler(BookDetailHandler):
allowed_methods = ('GET', 'POST')
model = Book
- fields = book_tag_categories + ['href', 'title', 'url', 'cover', 'cover_thumb', 'cover_color', 'slug']
+ fields = book_list_fields + ['liked']
anonymous = AnonymousBooksHandler
+ # hack, because piston is stupid
+ @classmethod
+ def liked(cls, book):
+ return getattr(book, 'liked', None)
+
+ def read(self, request, **kwargs):
+ books = AnonymousBooksHandler().read(request, **kwargs)
+ likes = set(Book.tagged.with_any(request.user.tag_set.all()).values_list('id', flat=True))
+
+ new_books = [
+ BookProxy(book).set('liked', book.id in likes)
+ for book in books]
+ return QuerySetProxy(new_books)
+
def create(self, request, *args, **kwargs):
if not request.user.has_perm('catalogue.add_book'):
return rc.FORBIDDEN
class Meta:
managed = False
- def __init__(self, book, key):
+ def __init__(self, book, key=None):
self.book = book
self.key = key
+ def set(self, attr, value):
+ self.__setattr__(attr, value)
+ return self
+
def __getattr__(self, item):
- if item not in ('book', 'key'):
- return self.book.__getattribute__(item)
- else:
- return self.__getattribute__(item)
+ return self.book.__getattribute__(item)
class QuerySetProxy(models.QuerySet):
return iter(self.list)
-class FilterBooksHandler(AnonymousBooksHandler):
- fields = book_tag_categories + [
- 'href', 'title', 'url', 'cover', 'cover_thumb', 'cover_color', 'simple_thumb', 'has_audio', 'slug', 'key']
+class AnonFilterBooksHandler(AnonymousBooksHandler):
+ fields = book_list_fields + ['key']
def parse_bool(self, s):
if s in ('true', 'false'):
return QuerySetProxy(filtered_books)
+class FilterBooksHandler(BooksHandler):
+ anonymous = AnonFilterBooksHandler
+ fields = book_list_fields + ['key', 'liked']
+
+ # hack, because piston is stupid
+ @classmethod
+ def liked(cls, book):
+ return getattr(book, 'liked', None)
+
+ def read(self, request):
+ qsp = AnonFilterBooksHandler().read(request)
+ likes = set(Book.tagged.with_any(request.user.tag_set.all()).values_list('id', flat=True))
+ for book in qsp.list:
+ book.set('liked', book.id in likes)
+ return qsp
+
+
class BookPreviewHandler(BookDetailHandler):
+ fields = BookDetailHandler.fields + ['slug']
+
def read(self, request):
return Book.objects.filter(preview=True)
class UserDataHandler(BaseHandler):
model = BookUserData
- fields = ('state', 'username')
+ fields = ('state', 'username', 'premium')
allowed_methods = ('GET', 'POST')
def read(self, request, slug=None):
if not request.user.is_authenticated():
return rc.FORBIDDEN
if slug is None:
- return {'username': request.user.username}
+ return {'username': request.user.username, 'premium': is_subscribed(request.user)}
try:
book = Book.objects.get(slug=slug)
except Book.DoesNotExist:
class UserShelfHandler(BookDetailHandler):
- fields = book_tag_categories + [
- 'href', 'title', 'url', 'cover', 'cover_thumb', 'simple_thumb', 'slug', 'key']
+ fields = book_list_fields + ['liked']
def parse_bool(self, s):
if s in ('true', 'false'):
else:
return None
+ # hack, because piston is stupid
+ @classmethod
+ def liked(cls, book):
+ return getattr(book, 'liked', None)
+
def read(self, request, state):
if not request.user.is_authenticated():
return rc.FORBIDDEN
+ likes = set(Book.tagged.with_any(request.user.tag_set.all()).values_list('id', flat=True))
if state not in ('reading', 'complete', 'likes'):
return rc.NOT_FOUND
after = request.GET.get('after')
books = books.filter(slug__gt=after)
if count:
books = books[:count]
- return books
+ new_books = []
+ for book in books:
+ new_books.append(BookProxy(book).set('liked', book.id in likes))
+ return QuerySetProxy(new_books)
class UserLikeHandler(BaseHandler):
class BlogEntryHandler(BaseHandler):
model = Entry
- fields = ('title', 'lead', 'body', 'place', 'time', 'image_url', 'image_thumb', 'gallery_urls', 'type', 'key')
+ fields = (
+ 'title', 'lead', 'body', 'place', 'time', 'image_url', 'image_thumb', 'gallery_urls', 'type', 'key', 'url')
def read(self, request):
after = request.GET.get('after')
@classmethod
def key(cls, entry):
return entry.first_published_at
+
+ @classmethod
+ def url(cls, entry):
+ return WL_BASE + entry.get_absolute_url()