Esempio n. 1
0
    def create(self):
        """ REST create method

        Returns:
            A JSON object with the new Post information

        Raises:
            LoginException: if user is not logged in
            FormException: if Post info is not valid

        """

        if not is_login():
            raise LoginException(message='not logged in')

        post = Post(
            title=request.form.get('title'),
            content=request.form.get('content'),
        )

        if post.is_valid():
            key = post.put()
            post_dict = key.get().to_dict()
            post_dict['id'] = key.id()
            user_key = post_dict['user']
            post_dict['isOwner'] = current_user_id() == user_key.id()
            post_dict.pop('user', None)

            return jsonify(**post_dict)
        else:
            raise FormException(message='invalid post data',
                                payload=post.errors())
Esempio n. 2
0
    def index(self, user_id=None):
        """ REST index method

        Params:
            cursor: A web safe string that represents the next page of pagination || None
            search: The search string to search for || None

        Args:
            user_id: The post forgien key that represents the user it belongs to

        Returns:
            An array of 15 JSON Post objects
        """

        json_results = {}
        if user_id:
            json_results = Post.users_with_pages(
                user_id=user_id, cursor=request.args.get('cursor'))
        elif bool(request.args.get('search')):
            json_results = Post.search(
                search_string=request.args.get('search'),
                cursor=request.args.get('cursor'))
        else:
            json_results = Post.all_with_pages(
                cursor=request.args.get('cursor'))

        return jsonify(**json_results)
Esempio n. 3
0
 def test_post_creation(self):
     """" ensure post is being created """
     mock_user = mixer.blend(User)
     post = Post(user=mock_user,
                 title=self.faker.sentence(),
                 body=self.faker.sentence())
     post.save()
     self.assertEqual(Post.objects.count(), 1)
Esempio n. 4
0
    def delete(self, post_id):
        """ REST delete method

        Note:
            This also updates the corresponding Post by subtracting 1 to its like property

        Args:
            post_id: The post's id that will be unliked

        Returns:
            A JSON object with a simple deleted key value true

        Raises:
            LoginException: if user is not logged in
            OwnerException: if like was not found
        """

        if not is_login():
            raise LoginException(message='not logged in')

        like = Like.get_by_post_user(long(post_id), current_user_id())

        if like:
            post = Post.get_by_id(long(post_id))
            post.likes = post.likes - 1
            post.put()
            like.key.delete()
            return jsonify(delete=True)
        else:
            raise OwnerException(message='not auth')
Esempio n. 5
0
    def is_valid(self):
        """ Check if Like is valid

        Returns:
            True if user and post exist and the user is not the owner of the post
        """

        user = User.get_by_id(self.user.id())
        post = Post.get_by_id(self.post.id())
        if user and post and not (post.user.id() == user.key.id()):
            return True
        return False
Esempio n. 6
0
    def create(self, post_id):
        """ REST create method

        Note:
            This also updates the corresponding Post by adding 1 to its like property

        Args:
            post_id: The post's id that will be liked

        Returns:
            A JSON object with the like information

        Raises:
            LoginException: if user is not logged in
            FormException: if the like is invalid || like already exist
        """

        if not is_login():
            raise LoginException(message='not logged in')

        like = Like(
            post=ndb.Key('Post', long(post_id)),
            user=ndb.Key('User', long(current_user_id())),
        )

        if like.is_valid() and not Like.already_exist(like.post.id(),
                                                      like.user.id()):
            post = Post.get_by_id(long(post_id))
            post.likes = post.likes + 1
            post.put()

            key = like.put()
            ld = like.to_dict()
            ld['id'] = key.id()
            ld['post'] = like.post.id()
            ld['user'] = like.user.id()
            return jsonify(**ld)
        else:
            raise FormException(message='not auth')
Esempio n. 7
0
    def get(self, post_id):
        """ REST get method

        Args:
            post_id: The id the post to get

        Returns:
            A JSON Post object

        Raises:
            OwnerException: if post is not found
        """

        post = Post.get_by_id(long(post_id))
        if post:
            post_dict = post.to_dict()
            post_dict['id'] = post.key.id()
            user_key = post_dict['user']
            post_dict['isOwner'] = current_user_id() == user_key.id()
            post_dict.pop('user', None)
            return jsonify(**post_dict)
        else:
            raise OwnerException(message='unauth data')
Esempio n. 8
0
    def delete(self, post_id):
        """ REST delete method

        Args:
            post_id: The post's id that will be deleted

        Returns:
            A JSON object key deleted set to true

        Raises:
            LoginException: if user is not logged in
            OwnerException: if Post does not belong to current user
        """

        if not is_login():
            raise LoginException(message='not logged in')

        post = Post.get_by_id(long(post_id))
        if post and post.user.id() == current_user_id():
            post.key.delete()
            return jsonify(deleted=True)
        else:
            raise OwnerException(message='unauth data')
Esempio n. 9
0
async def create_post_api(post: PostInSerializer, current_user=Depends(get_current_user)):
    post = Post(title=post.title, created_by=current_user.id, content=post.content, comments=[])
    await post.commit()
    post = post.dump()
    return post
Esempio n. 10
0
async def get_posts_api():
    cursor = Post.find()
    posts = [post.dump() for post in await cursor.to_list(length=10)]
    return posts