예제 #1
0
def upload_cover(request, book_id):
    ''' upload a new cover '''
    # TODO: alternate covers?
    if not request.method == 'POST':
        return redirect('/book/%s' % request.user.localname)

    try:
        book = models.Edition.objects.get(id=book_id)
    except models.Book.DoesNotExist:
        return HttpResponseNotFound()

    form = forms.CoverForm(request.POST, request.FILES, instance=book)
    if not form.is_valid():
        return redirect(request.headers.get('Referer', '/'))

    book.cover = form.files['cover']
    book.sync_cover = False
    book.save()

    outgoing.handle_update_book(request.user, book)
    return redirect('/book/%s' % book.fedireads_key)
예제 #2
0
def book_page(request, book_id, tab='friends'):
    ''' info about a book '''
    book = models.Book.objects.select_subclasses().get(id=book_id)
    if is_api_request(request):
        return JsonResponse(activitypub.get_book(book))

    if isinstance(book, models.Work):
        book = book.default_edition
    if not book:
        return HttpResponseNotFound()

    work = book.parent_work
    if not work:
        return HttpResponseNotFound()

    book_reviews = models.Review.objects.filter(
        book__in=work.edition_set.all())

    if request.user.is_authenticated:
        user_reviews = book_reviews.filter(user=request.user, ).all()

        reviews = get_activity_feed(request.user, tab, model=book_reviews)

        try:
            # TODO: books can be on multiple shelves
            shelf = models.Shelf.objects.filter(user=request.user,
                                                edition=book).first()
        except models.Shelf.DoesNotExist:
            shelf = None

        user_tags = models.Tag.objects.filter(
            book=book, user=request.user).values_list('identifier', flat=True)
    else:
        tab = 'public'
        reviews = book_reviews.filter(privacy='public')
        shelf = None
        user_reviews = []
        user_tags = []

    rating = reviews.aggregate(Avg('rating'))
    tags = models.Tag.objects.filter(book=book).values(
        'book', 'name', 'identifier').distinct().all()

    data = {
        'book':
        book,
        'shelf':
        shelf,
        'user_reviews':
        user_reviews,
        'reviews':
        reviews.distinct(),
        'rating':
        rating['rating__avg'],
        'tags':
        tags,
        'user_tags':
        user_tags,
        'review_form':
        forms.ReviewForm(),
        'tag_form':
        forms.TagForm(),
        'feed_tabs': [{
            'id': 'friends',
            'display': 'Friends'
        }, {
            'id': 'local',
            'display': 'Local'
        }, {
            'id': 'federated',
            'display': 'Federated'
        }],
        'active_tab':
        tab,
        'path':
        '/book/%s' % book_id,
        'cover_form':
        forms.CoverForm(instance=book),
        'info_fields': [
            {
                'name': 'ISBN',
                'value': book.isbn_13
            },
            {
                'name': 'OCLC number',
                'value': book.oclc_number
            },
            {
                'name': 'OpenLibrary ID',
                'value': book.openlibrary_key
            },
            {
                'name': 'Goodreads ID',
                'value': book.goodreads_key
            },
            {
                'name': 'Format',
                'value': book.physical_format
            },
            {
                'name': 'Pages',
                'value': book.pages
            },
        ],
    }
    return TemplateResponse(request, 'book.html', data)