def populate_db(): """Add 'personalwebapp' tag, first post to database.""" from datetime import datetime from models import Post, Tag, post_to_tag Post.query.delete() Tag.query.delete() db.session.execute(post_to_tag.delete()) personalwebapp = Tag(name='personalwebapp') db.session.add(personalwebapp) post = Post(title='Hello PersonalWebApp!', content=FIRST_POST, author_id=1, timezone=-5) post.tags.append(personalwebapp) post.state_id = 2 post.published_at = datetime.utcnow() post.url = 'hello_personal_webapp' db.session.add(post) post = Post(title='Example Draft Post', content='This is the first draft. I\'ll finish it later.', author_id=1, timezone=-5) post.url = 'example_draft_post' db.session.add(post) db.session.commit() print('"personalwebapp" tag' 'and "Hello PersonalWebApp!" published post and "Example Draft Post" draft post have been generated.')
def create_tags_posts(self): post1 = Post(title='title one', content='content one', author_id=1, timezone=0) post2 = Post(title='title two', content='content two', author_id=1, timezone=0) post3 = Post(title='title three', content='content three', author_id=1, timezone=0) post1.url = 'post_one' post1.state_id = 2 post2.url = 'post_two' post2.state_id = 1 post3.url = 'post_three' tag_a = Tag(name='tag_a') tag_b = Tag(name='tag_b') post1.tags = [tag_a, tag_b] post2.tags = [tag_a] with self.get_context(): db.session.add(tag_a) db.session.add(tag_b) db.session.add(post1) db.session.add(post2) db.session.add(post3) db.session.commit()