示例#1
0
文件: views.py 项目: saaalik/Kitabe
def book_recommendations(request):
    '''
        View to render book recommendations

        Count Vectorizer Approach:
            1. Get Ratings of User
            2. Shuffle by Top Ratings(For Randomness each time)
            3. Recommend according to Top Rated Book
    '''
    user_ratings = list(UserRating.objects.filter(user=request.user).order_by('-bookrating'))
    random.shuffle(user_ratings)
    best_user_ratings = sorted(user_ratings, key=operator.attrgetter('bookrating'), reverse=True)

    if len(best_user_ratings) < 4:
        messages.info(request, 'Please rate atleast 5 books')
        return redirect('index')
    if best_user_ratings:
        # If one or more book is rated
        bookid = best_user_ratings[0].bookid
        already_rated_books = set(get_rated_bookids(user_ratings))
        # Get bookids based on Count Vectorizer
        cv_bookids = set(count_vectorizer_recommendations(bookid))

        # Shuffle again for randomness for second approach
        random.shuffle(user_ratings)
        best_user_ratings = sorted(user_ratings, key=operator.attrgetter('bookrating'), reverse=True)
        # Get Top 10 bookids based on embedding
        embedding_bookids = set(embedding_recommendations(best_user_ratings))

        best_bookids = combine_ids(cv_bookids, embedding_bookids, already_rated_books)
        all_books_dict = get_book_dict(best_bookids)
    else:
        return redirect('index')
    return render(request, 'mainapp/recommendation.html', {'books': all_books_dict})
示例#2
0
def SaveList(request):
    """View to render Saved books page"""
    user_ratings = list(
        UserRating.objects.filter(user=request.user).order_by('-bookrating'))
    rated_books = set(get_rated_bookids(user_ratings))
    book = set(
        SaveForLater.objects.filter(user=request.user).values_list('bookid',
                                                                   flat=True))
    book_id = list(book)
    for i in range(len(book_id)):
        if book_id[i] in rated_books:
            saved_book = SaveForLater.objects.filter(user=request.user,
                                                     bookid=book_id[i])
            saved_book.delete()
            book_id.remove(book_id[i])
    if len(book_id) == 0:
        messages.info(request, 'Please Add Some Books')
        return redirect('index')
    books = get_book_dict(book_id)
    total_books = len(books)
    paginator = Paginator(books, 10)
    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)
    return render(request, 'mainapp/saved_book.html', {
        'page_obj': page_obj,
        'num': total_books
    })
示例#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)
示例#4
0
文件: views.py 项目: saaalik/Kitabe
def read_books(request):
    """View To Render Library Page"""
    user_ratings = list(UserRating.objects.filter(user=request.user).order_by('-bookrating'))
    if len(user_ratings) == 0:
        messages.info(request, 'Please rate some books')
        return redirect('index')
    if user_ratings:
        rated_books = set(get_rated_bookids(user_ratings))
        books = get_book_dict(rated_books)
        num = len(books)
        # Add pagination to the page showing 10 books
        paginator = Paginator(books, 10)
        page_number = request.GET.get('page')
        page_obj = paginator.get_page(page_number)
    else:
        return redirect('index')
    return render(request, 'mainapp/read.html', {'page_obj': page_obj, 'num': num})