def return_book(book_id): user_obj = User.objects(id=current_user.id).first() book_obj = BookInfo.objects(id=book_id).first() if user_obj in book_obj.user_borrowed: user_obj.update(pull__borrowed_book=book_obj) book_obj.update(inc__num=1, pull__user_borrowed=user_obj) flash(u'「{}」, 归还成功'.format(book_obj.title)) Operation(type='return', user=user_obj, book_info=book_obj).save() Delivery.objects(user=user_obj, book=book_obj).update(set__return_time=datetime.now(), set__returned=True) return redirect('/') else: flash(u'非法操作') return redirect('/')
def profile_info(): user_obj = User.objects.get(id=current_user.id) user_borrowed_books = user_obj.borrowed_book deliverys = Delivery.objects(user=user_obj) owned_books = BookInfo.objects(owner=user_obj) now = datetime.now() return render_template('profile.html', books=user_borrowed_books, owned_books=owned_books, user=user_obj, deliverys=deliverys, now=now)
def index(): book_count = BookInfo.objects.count() books = BookInfo.objects.limit(page_limit) # user = User.objects.get(id=current_user.id) users = User.objects book_amount = amount_fake_aggregation.count_all() book_out = Delivery.objects(returned__ne=True).count() return render_template('index.html', books=books, user=current_user, all_books=books, users=users, page=(book_count/page_limit)+2, current_page=1, book_amount=book_amount, book_out=book_out)
def index_page(n): # TODO: 分页实现的优化 skip = (int(n)-1) * page_limit book_count = BookInfo.objects.count() books = BookInfo.objects.skip(skip).limit(page_limit) users = User.objects book_amount = amount_fake_aggregation.count_all() book_out = Delivery.objects(returned__ne=True).count() return render_template('index.html', books=books, user=current_user, all_books=books, users=users, page=(book_count/page_limit)+2, current_page=int(n), book_amount=book_amount, book_out=book_out)
def count_pop_book(): # 统计最受欢迎的图书 book_name = [] count = [] b_id = [] for book in BookInfo.objects: book_name.append(book.title) count.append(Delivery.objects(book=book).count()) b_id.append(book.id) result = zip(book_name, count, b_id) result_sorted = sorted(result, key=lambda x: x[1], reverse=True) return result_sorted
def count_borrowed(): # 统计借过最多书的人 name = [] count = [] u_id = [] for user in User.objects: name.append(user.nickname) count.append(Delivery.objects(user=user).count()) u_id.append(user.id) result = zip(name, count, u_id) result_sorted = sorted(result, key=lambda x: x[1], reverse=True) return result_sorted
def index(category=''): category_dict = { 'category': category } book_count = BookInfo.objects.count() if category: books = BookInfo.objects.filter(**category_dict) else: books = BookInfo.objects.limit(PAGE_LIMIT) users = User.objects book_amount = amount.count_all() book_out = Delivery.objects(returned__ne=True).count() return render_template('index.html', books=books, user=current_user, all_books=books, users=users, page=(book_count/PAGE_LIMIT)+2, current_page=1, book_amount=book_amount, book_out=book_out)
def user_info(id): user = User.objects(id=id).first() deliverys = Delivery.objects(user=user) owned_books = BookInfo.objects(owner=user) return render_template('profile_pub.html', user=user, deliverys=deliverys, owned_books=owned_books)
def book_detail(book_id): book_obj = BookInfo.objects(id=book_id).first() delivers = Delivery.objects(book=book_obj) return render_template('detail.html', book=book_obj, delivers=delivers)