From: Jan Szejko Date: Tue, 5 Jun 2018 14:46:14 +0000 (+0200) Subject: Merge branch 'preview' X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/73b6a1639ba8f9f5fc7615c136364fdba73e5bb2?ds=sidebyside Merge branch 'preview' # Conflicts: # src/api/handlers.py # src/catalogue/models/book.py # src/catalogue/templates/catalogue/book_short.html --- 73b6a1639ba8f9f5fc7615c136364fdba73e5bb2 diff --cc src/api/handlers.py index 893cd7e1b,c89997617..3e4e0931a --- a/src/api/handlers.py +++ b/src/api/handlers.py @@@ -161,7 -164,7 +164,8 @@@ class BookDetailHandler(BaseHandler, Bo """ allowed_methods = ['GET'] fields = ['title', 'parent', 'children'] + Book.formats + [ - 'media', 'url', 'cover', 'cover_thumb', 'simple_thumb', 'simple_cover', 'fragment_data', 'audio_length'] + [ - 'media', 'url', 'cover', 'cover_thumb', 'simple_thumb', 'simple_cover', 'fragment_data', 'preview'] + [ ++ 'media', 'url', 'cover', 'cover_thumb', 'simple_thumb', 'simple_cover', 'fragment_data', 'audio_length', ++ 'preview'] + [ category_plural[c] for c in book_tag_categories] @piwik_track @@@ -307,8 -322,14 +323,14 @@@ class QuerySetProxy(models.QuerySet) class FilterBooksHandler(AnonymousBooksHandler): fields = book_tag_categories + [ - 'href', 'title', 'url', 'cover', 'cover_thumb', 'simple_thumb', 'slug', 'key'] + 'href', 'title', 'url', 'cover', 'cover_thumb', 'simple_thumb', 'has_audio', 'slug', 'key'] + def parse_bool(self, s): + if s in ('true', 'false'): + return s == 'true' + else: + return None + def read(self, request): key_sep = '$' search_string = request.GET.get('search') @@@ -415,6 -433,6 +434,7 @@@ def add_file_getters() for book_format in Book.formats: setattr(BookDetails, book_format, _file_getter(book_format)) ++ add_file_getters() @@@ -606,7 -624,7 +626,7 @@@ class FragmentsHandler(BaseHandler, Fra """ try: -- tags, ancestors = read_tags(tags, allowed=self.categories) ++ tags, ancestors = read_tags(tags, request, allowed=self.categories) except ValueError: return rc.NOT_FOUND fragments = Fragment.tagged.with_all(tags).select_related('book') @@@ -632,3 -650,62 +652,63 @@@ class PictureHandler(BaseHandler) return rc.CREATED else: return rc.NOT_FOUND + + + class UserDataHandler(BaseHandler): + model = BookUserData + fields = ('state',) + allowed_methods = ('GET', 'POST') + + def read(self, request, slug): + try: + book = Book.objects.get(slug=slug) + except Book.DoesNotExist: + return rc.NOT_FOUND + if not request.user.is_authenticated(): + return rc.FORBIDDEN + try: + data = BookUserData.objects.get(book=book, user=request.user) + except BookUserData.DoesNotExist: + return {'state': 'not_started'} + return data + + def create(self, request, slug, state): + try: + book = Book.objects.get(slug=slug) + except Book.DoesNotExist: + return rc.NOT_FOUND + if not request.user.is_authenticated(): + return rc.FORBIDDEN + if state not in ('reading', 'complete'): + return rc.NOT_FOUND + data, created = BookUserData.objects.get_or_create(book=book, user=request.user) + data.state = state + data.save() + return data + + + class UserShelfHandler(BookDetailHandler): + fields = book_tag_categories + [ + 'href', 'title', 'url', 'cover', 'cover_thumb', 'simple_thumb', 'slug', 'key'] + + def parse_bool(self, s): + if s in ('true', 'false'): + return s == 'true' + else: + return None + + def read(self, request, state): + if not request.user.is_authenticated(): + return rc.FORBIDDEN + if state not in ('reading', 'complete'): + return rc.NOT_FOUND + after = request.GET.get('after') + count = int(request.GET.get('count', 50)) - ids = BookUserData.objects.filter(user=request.user, complete=state == 'complete').values_list('book_id', flat=True) ++ ids = BookUserData.objects.filter(user=request.user, complete=state == 'complete')\ ++ .values_list('book_id', flat=True) + books = Book.objects.filter(id__in=list(ids)).distinct().order_by('slug') + if after: + books = books.filter(slug__gt=after) + if count: + books = books[:count] + return books diff --cc src/catalogue/migrations/0025_merge.py index 000000000,000000000..66c8c8f4d new file mode 100644 --- /dev/null +++ b/src/catalogue/migrations/0025_merge.py @@@ -1,0 -1,0 +1,15 @@@ ++# -*- coding: utf-8 -*- ++from __future__ import unicode_literals ++ ++from django.db import migrations, models ++ ++ ++class Migration(migrations.Migration): ++ ++ dependencies = [ ++ ('catalogue', '0024_book_audio_length'), ++ ('catalogue', '0024_auto_20180510_1407'), ++ ] ++ ++ operations = [ ++ ] diff --cc src/catalogue/models/book.py index a1ca5bbed,027d272a3..004c27e89 --- a/src/catalogue/models/book.py +++ b/src/catalogue/models/book.py @@@ -71,7 -72,8 +72,9 @@@ class Book(models.Model) wiki_link = models.CharField(blank=True, max_length=240) print_on_demand = models.BooleanField(_('print on demand'), default=False) recommended = models.BooleanField(_('recommended'), default=False) + audio_length = models.CharField(_('audio length'), blank=True, max_length=8) + preview = models.BooleanField(_('preview'), default=False) + preview_until = models.DateField(_('preview until'), blank=True, null=True) # files generated during publication cover = EbookField( @@@ -748,6 -762,6 +788,7 @@@ def add_file_fields() default='' ).contribute_to_class(Book, field_name) ++ add_file_fields() diff --cc src/catalogue/templates/catalogue/book_short.html index fe4871d81,727ffb474..675e761c4 --- a/src/catalogue/templates/catalogue/book_short.html +++ b/src/catalogue/templates/catalogue/book_short.html @@@ -97,55 -97,61 +97,59 @@@ {% book_shelf_tags book.pk %} - + {% else %} +

{% trans "For now this work is only available for our subscribers." %}

+ {% endif %} -
- {% if book.abstract %} -
- {{ book.abstract|safe }} -
- {% endif %} {% block book-box-extra-info %}{% endblock %} - {% block box-append %} - {% endblock %} + {% block box-append %}{% endblock %} + {% if book.abstract %} +
+ {{ book.abstract|safe }} +
+ {% endif %} {% endwith %} {% block right-column %}