def post(self, post_id=None): args = post_post_parser.parse_args(strict=True) new_post = Post(args['title']) new_post.user_id = get_jwt_identity() new_post.body = args['body'] if args['tags']: add_tags_to_post(new_post, args['tags']) db.session.add(new_post) db.session.commit() return {'id': new_post.id}, 201
def post(self): print(request.data) args = post_post_parser.parse_args(strict=True) new_post = Post(args["title"]) new_post.user_id = get_jwt_identity() new_post.text = args["text"] if args["tags"]: add_tags_to_post(new_post, args["tags"]) db.session.add(new_post) db.session.commit() return {"id": new_post.id}, 201
def generate_posts(n, users, tags): for i in range(n): post = Post() post.title = faker.sentence() post.text = faker.text(max_nb_chars=1000) post.publish_date = faker.date_this_century(before_today=True, after_today=False) post.user = users[random.randrange(0, len(users))].id post.tags = [tags[random.randrange(0, len(tags))] for i in range(0, 2)] try: post.save() except Exception as e: log.error("Fail to add post %s: %s" % (str(post), e))
def generate_dev_data(): tags = Tag.query.all() for i in range(USER_COUNT): profile = faker.profile() user = User() user.username = profile["username"] user.email = profile["mail"] user.name = profile["name"] for i in range(POST_COUNT): post = Post() post.title = faker.text(max_nb_chars=40) post.body = faker.text(max_nb_chars=4000) for tag in tags: post.tags.append(tag) user.posts.append(post) db.session.add(user) db.session.commit()