def test_list(self): first_movie = models.Movie(title='Song of Water and Mud', year='2020', runtime='136 min', genre='Fantasy', external_api_response={}) first_movie.save() second_movie = models.Movie(title='Song of Ice and Fire', year='2020', runtime='136 min', genre='Fantasy', external_api_response={}) second_movie.save() comment = models.Comment(movie=first_movie, text_body='It is my first comment.') comment.save() comment = models.Comment(movie=first_movie, text_body='It is my second comment.') comment.save() comment = models.Comment(movie=second_movie, text_body='It is my third comment.') comment.save() response = self.client.get('/comments/') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()['comments']), 3) response = self.client.get('/comments/?movie_pk={}'.format(second_movie.pk)) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()['comments']), 1)
def test_top(self): first_movie = models.Movie(title='Song of Water and Mud 1', year='2020', runtime='136 min', genre='Fantasy', external_api_response={}) first_movie.save() comment = models.Comment(movie=first_movie, text_body='1') comment.save() comment = models.Comment(movie=first_movie, text_body='1') comment.save() second_movie = models.Movie(title='Song of Water and Mud 2', year='2020', runtime='136 min', genre='Fantasy', external_api_response={}) second_movie.save() comment = models.Comment(movie=second_movie, text_body='1') comment.save() third_movie = models.Movie(title='Song of Water and Mud 3', year='2020', runtime='136 min', genre='Fantasy', external_api_response={}) third_movie.save() comment = models.Comment(movie=third_movie, text_body='1') comment.save() fourth_movie = models.Movie(title='Song of Water and Mud 4', year='2020', runtime='136 min', genre='Fantasy', external_api_response={}) fourth_movie.save() now = django.utils.timezone.now() day_ago = now - timedelta(days=1) start = day_ago.timestamp() end = now.timestamp() response = self.client.get('/top/?start={}&end={}'.format(start, end)) self.assertEqual(response.status_code, 200) self.assertListEqual(response.json(), [{'movie_id': first_movie.pk, 'rank': 1, 'total_comments': 2}, {'movie_id': second_movie.pk, 'rank': 2, 'total_comments': 1}, {'movie_id': third_movie.pk, 'rank': 2, 'total_comments': 1}])
def post(request): serializer = serializers.CreateCommentSerializer(data=request.data) if not serializer.is_valid(): return response.Response(status=400) comment = models.Comment( movie=serializer.validated_data['movie'], text_body=serializer.validated_data['text_body']) comment.save() return response.Response(status=200, data={'comment': comment.to_json()})
def comment(self, request, pk=None): event = self.get_object() user = request.user content = request.data.get("content") comment = models.Comment(user=user, event=event, content=content) comment.save() return Response( { 'action': 'comment', 'event': event.id, 'user': user.id, 'content': content }, status=HTTP_200_OK)
# -*- coding:utf-8 -*- # Author:cqk # Data:2020/1/15 12:44 import os import sys import random import django # 添加环境变量 base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(base_dir) os.environ.setdefault("DJANGO_SETTINGS_MODULE", "auction.settings") django.setup() # 读取配置文件 from api import models user_objs = models.UserInfo.objects.all() article_objs = models.Article.objects.all() string = "的是咖啡色大鬼撒会否啊回复i话啊ui发会返回爱国覅给覅噶覅u阿哥覅给覅u阿飞贵啊发给i分别是嗲u戈辉阿哥覅iu啊v看不发达发布啊和哈维杀ui划分方法都是发货服啊和 是" models.Comment.objects.bulk_create([ models.Comment(content="".join( random.sample(string, random.randint(1, len(string)))), article=random.choice(article_objs), commentator=random.choice(user_objs)) for i in range(100) ])