示例#1
0
def book_detail(isbn):
    has_in_gifts = False  # 默认情况下,书籍是否在礼物清单
    has_in_wishes = False  # 是否在心愿清单

    # 取书籍详情数据
    yushu_book = YushuBook()
    yushu_book.search_by_isbn(isbn)
    book = BookViewModel(yushu_book.first)

    if current_user.is_authenticated:
        if Gift.query.filter_by(uid=current_user.id, isbn=isbn,
                                launched=False).first():
            has_in_gifts = True
        if Wish.query.filter_by(uid=current_user.id, isbn=isbn,
                                launched=False).first():
            has_in_wishes = True
    trade_gifts = Gift.query.filter_by(isbn=isbn, launched=False).all()
    trade_wishes = Wish.query.filter_by(isbn=isbn, launched=False).all()
    # 页面还需要user、create_time
    # 数据转换 => viewModel
    # wishes gifts
    trade_gifts_model = TradeInfo(trade_gifts)
    trade_wishes_model = TradeInfo(trade_wishes)
    return render_template('book_detail.html',
                           book=book,
                           wishes=trade_wishes_model,
                           gifts=trade_gifts_model,
                           has_in_gifts=has_in_gifts,
                           has_in_wishes=has_in_wishes)
示例#2
0
def search():  # controller 也叫视图函数,本质就是函数,用于控制mvc view视图
    """
        q :普通关键字/isbn查询 用代码区分
        page
        ?q=name&page=num
    """
    # 验证层
    form = SearchForm(request.args)
    books = BookCollection()  # 空数据

    if form.validate():  # 执行校验
        q = form.q.data.strip()
        page = form.page.data
        isbn_or_key = is_isbn_or_key(q)
        # 不可变转可变 test3.py = request.args.to_dict()  args是一个immutable dict
        yushu_book = YushuBook()

        if isbn_or_key == 'isbn':
            yushu_book.search_by_isbn(q)
        else:
            yushu_book.search_by_keyword(q, page)

        books.fill(yushu_book, q)
        # return json.dumps(books, default=lambda o: o.__dict__)
    else:
        flash('搜索的关键字不符合要求,请重新输入关键字')
        # return jsonify(form.errors)
        # return json.dumps(result), 200, {'content-type': 'application/json'}
    # 确保总能返回结果
    return render_template('search_result.html', books=books)
示例#3
0
def book_detail(isbn):
    #默认情况下
    has_in_giftes = False
    has_in_wishes = False

    # 书籍详情信息
    yushu_book = YushuBook()
    yushu_book.search_by_isbn(isbn)
    book = BookViewModel(yushu_book.books[0])

    # 用户登录情况
    if current_user.is_authenticated:
        if Gift.query.filter_by(isbn=isbn, uid=current_user.id,
                                launched=False).first():
            has_in_gifts = True
        if Wish.query.filter_by(isbn=isbn, uid=current_user.id,
                                launched=False).first():
            has_in_wishes = True

    trade_gifts = Gift.query.filter_by(isbn=isbn, launched=False).all()
    trade_wishes = Wish.query.filter_by(isbn=isbn, launched=False).all()

    trade_gifts_model = Trade(trade_gifts)
    trade_wishes_model = Trade(trade_wishes)

    return render_template('book_detail.html',
                           book=book,
                           wishes=trade_wishes_model,
                           gifts=trade_gifts_model,
                           has_in_gifts=has_in_gifts,
                           has_in_wishes=has_in_wishes)
