Skip to content

Commit f215b0b

Browse files
committed
fix: 修改模型层的实例方法为类方法
1 parent d42c793 commit f215b0b

2 files changed

Lines changed: 26 additions & 20 deletions

File tree

app/api/v1/book.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,42 +22,42 @@
2222
@login_required
2323
@Notify(template='{user.nickname}查询了一本图书', event='queryBook')
2424
def get_book(bid):
25-
book = Book().get_detail(bid)
25+
book = Book.get_detail(bid)
2626
return jsonify(book)
2727

2828

2929
@book_api.route('/', methods=['GET'])
3030
@login_required
3131
@Notify(template='{user.nickname}查询了所有图书', event='queryBooks')
3232
def get_books():
33-
books = Book().get_all()
33+
books = Book.get_all()
3434
return jsonify(books)
3535

3636

3737
@book_api.route('/search', methods=['GET'])
3838
def search():
3939
form = BookSearchForm().validate_for_api()
40-
books = Book().search_by_keywords(form.q.data)
40+
books = Book.search_by_keywords(form.q.data)
4141
return jsonify(books)
4242

4343

4444
@book_api.route('/', methods=['POST'])
4545
def create_book():
4646
form = CreateOrUpdateBookForm().validate_for_api()
47-
Book().new_book(form)
47+
Book.new_book(form)
4848
return Success(msg='新建图书成功')
4949

5050

5151
@book_api.route('/<bid>', methods=['PUT'])
5252
def update_book(bid):
5353
form = CreateOrUpdateBookForm().validate_for_api()
54-
Book().edit_book(bid, form)
54+
Book.edit_book(bid, form)
5555
return Success(msg='更新图书成功')
5656

5757

5858
@book_api.route('/<bid>', methods=['DELETE'])
5959
@route_meta(auth='删除图书', module='图书')
6060
@group_required
6161
def delete_book(bid):
62-
Book().remove_book(bid)
62+
Book.remove_book(bid)
6363
return Success(msg='删除图书成功')

app/models/book.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,44 @@ class Book(Base):
1616
summary = Column(String(1000))
1717
image = Column(String(50))
1818

19-
def get_detail(self, bid):
20-
book = self.query.filter_by(id=bid, delete_time=None).first()
19+
@classmethod
20+
def get_detail(cls, bid):
21+
book = cls.query.filter_by(id=bid, delete_time=None).first()
2122
if book is None:
2223
raise NotFound(msg='没有找到相关书籍')
2324
return book
2425

25-
def get_all(self):
26-
books = self.query.filter_by(delete_time=None).all()
26+
@classmethod
27+
def get_all(cls):
28+
books = cls.query.filter_by(delete_time=None).all()
2729
if not books:
2830
raise NotFound(msg='没有找到相关书籍')
2931
return books
3032

31-
def search_by_keywords(self, q):
32-
books = self.query.filter(Book.title.like('%' + q + '%'), Book.delete_time == None).all()
33+
@classmethod
34+
def search_by_keywords(cls, q):
35+
books = cls.query.filter(Book.title.like('%' + q + '%'), Book.delete_time == None).all()
3336
if not books:
3437
raise BookNotFound()
3538
return books
3639

37-
def new_book(self, form):
40+
@classmethod
41+
def new_book(cls, form):
3842
book = Book.query.filter_by(title=form.title.data, delete_time=None).first()
3943
if book is not None:
4044
raise ParameterException(msg='图书已存在')
4145

42-
self.create(
46+
Book.create(
4347
title=form.title.data,
4448
author=form.author.data,
4549
summary=form.summary.data,
4650
image=form.image.data,
4751
commit=True
4852
)
49-
return self
53+
return True
5054

51-
def edit_book(self, bid, form):
55+
@classmethod
56+
def edit_book(cls, bid, form):
5257
book = Book.query.filter_by(id=bid, delete_time=None).first()
5358
if book is None:
5459
raise NotFound(msg='没有找到相关书籍')
@@ -61,12 +66,13 @@ def edit_book(self, bid, form):
6166
image=form.image.data,
6267
commit=True
6368
)
64-
return self
69+
return True
6570

66-
def remove_book(self, bid):
67-
book = self.query.filter_by(id=bid, delete_time=None).first()
71+
@classmethod
72+
def remove_book(cls, bid):
73+
book = cls.query.filter_by(id=bid, delete_time=None).first()
6874
if book is None:
6975
raise NotFound(msg='没有找到相关书籍')
7076
# 删除图书,软删除
7177
book.delete(commit=True)
72-
return self
78+
return True

0 commit comments

Comments
 (0)