# 9.3 书籍交易视图模型

书籍详情页，除了需要显示书籍详情信息外。还应该显示其他信息，这些信息分为三类 1.默认情况下，显示想要赠送这本书的人的列表，包括名字和上传时间。 2.如果当前用户是此书的赠送者，应该显示索要这本书的人的列表。 3.如果当前用户是此书的索要者，应该显示想要赠送这本书的人的列表。

综上所述，我们一共需要两个列表，这本书的索要人列表和这本书的赠书人的列表，根据不同情况进行展示。

```python
    # 赠书人列表和索要人列表
    trade_gifts = Gift.query.filter_by(isbn=isbn).all()
    trade_wishs = Wish.query.filter_by(isbn=isbn).all()
```

我们在view\_model中处理这两个列表的原始数据，加工成我们姓名，上传时间的列表。由于gifts,wishs两个的加工逻辑一样，只是数据库表不一样，所以可以写一个统一的类trade来处理

```python
class TradeInfo:

    def __init__(self, goods):
        self.total = 0
        self.trades = []
        self.__parse(goods)

    def __parse(self, goods):
        self.total = len(goods)
        self.trades = [self.__map_to_trade(single) for single in goods]

    def __map_to_trade(self, single):
        if single.create_datetime:
            time = single.create_datetime.strftime('%Y-%m-%d')
        else:
            time = '未知'
        return dict(
            user_name=single.user.nickname,
            time=time,
            id=single.id
        )
```

create\_time 本是int类型，要进行strftime格式化操作需要转化成string类型，这个操作每个模型都要用到，所以编写在base.py里

```python
    @property
    def create_datetime(self):
        if self.create_time:
            return str(self.create_time)
        else:
            return None
```

接下来完善书籍详情视图函数。区分上面说的三种情况。使用current\_user的is\_authenticated可以判断用户是否登录。然后分别以当前用户id为查询条件去wish表和gift表里查询，如果能查询到，则将对应的has\_in\_gifts/has\_in\_wishs设置为True

```python
@web.route("/book/<isbn>/detail")
def book_detail(isbn):
    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).first():
            has_in_gifts = True
        if Wish.query.filter_by(uid=current_user.id).first():
            has_in_wishs = True

    # 赠书人列表和索要人列表
    trade_gifts = Gift.query.filter_by(isbn=isbn).all()
    trade_wishs = Wish.query.filter_by(isbn=isbn).all()
    return render_template("book_detail.html", book=book,
                           wishes=trade_wishs, gifts=trade_gifts,
                           has_in_wishs=has_in_wishs, has_in_gifts=has_in_gifts)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://flask-yushu.gitbook.io/yushu/9.-shu-ji-jiao-yi-mo-xing/9.3-shu-ji-jiao-yi-shi-tu-mo-xing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
