def parse_content(self): title = self.html.xpath('//h1/span/text()') if title: self.title = title[0] author = self.html.xpath('//div[@id="info"]//a/text()') if author: self.author = author[0] rate = self.html.xpath('//div[@class="rating_wrap"]//strong/text()') # float if rate: if '.' in rate[0]: # 匹配少于10人评分 self.rate = float(rate[0]) img_url = self.html.xpath('//div[@class="article"]//a[@class="nbg"]/@href') if img_url: self.img_url = img_url[0] self.detail = self.html.xpath('//div[@class="intro"]/p/text()') # paragraph, lists self.tags = self.html.xpath('//div[@class="indent"]/span/a/text()') # lists if not BookInfo.objects(title=self.title, author=self.author): new_book = self.save() new_book.update(push__owner=User.objects.get(id=self.owner_id)) User.objects(id=self.owner_id).first().update(push__owned_book=self.new_book) return True else: exist_book = BookInfo.objects(title=self.title).first() add_book = exist_book.update(push__owner=User.objects.get(id=self.owner_id), inc__num=1) User.objects(id=self.owner_id).first().update(push__owned_book=exist_book) return True
def parse_content(self): title = self.html.xpath('//h1[@class="article-title"]/text()') if title: self.title = title[0] author = self.html.xpath('//a[@class="author-item"]/text()') if author: self.author = author[0] rate = self.html.xpath('//span[@class="rating-average"]/text()') if rate: self.rate = float(rate[0]) img_url = self.html.xpath('//div[@class="cover shadow-cover"]/img/@src') if img_url: self.img_url = img_url[0] self.detail = self.html.xpath('//div[@class="article-profile-section article-profile-intros"]//p/text()') self.tags = self.html.xpath('//*[@class="tags"]//span[1]/text()') self.category = '' if not BookInfo.objects(title=self.title, author=self.author): new_book = self.save() new_book.update(push__owner=User.objects.get(id=self.owner_id)) User.objects(id=self.owner_id).first().update(push__owned_book=new_book) return True else: exist_book = BookInfo.objects(title=title) add_book = exist_book.update(push__owner=User.objects.get(id=self.owner_id), inc__num=1) User.objects(id=self.owner_id).first().update(push__owned_book=exist_book.first()) return True
def if_exist_book(self): self.existed_book = BookInfo.objects(title=self.content_dict.get('title'), author=self.content_dict.get('author')) if self.existed_book: # 己经有这本书了 return True else: return False
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 handle_comment(): form = request.form content = request.form['content'] book_id = request.form['book_id'] user = User.objects(id=current_user.id).first() book = BookInfo.objects(id=book_id).first() comment = Comment(content=content, name=user) book.update(push__comment=comment) flash(u'评论成功') return book_detail(book_id)
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 borrow_book(book_id): if current_user.is_active: book_obj = BookInfo.objects(id=book_id).first() user_online_obj = User.objects(id=current_user.id).first() # Watch out, current_user is <class 'werkzeug.local.LocalProxy'> if (book_obj not in user_online_obj.borrowed_book) and (book_obj.num > 0): book_obj.update(dec__num=1, push__user_borrowed=user_online_obj) user_online_obj.update(push__borrowed_book=book_obj) Operation(type='borrow', user=user_online_obj, book_info=book_obj).save() Delivery(user=user_online_obj, book=book_obj).save() # 异常处理 flash(u'「{}」, 借阅成功'.format(book_obj.title)) else: flash(u'失败, 同一本书每人只能借一本') else: flash(u'失败, 请登录后再操作') return redirect('/')
def borrow_book(book_id): if current_user.is_active: book = BookInfo.objects(id=book_id).first() user = User.objects(id=current_user.id).first() if (book not in user.borrowed_book) and (book.num > 0): book.update(dec__num=1, push__user_borrowed=user) user.update(push__borrowed_book=book) Operation(type='borrow', user=user, book_info=book).save() Delivery(user=user, book=book).save() # 发送邮件给拥有者 owner_list = [] for owner in book.owner: send_borrow_noti_to_owner(user, owner, book) owner_list.append(u'{}({})'.format(owner.nickname, owner.real_name)) flash(u'「{}」, 操作成功, 己经发送借阅通知邮件给拥有者 {}'.format(book.title, ','.join(owner_list))) else: flash(u'失败, 同一本书每人只能借一本') else: flash(u'请登录后再操作') return redirect('/')
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)