Пример #1
0
    def put(self, request):

        request_data = request.data.dict()

        circle_id = request_data.get('circle_id', None)
        if circle_id and isinstance(circle_id, str):
            circle_id = circle_id.split(',')
            request_data['circle_id'] = circle_id

        serializer = CreatePostSerializer(data=request_data,
                                          context={"request": request})
        serializer.is_valid(raise_exception=True)
        data = serializer.validated_data
        text = data.get('text')
        image = data.get('image')
        video = data.get(
            'video') if settings.FEATURE_VIDEO_POSTS_ENABLED else None
        circles_ids = data.get('circle_id')
        user = request.user

        with transaction.atomic():
            if circles_ids:
                post = user.create_encircled_post(text=text,
                                                  circles_ids=circles_ids,
                                                  image=image,
                                                  video=video)
            else:
                post = user.create_public_post(text=text,
                                               image=image,
                                               video=video)

        post_serializer = AuthenticatedUserPostSerializer(
            post, context={"request": request})

        return Response(post_serializer.data, status=status.HTTP_201_CREATED)
Пример #2
0
    def get(self, request):
        user = request.user

        posts = user.get_trending_posts()[:30]
        posts_serializer = AuthenticatedUserPostSerializer(
            posts, many=True, context={"request": request})
        return Response(posts_serializer.data, status=status.HTTP_200_OK)
Пример #3
0
    def put(self, request):

        request_data = request.data.dict()

        normalize_list_value_in_request_data('circle_id', request_data)

        serializer = CreatePostSerializer(data=request_data,
                                          context={"request": request})
        serializer.is_valid(raise_exception=True)
        data = serializer.validated_data
        text = data.get('text')
        image = data.get('image')
        video = data.get('video')
        circles_ids = data.get('circle_id')
        is_draft = data.get('is_draft')
        user = request.user

        with transaction.atomic():
            if circles_ids:
                post = user.create_encircled_post(text=text,
                                                  circles_ids=circles_ids,
                                                  image=image,
                                                  video=video,
                                                  is_draft=is_draft)
            else:
                post = user.create_public_post(text=text,
                                               image=image,
                                               video=video,
                                               is_draft=is_draft)

        post_serializer = AuthenticatedUserPostSerializer(
            post, context={"request": request})

        return Response(post_serializer.data, status=status.HTTP_201_CREATED)
Пример #4
0
    def get_posts_for_authenticated_user(self, request):
        query_params = request.query_params.dict()
        normalize_list_value_in_request_data('circle_id', query_params)
        normalize_list_value_in_request_data('list_id', query_params)

        serializer = GetPostsSerializer(data=query_params)
        serializer.is_valid(raise_exception=True)
        data = serializer.validated_data

        circles_ids = data.get('circle_id')
        lists_ids = data.get('list_id')
        max_id = data.get('max_id')
        min_id = data.get('min_id')
        count = data.get('count', 10)
        username = data.get('username')

        user = request.user

        if username:
            if username == user.username:
                posts = user.get_posts(max_id=max_id)
            elif not user.is_connected_with_user_with_username(username):
                User = get_user_model()
                posts = User.get_public_posts_for_user_with_username(
                    max_id=max_id, min_id=min_id, username=username)
            else:
                posts = user.get_posts_for_user_with_username(username,
                                                              max_id=max_id)
        else:
            posts = user.get_timeline_posts(circles_ids=circles_ids,
                                            lists_ids=lists_ids,
                                            max_id=max_id,
                                            min_id=min_id,
                                            count=count)

        posts = posts.order_by('-id')[:count]

        post_serializer_data = AuthenticatedUserPostSerializer(posts,
                                                               many=True,
                                                               context={
                                                                   "request":
                                                                   request
                                                               }).data

        return Response(post_serializer_data, status=status.HTTP_200_OK)