示例#4
0
    def can_save_to_list(self, isbn):
        if is_isbn_or_key(isbn) != 'isbn':
            return False
        yushu_book = YushuBook()
        yushu_book.search_by_isbn(isbn)
        if not yushu_book.first:
            return False

        gifting = Gift.quer.filter_by(uid=self.id, isbn=isbn,
                                      launched=False).first()
        wishing = Wish.query.filter_by(uid=self.id, isbn=isbn,
                                       launched=False).first()
        if not gifting and not wishing:
            return True
        else:
            return False

        def generate_token(self, expiration=600):
            s = Serializer(current_app.config['SECRET_KEY'], expiration)
            return s.dumps({'id': self.id}).decode('utf-8')

        @staticmethod
        def reset_password(token, new_password):
            s = Serializer(current_app.cofig['SECRET_KEY'])
            # 反向去序列化器中的数据
            try:
                data = s.loads(token.encode('utf-8'))
            except:
                return False
            uid = data.get('id')
            with db.auto_commit():
                user = User.quer.get(uid)
                user.password = new_password
            return True

        def can_send_drift(self):
            if self.beans < 1:
                return False
            success_gifts_count = Gift.query.filter_by(uid=self.uid,
                                                       launched=True).count()
            success_receive_count = Drift.query.filter_by(
                requester_id=self.id, pending=PendingStatus.Success).count()

            return True if \
                floor(success_receive_count / 2) <= floor(success_gifts_count) \
                else False

        @property
        def summary(self):
            return dict(nickname=self.nickname,
                        beans=self.beans,
                        email=self.email,
                        send_receive=str(self.seng_counter) + '/' +
                        str(self.receive_counter))
示例#5
0
def save_to_wish(isbn):
    yushu_book = YushuBook()
    yushu_book.search_by_isbn(isbn)
    if current_user.can_save_to_list(isbn):
        with db.auto_commit():
            wish = Wish()
            wish.uid = current_user.id
            wish.isbn = isbn
            db.session.add(wish)
    else:
        flash('这本书已添加至你的赠送清单或已存在于你的心愿清单,请不要重复添加')
    return redirect(url_for('web.book_detail', isbn=isbn))
示例#6
0
 def can_save_to_list(self, isbn):
     if is_isbn_or_key(isbn) != 'isbn':
         return False
     yushu_book = YushuBook()
     yushu_book.search_by_isbn(isbn)
     if not yushu_book.first:
         return False
     gifting = Gift.query.filter_by(uid=self.id, isbn=isbn,
                                    launched=False).first()
     wishing = Wish.query.filter_by(uid=self.id, isbn=isbn,
                                    launched=False).first()
     if not gifting and not wishing:
         return True
     else:
         return False
示例#7
0
def save_to_gifts(isbn):
    yushu_book = YushuBook()
    yushu_book.search_by_isbn(isbn)

    if current_user.can_save_to_list(isbn):
        # 既不在赠送清单,也不在心愿清单才能添加
        with db.auto_commit():
            gift = Gift()
            gift.uid = current_user.id
            gift.isbn = isbn

            db.session.add(gift)
            current_user.beans += current_app.config['BEANS_UPLOAD_ONE_BOOK']
    else:
        flash('这本书已添加至你的赠送清单或已存在于你的心愿清单,请不要重复添加')
    return redirect(url_for('web.book_detail', isbn=isbn))
示例#8
0
def search():
    form = SearchForm(request.args)
    books = BookCollection()
    if form.validate():
        q = form.q.data.strip()
        page = form.page.data
        is_key = is_isbn_or_key(q)

        yushu_book = YushuBook()
        if is_key == 'isbn':
            yushu_book.search_by_isbn(q)

        else:
            yushu_book.search_by_keyword(q)
        books.fill(yushu_book, q)
    else:
        flash("您输入的关键词有误,请重新输入")
    return render_template('search_result.html', books=books)
示例#9
0
    def can_save_to_list(self, isbn):
        # 判断是真实的isbn编号
        if is_isbn_or_key(isbn) != 'isbn':
            return False
        # 判断数据库是否存在这本书
        yushu_book = YushuBook()
        yushu_book.search_by_isbn(isbn)
        if not yushu_book.first:
            return False
        # 不允许一个用户同时赠送多本相同的图书
        # 一个用户不能同时成为赠送者和索要者

        # 既不在赠送清单,也不在心愿清单才能添加
        gifting = Gift.query.filter_by(uid=self.id, isbn=isbn,
                                       launched=False).first()
        wishing = Wish.query.filter_by(uid=self.id, isbn=isbn,
                                       launched=False).first()

        if not gifting and not wishing:
            return True
        else:
            return False
