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) # use Message Queue to create Like in DB if serializer.validated_data['content_type'] == 'comment': content_type_int = 0 model_class = Comment else: content_type_int = 1 model_class = Tweet object_id = serializer.validated_data['object_id'] LikeService.async_create_like(content_type_int, object_id, request.user.id) instance = Like( content_type=ContentType.objects.get_for_model(model_class), object_id=object_id, user=request.user, ) return Response( LikeSerializer(instance).data, status=status.HTTP_201_CREATED, )
def get_has_liked(self, obj): return LikeService.has_liked(self.context['request'].user, obj)
def _get_has_liked(self): return LikeService.has_liked(data['request'].user, self)
def get_has_liked(self, obj): # current login user can be obtained from self.context['request'].user return LikeService.has_liked(self.context['request'].user, obj)
def get_has_liked(self, obj): # current login user has this like return LikeService.has_liked(self.context['request'].user, obj)
def get_has_liked(self, obj): # 这个request.user 只是当下发送请求的用户,可以是登录的也可以没有登录 return LikeService.has_liked(self.context['request'].user, obj)