Repository navigation
Like/dislike feature #115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Like/dislike feature #115
Changes from 1 commit
891b122
1ce2f5d
9dd4384
08b07d0
91a6fa7
2cb3c87
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| 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: | ||
| post.likes.add(current_user.id) | ||
| return '', 201 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. body 为空的话,状态码一般是 204。
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 通常 201 是代表修改成功吧?204 No Content 是否有些奇怪? @mozillazg
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 202也可以接受
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 那我跟 api/user.py 统一吧,PUT 202 DELETE 204
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. delete 应该也是204 |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
get_or_404已经判断过了不需要再次判断。There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mozillazg 好的,谢谢
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
404 会直接raise