Example #1
0
    def post(self, user):
        """
        API for creating a new post

        :param user: authenticated user, returned by auth.token_required function
        :return: json of a new created post

        example:
        curl -H "Content-Type: application/json" -H "X-Api-Key: <USER_TOKEN>"
        -d '{"text": "user has created a new post!!!"}' http://127.0.0.1:5000/api/posts

        where <USER_TOKEN> must be replaced by an authenticated user's token
        """

        args = request.get_json()

        if not args:
            log_activity("tried to add post without text", user_id=user.id)

            return {"error": "no post text provided"}, 401

        new_post = Post(author_id=user.id, text=args["text"])

        db.session.add(new_post)
        db.session.commit()

        log_activity("new post added", user_id=user.id, post_id=new_post.id)

        return new_post.json(), 201
Example #2
0
    def setUp(self):
        self.app = app.test_client()

        user1 = User(name="name",
                     surname="surname",
                     password="******",
                     username="******")
        user2 = User(name="name2",
                     surname="surname2",
                     password="******",
                     username="******")

        db.session.add(user1)
        db.session.add(user2)

        post1 = Post(author_id=user1.id, text="Directed by R. B. Weirde")
        post2 = Post(
            author_id=user1.id,
            text="My second post: I've just turned 18! Congrats to me!!!")
        post3 = Post(author_id=user2.id,
                     text="I think I`m very good at doing nothing")
        post4 = Post(author_id=user2.id, text="Йой, най буде")

        db.session.add(post1)
        db.session.add(post2)
        db.session.add(post3)
        db.session.add(post4)

        db.session.commit()

        self.posts_json = [
            post1.json(),
            post2.json(),
            post3.json(),
            post4.json()
        ]
        self.users_json = [user1.json(), user2.json()]