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_like_notification(self): # do not dispatch notification if tweet user == like user like = self.create_like(self.linghu, self.linghu_tweet) NotificationService.send_like_notification(like) self.assertEqual(Notification.objects.count(), 0) # dispatch notification if tweet user != comment user like = self.create_comment(self.dongxie, self.linghu_tweet) NotificationService.send_comment_notification(like) 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.hanyuan, self.hanyuan_tweet) NotificationService.send_like_notification(like) self.assertEqual(Notification.objects.count(), 0) # dispatch notification if tweet user != comment user like = self.create_like(self.eric, self.hanyuan_tweet) NotificationService.send_like_notification(like) 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.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 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)