def get(self, _, post_id): if not is_uuid(post_id): error_message = json.dumps({'error': f"It's not uuid {post_id}"}) return Response(error_message, status=HTTP_400_BAD_REQUEST) like_count = Like.objects.filter(creator=get_user_profile( self.request), post_id__id=post_id).count() return Response(json.dumps({post_id: like_count}), status=HTTP_200_OK)
def post(self, request, *args, **kwargs): user_profile = get_user_profile(request) if user_profile.is_system: return Response( 'With great power comes great responsibility!', status=status.HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS) request.data['creator'] = user_profile.id return self.create(request, *args, **kwargs)
def post(self, request, *args, **kwargs): if not is_uuid(request.data['userId']): return Response('Invalid user', status=status.HTTP_400_BAD_REQUEST) user_profile = get_user_profile(request) if user_profile.is_system: return Response( 'Well, if computers could think, thereād be none of us here, would there?', status=status.HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS, ) request.data['creator'] = user_profile.id liked = Like.objects.filter(creator=request.data['creator'], user_id=request.data['userId'], post=request.data['post']).first() if liked: liked.delete() return Response(status=HTTP_200_OK) return self.create(request, *args, **kwargs)
def get_queryset(self): user_profile = get_user_profile(self.request) result = (Like.objects.values('post').filter( Q(post__creator__is_system=True) | Q(post__creator=user_profile), post__is_active=True, creator=user_profile, ).annotate(like_count=Count('post')).order_by('-like_count').values( 'post')[:self.TOP_LIMIT]) if not result: return Post.active.filter(creator__is_system=True)[:self.TOP_LIMIT] preserved = Case(*[ When(pk=pk, then=pos) for pos, pk in enumerate(map(lambda x: x['post'], result)) ]) return Post.active.filter(id__in=result).order_by(preserved)
def get(self, request, *args, **kwargs): raw_posts = self.request.GET.get(self.LIKE_PARAM) if not raw_posts: return self.create_error_message('This field is required.') posts = raw_posts.split(self.DIVIDER) if len(posts) > self.MAX_POSTS_COUNT: return self.create_error_message('Wow. Wow. Too much posts.') for post_uuid in posts: if not is_uuid(post_uuid): return self.create_error_message( "It's not UUID. Don't try to hack us. ^_^") user_profile = get_user_profile(self.request) post_list = Post.active.filter(id__in=posts) return Response( {str(post.id): post.get_like(user_profile) for post in post_list}, status=HTTP_200_OK)
def create(self, validated_data): validated_data['creator'] = get_user_profile( self.context.get("request")) like = Like.objects.create(**validated_data) return like
def get_queryset(self): user_profile = get_user_profile(self.request) return Post.active.filter( Q(creator__is_system=True) | Q(creator=user_profile))[:self.FRESH_LIMIT]
def get_queryset(self): user_profile = get_user_profile(self.request) return Passport.active.filter(creator=user_profile)
def post(self, request, *args, **kwargs): user_profile = get_user_profile(request) request.data['creator'] = user_profile.id return self.create(request, *args, **kwargs)