Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add like, unlike url route
  • Loading branch information
killpanda committed Sep 15, 2015
commit 1ce2f5d8a40666fbf1f812f587d39598c1d2567e
2 changes: 2 additions & 0 deletions firefly/views/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .category import CategoryApi, CategoryListApi
from .comment import ReplyApi
from .user import FollowUserApi, BlockUserApi
from .topic import LikePostApi

bp = Blueprint('api', __name__, url_prefix='/api')
api = Api(bp)
Expand All @@ -14,3 +15,4 @@
api.add_resource(FollowUserApi, '/users/<id>/follow')
api.add_resource(BlockUserApi, '/users/<id>/block')
api.add_resource(ReplyApi, '/posts/<int:id>/replies')
api.add_resource(LikePostApi, '/posts/<int:id>/like', endpoint='like')
26 changes: 26 additions & 0 deletions firefly/views/api/topic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# coding: utf-8

from __future__ import absolute_import

from flask_restful import Resource
from flask_security import login_required
from flask_login import current_user

from firefly.models.topic import Post


class LikePostApi(Resource):

method_decorators = [login_required]

def put(self, id):
post = Post.objects.get_or_404(id=id)
if post:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_or_404 已经判断过了不需要再次判断。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mozillazg 好的,谢谢

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

404 会直接raise

post.likes.add(current_user.id)
return '', 201

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

body 为空的话,状态码一般是 204。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

通常 201 是代表修改成功吧?204 No Content 是否有些奇怪? @mozillazg

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@killpanda @mozillazg +1 你可以看看状态码的说明 比如 https://zh.wikipedia.org/wiki/HTTP%E7%8A%B6%E6%80%81%E7%A0%81

如果只处理 但是返回空的body 正规的做法是204. 而且201一般都是POST

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

202也可以接受

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那我跟 api/user.py 统一吧,PUT 202 DELETE 204

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我又翻到一篇 https://developer.yahoo.com/social/rest_api_guide/http-response-codes.html

其实不同方法的状态码还是可以不一样的 关键是用途.


def delete(self, id):
post = Post.objects.get_or_404(id=id)
if post:
post.likes.delete(current_user.id)
return '', 201

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete 应该也是204