コード例 #1
0
ファイル: spiders.py プロジェクト: likaiguo/Book-1
    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
コード例 #2
0
ファイル: spiders.py プロジェクト: likaiguo/Book-1
    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
コード例 #3
0
ファイル: spiders.py プロジェクト: TXLtianxiaolong/Book
class BasicSpider(object):
    def __init__(self, url, owner_id, online_url=''):
        self.url = url.strip()
        self.owner_id = owner_id
        self.online_url = online_url
        self.content = ''
        self.html = ''
        self.new_book = ''
        self.existed_book = ''

        self.content_dict = {}

    def crawl(self):
        self.content = requests.get(self.url).content
        self.html = etree.HTML(self.content)

    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 save(self):
        if self.content_dict.get('title'):
            self.new_book = BookInfo(**self.content_dict).save()
            self.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 self.new_book
        else:
            return False
コード例 #4
0
ファイル: book_service.py プロジェクト: acmchen/OnlineTest
 def add_book(book_info):
     book = BookInfo()
     book.book_id = str(uuid.uuid1())
     book.book_name = book_info.book_name.data
     book.version = book_info.version.data
     book.author = book_info.author.data
     book.publisher = book_info.publisher.data
     book.publish_time = book_info.publish_time.data
     book.subject = book_info.subject.data
     dbs.session.add(book)
     dbs.session.commit()
コード例 #5
0
ファイル: spiders.py プロジェクト: Kxrr/Book
 def save(self):
     if self.content_dict.get('title'):
         self.new_book = BookInfo(**self.content_dict).save()
         self.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 self.new_book
     else:
         return False
コード例 #6
0
ファイル: spiders.py プロジェクト: Kxrr/Book
 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
コード例 #7
0
ファイル: views.py プロジェクト: likaiguo/Book-1
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)
コード例 #8
0
ファイル: views.py プロジェクト: TXLtianxiaolong/Book
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)
コード例 #9
0
ファイル: views.py プロジェクト: likaiguo/Book-1
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('/')
コード例 #10
0
ファイル: views.py プロジェクト: likaiguo/Book-1
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('/')
コード例 #11
0
ファイル: views.py プロジェクト: TXLtianxiaolong/Book
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('/')
コード例 #12
0
ファイル: views.py プロジェクト: likaiguo/Book-1
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)
コード例 #13
0
ファイル: views.py プロジェクト: TXLtianxiaolong/Book
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)