def vote_cancel(self, request, *args, **kwargs): article = self.get_object() if not Vote.objects.filter( voted_by=request.user, parent_article=article, ).exists(): return response.Response(status=status.HTTP_200_OK) vote = Vote.objects.get( voted_by=request.user, parent_article=article, ) vote.delete() article.update_vote_status() if vote.is_positive: pipe = redis.pipeline() redis_key = 'articles:vote' pipe.zadd(redis_key, { f'{article.id}:-1:{request.user.id}:{time.time()}': time.time() }) pipe.execute(raise_on_error=True) return response.Response(status=status.HTTP_200_OK)
def vote_positive(self, request, *args, **kwargs): article = self.get_object() if article.created_by_id == request.user.id: return response.Response({'message': '본인 글에는 좋아요를 누를 수 없습니다.'}, status=status.HTTP_403_FORBIDDEN) Vote.objects.update_or_create( voted_by=request.user, parent_article=article, defaults={ 'is_positive': True, }, ) article.update_vote_status() pipe = redis.pipeline() redis_key = 'articles:vote' pipe.zadd( redis_key, {f'{article.id}:1:{request.user.id}:{time.time()}': time.time()}) pipe.execute(raise_on_error=True) return response.Response(status=status.HTTP_200_OK)
def retrieve(self, request, *args, **kwargs): article = self.get_object() ArticleReadLog.objects.create( read_by=self.request.user, article=article, ) article.update_hit_count() pipe = redis.pipeline() redis_key = 'articles:hit' pipe.zadd(redis_key, { f'{article.id}:1:{self.request.user.id}:{time.time()}': time.time() }) pipe.execute(raise_on_error=True) return super().retrieve(request, *args, **kwargs)
def delete_block_from_redis(block): pipe = redis.pipeline() redis_key = f'blocks:{block.blocked_by_id}' pipe.zrem(redis_key, f'{block.user_id}') pipe.execute(raise_on_error=True)
def add_block_to_redis(block): pipe = redis.pipeline() redis_key = f'blocks:{block.blocked_by_id}' pipe.zadd(redis_key, {f'{block.user_id}': time.time()}) pipe.execute(raise_on_error=True)