示例#10
0
def book_detail(isbn):
    """
            1. 当书籍既不在心愿清单也不在礼物清单时,显示礼物清单
            2. 当书籍在心愿清单时,显示礼物清单
            3. 当书籍在礼物清单时,显示心愿清单
            4. 一本书要防止即在礼物清单,又在赠送清单,这种情况是不符合逻辑的

            这个视图函数不可以直接用cache缓存,因为不同的用户看到的视图不一样
            优化是一个逐步迭代的过程,建议在优化的初期,只缓存那些和用户无关的“公共数据"
    """
    has_in_gifts = False
    has_in_wishs = False

    # 取书籍详情数据
    yushu_book = YushuBook()
    yushu_book.search_by_isbn(isbn)
    book = BookViewModel(yushu_book.first)
    if current_user.is_authenticated:
        if Gift.query.filter_by(uid=current_user.id, isbn=isbn,
                                launched=False).first():
            has_in_gifts = True

        if Wish.query.filter_by(uid=current_user.id, isbn=isbn,
                                launched=False).first():
            has_in_wishs = False

    trade_gifts = Gift.query.filter_by(isbn=isbn, launched=False).all()
    trade_wishs = Wish.query.filter_by(isbn=isbn, launched=False).all()

    trade_gifts_models = TradeInfo(trade_gifts)
    trade_wishs_models = TradeInfo(trade_wishs)

    return render_template('book_detail.html',
                           book=book,
                           has_in_gifts=has_in_gifts,
                           has_in_wishs=has_in_wishs,
                           wishes=trade_wishs_models,
                           gifts=trade_gifts_models)
示例#11
0
def search():
    """
        q: 普通关键字或isbn
        page:
        ?q=金庸&page=1
        isbn isbn13 isbn13个0到9的数字组成
        isbn10 10个0到9的数字组成,含有一些' - '
    """
    # Request Response
    # HTTP的请求信息
    # 查询参数 POST参数 remote ip

    # isbn isbn13 isbn13个0到9的数字组成
    # isbn10 10个0到9的数字组成,含有一些' - '

    # q = request.args['q']
    # page = request.args['page']

    form = SearchForm(request.args)
    books = BookCollection()

    if form.validate():
        q = form.q.data.strip()
        page = form.page.data
        isbn_or_key = is_isbn_or_key(q)
        yushu_book = YushuBook()

        if isbn_or_key == 'isbn':
            yushu_book.search_by_isbn(q)
        else:
            yushu_book.search_by_keyword(q, page)

        books.fill(yushu_book, q)

    else:
        flash("搜索的关键字不符合要求,请重新输入关键字")
        # return jsonify(form.errors)
    return render_template('search_result.html', books=books)
示例#12
0
def search():
    form = SearcgForm(request.args)
    books = BookCollection()

    if form.validate():
        q = form.q.data.strip()
        page = form.page.data
        isbn_or_key = is_isbn_or_key(q)
        book = YushuBook()

        if isbn_or_key == 'isbn':
            book.search_by_isbn(q)
        else:
            book.search_by_keyword(q, page)

        books.fill(book, q)
        # return jsonify(books.__dict__)
        # print('books', books)
        # return json.dumps(books, default=lambda o:o.__dict__)
        return render_template('index.html', data=books)
    else:
        # return jsonify({'meg': '参数校验错误'})
        return jsonify(form.errors)
示例#13
0
def search():
    '''
    q:关键字 or ISBN号
    '''
    # q = request.args['q']
    # page = request.args['page']

    form = SearchForm(request.args)
    if form.validate():
        q = form.q.data.strip() #strip函数去除字符串前后的空格
        page = form.page.data
        isbn_or_key = is_isbn_or_key(q)
        if isbn_or_key == 'isbn':
            result = YushuBook.search_by_isbn(q)
        else:
            result = YushuBook.search_by_keyword(q)
        # return json.dumps(result),200,{'content-type':'application/json'}
        return jsonify(result)
    else:
        return jsonify(form.errors)
示例#14
0
 def book(self):
     yushu_book = YushuBook()
     yushu_book.search_by_isbn(self.isbn)
     return yushu_book.first