def reback(book_id): print "reback book , book_id=", book_id book = Book.query.filter_by(id=book_id).first() # 修改图书属性 # book.borrow_id = None # book.borrow_name = '' book.status = 2 # 还书成功,将状态恢复为2 0 :空闲中 1:借阅中 2:归还中 # 记录到还书确认任务表中 task = Task() task.user_id = book.owner_id task.book_id = book.id task.content = u"%s 要归还书:<<%s>>,请确认是否收到。" % (current_user.name, book.title) task.status = 0 task.create_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') db.session.add(task) # 记录还书时间 borrow_log = BorrowLog.query.filter_by(id=book.borrow_log_id).first() borrow_log.real_reback_time = datetime.datetime.now().strftime( '%Y-%m-%d %H:%M:%S') db.session.commit() save_syslog(current_user, request.remote_addr, u"申请还书,书名:《%s》" % book.title) flash(u"申请还书成功,请找%s确认" % book.owner_name, "danger") return redirect(url_for("books.my"))
def confirm_reback(id): task = Task.query.filter_by(id=id).first() if task: print task.book_id book = Book.query.filter_by(id=task.book_id).first() if book: print book.borrow_id # 修改图书属性 book.borrow_id = None book.borrow_name = '' book.status = 0 # 还书成功,将状态恢复为0 0 :空闲中 1:借阅中 2:归还中 print book.borrow_id db.session.commit() task.status = 1 task.opt_time = datetime.datetime.now().strftime( '%Y-%m-%d %H:%M:%S') db.session.commit() save_syslog(current_user, request.remote_addr, u"还书确认成功,书名:《%s》" % book.title) flash(u"确认成功。", "success") else: flash(u"确认失败,没有找到对应的图书。", "danger") else: flash(u"确认失败,没有找到任务。", "danger") return redirect(url_for("books.tasks"))
def confirm_reback(id): task = Task.query.filter_by(id=id).first() if task: print task.book_id book = Book.query.filter_by(id=task.book_id).first() if book: print book.borrow_id # 修改图书属性 book.borrow_id = None book.borrow_name = '' book.status = 0 # 还书成功,将状态恢复为0 0 :空闲中 1:借阅中 2:归还中 print book.borrow_id db.session.commit() task.status = 1 task.opt_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') db.session.commit() save_syslog(current_user, request.remote_addr, u"还书确认成功,书名:《%s》" % book.title) flash(u"确认成功。", "success") else: flash(u"确认失败,没有找到对应的图书。", "danger") else: flash(u"确认失败,没有找到任务。", "danger") return redirect(url_for("books.tasks"))
def reback(book_id): print "reback book , book_id=", book_id book = Book.query.filter_by(id=book_id).first() # 修改图书属性 # book.borrow_id = None # book.borrow_name = '' book.status = 2 # 还书成功,将状态恢复为2 0 :空闲中 1:借阅中 2:归还中 # 记录到还书确认任务表中 task = Task() task.user_id = book.owner_id task.book_id = book.id task.content = u"%s 要归还书:<<%s>>,请确认是否收到。" % (current_user.name, book.title) task.status = 0 task.create_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') db.session.add(task) # 记录还书时间 borrow_log = BorrowLog.query.filter_by(id=book.borrow_log_id).first() borrow_log.real_reback_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') db.session.commit() save_syslog(current_user, request.remote_addr, u"申请还书,书名:《%s》" % book.title) flash(u"申请还书成功,请找%s确认" % book.owner_name, "danger") return redirect(url_for("books.my"))
def oa_login(): login_name = '' name = '' if request.method == "POST": login_name = request.form['login_name'] name = request.form['name'].encode("utf-8") else: login_name = request.args.get('login_name', '') name = request.args.get('name', '').encode("utf-8") print "oa_login,login_name =", login_name, ",name =", name if login_name and name: account = Account.query.filter_by(login_name=login_name).first() if not account: # 如果不存在,首先创建用户,然后默认登录 account = Account(login_name, name, '123456') account.create_time = datetime.datetime.now().strftime('%Y-%d-%d %H:%M:%S') db.session.add(account) db.session.commit() save_syslog(account, request.remote_addr, u"创建用户") if login_user(account): save_syslog(account, request.remote_addr, u"登录成功") return redirect(request.args.get("next") or url_for("books.index")) return redirect(request.args.get("next") or url_for("books.index"))
def logout(): save_syslog(current_user, request.remote_addr, u"登出") logout_user() flash(u"已退出登录.", 'info') return redirect(url_for("books.index"))
def view(book_id): book = Book.query.filter_by(id=book_id).first() save_syslog(current_user, request.remote_addr, u"查看图书详情,书名:《%s》" % book.title) # 借阅历史 borrow_log_list = BorrowLog.query.filter_by(book_id=book.id).order_by(BorrowLog.id.desc()).limit(10) # 评论 comments = Comment.query.filter_by(book_id=book.id).order_by(Comment.id.desc()).limit(10) return render_template("book_detail.html", book=book, borrow_log_list=borrow_log_list, comments=comments)
def view(book_id): book = Book.query.filter_by(id=book_id).first() save_syslog(current_user, request.remote_addr, u"查看图书详情,书名:《%s》" % book.title) # 借阅历史 borrow_log_list = BorrowLog.query.filter_by(book_id=book.id).order_by( BorrowLog.id.desc()).limit(10) # 评论 comments = Comment.query.filter_by(book_id=book.id).order_by( Comment.id.desc()).limit(10) return render_template("book_detail.html", book=book, borrow_log_list=borrow_log_list, comments=comments)
def comment(): content = request.form['content'] book_id = request.form['book_id'] print "add comment:", content.encode("utf-8"), book_id comment = Comment() comment.content = content comment.book_id = book_id comment.account_id = current_user.id comment.account_name = current_user.name comment.create_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') db.session.add(comment) db.session.commit() save_syslog(current_user, request.remote_addr, u"评价成功") flash(u"评价成功", "info") return redirect(url_for("books.view", book_id=book_id))
def login(): if request.method == "POST": login_name = request.form['login_name'] if login_name: account = Account.query.filter_by(login_name=login_name).first() if account: if login_user(account): save_syslog(account, request.remote_addr, u"登录成功") return redirect(request.args.get("next") or url_for("books.index")) else: flash("Sorry, please check your username or password!", "danger") else: flash("Sorry, please check your username or password!", "danger") return render_template("login.html")
def share(): if request.method == "GET": return render_template("share.html") isbn = request.form['isbn'] # 校验书是否重复 book = Book.query.filter_by(isbn13=isbn).filter_by( owner_id=current_user.id).first() print book if book: res = Res(400, "分享失败,你已经共享过此书。") return jsonify(res.serialize()) city = request.form['city'] recommend = request.form['recommend'] client = DoubanClient() book = client.parse_book_info(isbn) if book: book.owner_id = current_user.id book.owner_name = current_user.name book.recommend = recommend book.city = city db.session.add(book) db.session.commit() for tag in book.tags: t = Tag.query.filter_by(name=tag['name'].encode('utf-8')).first() if not t: # 如果以前不存在 t = Tag() t.name = tag['name'] t.counts = int(tag['count']) t.create_time = datetime.datetime.now().strftime( '%Y-%m-%d %H:%M:%S') db.session.add(t) db.session.commit() else: t.counts = t.counts + int(tag['count']) bookTag = BookTag() bookTag.book_id = book.id bookTag.tag_id = t.id bookTag.count = tag['count'] db.session.add(bookTag) db.session.commit() save_syslog(current_user, request.remote_addr, u"分享图书,书名:《%s》" % book.title) # 处理当前人的城市属性 # 修改当前登录人的城市为最后分享图书的城市 account = Account.query.filter_by(id=current_user.id).first() account.city = book.city db.session.commit() print account res = Res(200, "分享成功") else: res = Res(400, "分享失败") return jsonify(res.serialize())
def share(): if request.method == "GET": return render_template("share.html") isbn = request.form['isbn'] # 校验书是否重复 book = Book.query.filter_by(isbn13=isbn).filter_by(owner_id=current_user.id).first() print book if book: res = Res(400, "分享失败,你已经共享过此书。") return jsonify(res.serialize()) city = request.form['city'] recommend = request.form['recommend'] client = DoubanClient() book = client.parse_book_info(isbn) if book: book.owner_id = current_user.id book.owner_name = current_user.name book.recommend = recommend book.city = city db.session.add(book) db.session.commit() for tag in book.tags: t = Tag.query.filter_by(name=tag['name'].encode('utf-8')).first() if not t: # 如果以前不存在 t = Tag() t.name = tag['name'] t.counts = int(tag['count']) t.create_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') db.session.add(t) db.session.commit() else: t.counts = t.counts + int(tag['count']) bookTag = BookTag() bookTag.book_id = book.id bookTag.tag_id = t.id bookTag.count = tag['count'] db.session.add(bookTag) db.session.commit() save_syslog(current_user, request.remote_addr, u"分享图书,书名:《%s》" % book.title) # 处理当前人的城市属性 # 修改当前登录人的城市为最后分享图书的城市 account = Account.query.filter_by(id=current_user.id).first() account.city = book.city db.session.commit() print account res = Res(200, "分享成功") else: res = Res(400, "分享失败") return jsonify(res.serialize())