Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 30 additions & 0 deletions comments/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,33 @@ def test_update(self):
self.assertEqual(comment.created_at, before_created_at)
self.assertNotEqual(comment.created_at, now)
self.assertNotEqual(comment.updated_at, before_updated_at)

def test_list(self):
# must have tweet_id
response = self.anonymous_client.get(COMMENT_URL)
self.assertEqual(response.status_code, 400)

# you can visit with tweet_id
# but at the beginning, no comment
response = self.anonymous_client.get(
COMMENT_URL,
{'tweet_id' : self.tweet.id},
)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data['comments']), 0)

# comments order by time
self.create_comment(self.linghu, self.tweet, '1')
self.create_comment(self.dongxie, self.tweet, '2')
self.create_comment(self.dongxie, self.create_tweet(self.dongxie), '3')
response = self.anonymous_client.get(COMMENT_URL, {'tweet_id' : self.tweet.id})
self.assertEqual(len(response.data['comments']), 2)
self.assertEqual(response.data['comments'][0]['content'], '1')
self.assertEqual(response.data['comments'][1]['content'], '2')

#同时提供 userid 和 tweetid, 只有 tweetid 会在 filter 中生效
response = self.anonymous_client.get(COMMENT_URL, {
'tweet_id' : self.tweet.id,
'user_id' : self.linghu.id,
})
self.assertEqual(len(response.data['comments']), 2)
22 changes: 21 additions & 1 deletion comments/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
class CommentViewSet(viewsets.GenericViewSet):
serializer_class = CommentSerializerForCreate
queryset = Comment.objects.all()
filterset_fields = ('tweet_id', )

# POST /api/comments/ -> create
# GET /api/comments/ -> list
# GET /api/comments/?tweet_id= -> list
# Get /api/comments/1/ -> retrieve
# DELETE /api/comments/1/ -> destroy
# PATCH /api/comments/1/ -> partial_update
Expand All @@ -28,6 +29,25 @@ def get_permissions(self):
return [IsAuthenticated(), IsObjectOwner()]
return [AllowAny()]

def list(self, request, *args, **kwargs):
if 'tweet_id' not in request.query_params:
return Response({
'message' : 'missing tweet_id in request',
'success' : False,
}, status=status.HTTP_400_BAD_REQUEST)
# 这种写法在后期需要添加其他属性进行筛选就很方便
queryset = self.get_queryset()
comments = self.filter_queryset(queryset)\
.prefetch_related('user')\
.order_by('created_at')
# tweet_id = request.query_params['tweet_id']
# comments = Comment.objects.filter(tweet_id=tweet_id)
serializer = CommentSerializer(comments, many=True)
return Response({
'comments' : serializer.data,
}, status=status.HTTP_200_OK)



def create(self, request, *args, **kwargs):
data = {
Expand Down
2 changes: 1 addition & 1 deletion comments/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Comment(models.Model):

user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
tweet = models.ForeignKey(Tweet, on_delete=models.SET_NULL, null=True)
content = models.CharField(max_length=140)
content = models.TextField(max_length=140)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ chardet==3.0.4
cryptography==2.1.4
Django==3.1.3
django-debug-toolbar==3.2.4
django-filter==21.1
djangorestframework==3.12.2
idna==2.6
keyring==10.6.0
Expand Down
15 changes: 12 additions & 3 deletions twitter/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,16 @@
'django.contrib.staticfiles',
# third party
'rest_framework',
'debug_toolbar',
'django_filters',

# project apps
'accounts',
'tweets',
'friendships',
'newsfeeds',
'comments',
# debug tool
'debug_toolbar',

]

MIDDLEWARE = [
Expand Down Expand Up @@ -118,7 +119,10 @@

REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 10
'PAGE_SIZE': 10,
'DEFAULT_FILTER_BACKENDS' : [
'django_filters.rest_framework.DjangoFilterBackend',
],
}


Expand All @@ -140,3 +144,8 @@
# https://docs.djangoproject.com/en/3.1/howto/static-files/

STATIC_URL = '/static/'

try:
from .local_settings import *
except:
pass