def create(self, request, *args, **kwargs): data = { 'user_id': request.user.id, 'tweet_id': request.data.get('tweet_id'), 'content': request.data.get('content'), } # validate the input by CommentSerializerForCreate serializer = CommentSerializerForCreate(data=data) if not serializer.is_valid(): return Response( { 'success': False, 'message': 'Invalid Comment Input', 'error': serializer.errors, }, status=status.HTTP_400_BAD_REQUEST) # save validated comment comment = serializer.save() # raise notification for comment creation NotificationService.send_comment_notification(comment) serializer = CommentSerializer( instance=comment, context={'request': request}, ) return Response(serializer.data, status=status.HTTP_201_CREATED)
def create(self, request, *args, **kwargs): data = { 'user_id': request.user.id, 'tweet_id': request.data.get('tweet_id'), 'content': request.data.get('content'), } # Here must have 'data=' to assign params to data # because the first default param is instance serializer = CommentSerializerForCreate(data=data) if not serializer.is_valid(): return Response( { 'message': 'Please check input', 'errors': serializer.errors, }, status=status.HTTP_400_BAD_REQUEST) # the save() method would trigger create() method in serializer, click # into save() to see its implementation. comment = serializer.save() NotificationService.send_comment_notification(comment) return Response( CommentSerializer(comment, context={ 'request': request }).data, status=status.HTTP_201_CREATED, )
def create(self, request, *args, **kwargs): data = { 'user_id': request.user.id, 'tweet_id': request.data.get('tweet_id'), 'content': request.data.get('content'), } # 注意这里必须要加 'data=' 来指定参数是传给 data 的 # 因为默认的第一个参数是 instance serializer = CommentSerializerForCreate(data=data) if not serializer.is_valid(): return Response( { 'message': 'Please check input', 'errors': serializer.errors, }, status=status.HTTP_400_BAD_REQUEST) # save 方法会触发 serializer 里的 create 方法,点进 save 的具体实现里可以看到 comment = serializer.save() NotificationService.send_comment_notification(comment) return Response( CommentSerializer(comment, context={ 'request': request }).data, status=status.HTTP_201_CREATED, )
def create(self, request, *args, **kwargs): data = { 'user_id': request.user.id, 'tweet_id': request.data.get('tweet_id'), 'content': request.data.get('content') } serializer = CommentSerializerForCreate(data=data) if not serializer.is_valid(): return Response( { 'message': 'Please check input', 'errors': serializer.errors, }, status=status.HTTP_400_BAD_REQUEST) comment = serializer.save() NotificationService.send_comment_notification(comment) return Response(CommentSerializer(comment, context={ 'request': request }).data, status=status.HTTP_201_CREATED)
def test_send_like_notification(self): like = self.create_like(self.linghu, self.linghu_tweet) NotificationService.send_like_notification(like) self.assertEqual(Notification.objects.count(), 0) like = self.create_like(self.dongxie, self.linghu_tweet) NotificationService.send_like_notification(like) self.assertEqual(Notification.objects.count(), 1)
def test_send_comment_notification(self): comment = self.create_comment(self.linghu, self.linghu_tweet) NotificationService.send_comment_notification(comment) self.assertEqual(Notification.objects.count(), 0) comment = self.create_comment(self.dongxie, self.linghu_tweet) NotificationService.send_comment_notification(comment) self.assertEqual(Notification.objects.count(), 1)
def test_send_comment_notification(self): # do not dispatch notification if tweet user == comment user comment = self.create_comment(self.linghu, self.linghu_tweet) NotificationService.send_comment_notification(comment) self.assertEqual(Notification.objects.count(), 0) # dispatch notification if tweet user != comment user comment = self.create_comment(self.dongxie, self.linghu_tweet) NotificationService.send_comment_notification(comment)
def test_send_comment_notification(self): # do not dispatch notification if tweet user == comment user comment = self.create_comment(self.hanyuan, self.hanyuan_tweet) NotificationService.send_comment_notification(comment) self.assertEqual(Notification.objects.count(), 0) # dispatch notification if tweet user != comment user comment = self.create_comment(self.eric, self.hanyuan_tweet) NotificationService.send_comment_notification(comment) self.assertEqual(Notification.objects.count(), 1)
def create(self, validated_data): model_class = self._get_model_class(validated_data) instance, created = Like.objects.get_or_create( content_type=ContentType.objects.get_for_model(model_class), object_id=validated_data['object_id'], user=self.context['request'].user, ) if created: NotificationService.send_like_notification(instance) return instance
def test_send_like_notification(self): # do not dispatch notification if tweet user == like user like = self.create_like(self.user1, self.user1_tweet) NotificationService.send_like_notification(like) self.assertEqual(Notification.objects.count(), 0) # dispatch notification if tweet user != comment user like = self.create_comment(self.user2, self.user1_tweet) NotificationService.send_comment_notification(like) self.assertEqual(Notification.objects.count(), 1)
def test_send_comment_notification(self): # dispatch notifications if comment on others tweets comment1 = self.create_comment(user=self.user2, tweet=self.tweet) NotificationService.send_comment_notification(comment1) self.assertEqual(Notification.objects.count(), 1) # do not dispatch notifications if comment self-owned tweet comment2 = self.create_comment(user=self.user1, tweet=self.tweet) NotificationService.send_comment_notification(comment2) self.assertEqual(Notification.objects.count(), 1)
def test_send_like_notification(self): # do not dispatch notification if tweet user == like user like = self.create_like(self.rui, self.rui_tweet) NotificationService.send_like_notification(like) self.assertEqual(Notification.objects.count(), 0) # dispatch notification if tweet user != like user like = self.create_like(self.ming, self.rui_tweet) NotificationService.send_like_notification(like) self.assertEqual(Notification.objects.count(), 1)
def create(self, request, *args, **kwargs): serializer = LikeSerializerForCreate(data=request.data, context={'request': request}) if not serializer.is_valid(): return Response( { 'message': 'Please check input', 'errors': serializer.errors, }, status=status.HTTP_400_BAD_REQUEST) instance, created = serializer.get_or_create() if created: NotificationService.send_like_notification(instance) return Response(LikeSerializer(instance).data, status=status.HTTP_201_CREATED)
def async_create_like_main_task(content_type_int, object_id, user_id): if content_type_int == 0: model_class = Comment else: model_class = Tweet # first get_or_create Like instance, created = Like.objects.get_or_create( content_type=ContentType.objects.get_for_model(model_class), object_id=object_id, user_id=user_id, ) # send notification if created: NotificationService.send_like_notification(instance) return '1 like created.'
def create(self, request): serializer = LikeSerializerForCreate( data=request.data, context={'request': request}, ) if not serializer.is_valid(): return Response({ 'success': False, 'error': serializer.errors, }, status=status.HTTP_400_BAD_REQUEST) like, created = serializer.get_or_create() # raise notifications in creation, do not dispatch notifications if liked if created: NotificationService.send_like_notification(like) return Response({ 'success': True, 'like': LikeSerializer(like).data }, status=status.HTTP_200_OK)
def update(self, request, *args, **kwargs): # get_object 是 DRF 包装的一个函数,会在找不到的时候 raise 404 error # 所以这里无需做额外判断 comment = self.get_object() serializer = CommentSerializerForUpdate( instance=comment, data=request.data, ) if not serializer.is_valid(): raise Response({'message': 'Please check input'}, status=status.HTTP_400_BAD_REQUEST) # save 方法会触发 serializer 里的 update 方法,点进 save 的具体实现里可以看到 # save 是根据 instance 参数有没有传来决定是触发 create 还是 update comment = serializer.save() NotificationService.send_comment_notification(comment) return Response( CommentSerializer(comment, context={ 'request': request }).data, status=status.HTTP_200_OK, )
def create(self, request, *args, **kwargs): data = { 'user_id': request.user.id, 'tweet_id': request.data.get('tweet_id'), 'content': request.data.get('content'), } # must 'data=' specify param is to data # default first param is instance serializer = CommentSerializerForCreate(data=data) if not serializer.is_valid(): return Response({ 'message': 'Please check input', 'errors': serializer.errors, }, status=status.HTTP_400_BAD_REQUEST) # save method will trigger create() comment = serializer.save() NotificationService.send_comment_notification(comment) return Response( CommentSerializer(comment, context={'request': request}).data, status=status.HTTP_201_CREATED )
def create(self, request, *args, **kwargs): # 这里是直接构造一个data dict,里面包含我们create要用到的参数 # 也可以像之前一样, 把request当成context传进去: # serializer = CommentSerializerForCreate( # data=request.data, # context={'request': request}, # ) data = { 'user_id': request.user.id, 'tweet_id': request.data.get('tweet_id'), 'content': request.data.get('content'), } # 注意这里必须要加 'data=' 来指定参数是传给 data 的 # 因为默认的第一个参数是 instance serializer = CommentSerializerForCreate(data=data) # serializer = CommentSerializerForCreate( # data=request.data, # context={'request': request}, # ) if not serializer.is_valid(): return Response( { 'success': False, 'message': 'Please check input.', 'errors': serializer.errors, }, status=status.HTTP_400_BAD_REQUEST) # save 方法会触发 serializer 里的 create 方法,点进 save 的具体实现里可以看到 comment = serializer.save() # 发送 comment notification NotificationService.send_comment_notification(comment) return Response( CommentSerializer(comment, context={ 'request': request }).data, status=status.HTTP_201_CREATED, )
def create(self, request, *args, **kwargs): data = { 'user_id': request.user.id, 'tweet_id': request.data.get('tweet_id'), 'content': request.data.get('content'), } # note that here we need to add 'data=' to specify that those params are to be passed to data # because by default the first parameter is meant to be instance serializer = CommentSerializerForCreate(data=data) if not serializer.is_valid(): return Response({ 'message': 'Please check input', 'errors': serializer.errors, }, status=status.HTTP_400_BAD_REQUEST) # save will trigger the create method inside serializer comment = serializer.save() NotificationService.send_comment_notification(comment) return Response( CommentSerializer(comment, context={'request': request}).data, status=status.HTTP_201_CREATED, )
def test_send_like_notification(self): # do not dispatch notifications if like self-owned tweet like1 = self.create_like(user=self.user1, object=self.tweet) NotificationService.send_like_notification(like1) self.assertEqual(Notification.objects.count(), 0) # dispatch notifications if like on others tweets like2 = self.create_like(user=self.user2, object=self.tweet) NotificationService.send_like_notification(like2) self.assertEqual(Notification.objects.count(), 1) comment = self.create_comment(user=self.user2, tweet=self.tweet) # do not dispatch notifications if like self-owned comment like3 = self.create_like(user=self.user2, object=comment) NotificationService.send_like_notification(like3) self.assertEqual(Notification.objects.count(), 1) # dispatch notifications if like others comments like4 = self.create_like(user=self.user1, object=comment) NotificationService.send_like_notification(like4) self.assertEqual(Notification.objects.count(), 2)