def mybook(request): """ 获取我收藏的书 """ profile = request.user.get_profile() uid = profile.did reading_bids, wish_bids, readed_bids = getUserBooks(uid) reading = [] wish = [] readed = [] for bid in reading_bids: book = getBook(bid) if book: book.image_link = book.image_link.replace('spic', 'lpic') book.like_num = getLikeNumOfBook(bid) reading.append(book) for bid in wish_bids: book = getBook(bid) if book: book.image_link = book.image_link.replace('spic', 'lpic') book.like_num = getLikeNumOfBook(bid) wish.append(book) for bid in readed_bids: book = getBook(bid) if book: book.image_link = book.image_link.replace('spic', 'lpic') book.like_num = getLikeNumOfBook(bid) readed.append(book) return render_to_response('book/mybook.html', {'reading': reading, 'wish': wish, 'readed': readed}, context_instance=RequestContext(request))
def ubook(request): profile = request.user.get_profile() uid = profile.did bids = getBookRecommend(uid) paginator = Paginator(bids , 15) page = request.GET.get('page') try: bids_page = paginator.page(page) except PageNotAnInteger: # 如果页码不是整数,返回第一页. bids_page = paginator.page(1) except EmptyPage: # 如果页码不在范围内,返回最后一页 bids_page = paginator.page(paginator.num_pages) books = [] for bid in bids_page: book = getBook(bid) if book: book.image_link = book.image_link.replace('spic', 'lpic') book.like_num = getLikeNumOfBook(bid) books.append(book) return render_to_response('book/ubook.html', {'books':books, 'page':bids_page}, context_instance=RequestContext(request))
def getUserLikeBooks(user): books = [] query = Like.objects.filter(user=user) for record in query: book = getBook(record.book) if book: book.like_num = getLikeNumOfBook(record.book) books.append(book) return books
def view_book(request, bid=''): if not bid: return HttpResponseRedirect("/") book = getBook(bid) if book: book.image_link = book.image_link.replace('spic', 'lpic') reviews = getBookReviews(bid) return render_to_response('book/book.html', {'book': book, 'reviews': reviews}, context_instance=RequestContext(request)) else: error_message = "Sorry, Can't get this book..." return render_to_response('error.html', {'error':error_message, 'redirect_url':'/book/ubook/'}, context_instance=RequestContext(request))