Exemplo n.º 1
0
def book_summary(request):
    '''
        AJAX request for book summary
    '''
    if request.method == 'POST' and request.is_ajax():
        bookid = request.POST.get('bookid', None)
        if is_bookid_invalid(bookid):
            return JsonResponse({'success': False}, status=200)
        URL = 'https://www.goodreads.com/book/show/' + bookid
        page = requests.get(URL)
        soup = BeautifulSoup(page.content, 'html.parser')
        div_container = soup.find(id='description')
        full_book_summary = ""
        if not div_container:
            return JsonResponse({'success': False}, status=200)
        for spantag in div_container.find_all('span'):
            try:
                # When text is too long, consider till last complete sentence
                full_book_summary += spantag.text[:spantag.text.
                                                  rindex('.')] + '. '
            except ValueError:
                full_book_summary += spantag.text + ' '
            break
        part_summary = ' '.join(full_book_summary.split()[:65]) + ' . . .'
        return JsonResponse({
            'success': True,
            'booksummary': part_summary
        },
                            status=200)
Exemplo n.º 2
0
def remove_saved_book(request):
    """AJAX request when user removes book"""
    if request.method == 'POST' and request.is_ajax():
        bookid = request.POST.get('bookid', None)
        if is_bookid_invalid(bookid):
            return JsonResponse({'success': False}, status=200)

        saved_book = SaveForLater.objects.filter(user=request.user,
                                                 bookid=bookid)
        saved_book.delete()
        return JsonResponse({'success': True}, status=200)
Exemplo n.º 3
0
def save_book(request):
    """AJAX request when user saves book"""
    if request.method == 'POST' and request.is_ajax():
        bookid = request.POST.get('bookid', None)
        user_ratings = list(UserRating.objects.filter(user=request.user))
        rated_books = set(get_rated_bookids(user_ratings))
        if is_bookid_invalid(bookid) or bookid in rated_books:
            return JsonResponse({'success': False}, status=200)

        SaveForLater.objects.create(user=request.user, bookid=bookid)
        return JsonResponse({'success': True}, status=200)
Exemplo n.º 4
0
def get_book_details(request):
    '''
        AJAX request for book details
    '''
    if request.method == 'POST' and request.is_ajax():
        bookid = request.POST.get('bookid', None)
        if is_bookid_invalid(bookid):
            return JsonResponse({'success': False}, status=200)

        df_book = pd.read_csv(book_path)
        book_details = df_book[df_book['book_id'] == int(bookid)]
        if not len(book_details):
            return JsonResponse({'success': False}, status=200)

        book_details = json.dumps(book_details.to_dict('records'))
        return JsonResponse({
            'success': True,
            'book_details': book_details
        },
                            status=200)
Exemplo n.º 5
0
def user_rate_book(request):
    '''
        AJAX request when user rates book
    '''
    if request.method == 'POST' and request.is_ajax():
        bookid = request.POST.get('bookid', None)
        bookrating = request.POST.get('bookrating', None)
        if is_bookid_invalid(bookid) or is_rating_invalid(bookrating):
            return JsonResponse({'success': False}, status=200)

        # Using Inbuilt Model
        query = UserRating.objects.filter(user=request.user).filter(
            bookid=bookid)
        if not query:
            # Create Rating
            UserRating.objects.create(user=request.user,
                                      bookid=bookid,
                                      bookrating=bookrating)
        else:
            # Update Rating
            rating_object = query[0]
            rating_object.bookrating = bookrating
            rating_object.save()
        return JsonResponse({'success': True}, status=200)