def create_post_detail(): form = CreatePostForm() post_type = session["post_type"] is_video = True if post_type == "video" else False if form.validate_on_submit(): if is_video: content = form.video_id.data else: content = form.text.data post = Post( title=form.title.data, is_video=is_video, content=content, author=current_user, ) db.session.add(post) db.session.commit() return redirect(url_for("main.index")) return render_template("create_post.html", title="Create Post", is_video=is_video, form=form)
def fill_db(): u1 = User(username='******', email='*****@*****.**') u2 = User(username='******', email='*****@*****.**') u3 = User(username='******', email='*****@*****.**') u4 = User(username='******', email='*****@*****.**') u5 = User(username='******', email='*****@*****.**') users = [u1, u2, u3, u4, u5] db.session.add_all(users) db.session.commit() for u in users: u.set_password(u.username.lower()) u.last_seen = datetime.utcnow() u.about_me = f"This is bio of {u.username}" ci = users.index(u) u.follow(users[ci-1]) u.follow(users[ci-2]) for i in range(1, 4): db.session.add(Post(body=f"Port from {u.username} #{i}", author=u, timestamp=datetime.utcnow())) db.session.commit()
def new_post(): form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, content=form.content.data) db.session.add(post) db.session.commit() flash('Your post has been created!', 'success') return redirect(url_for('home')) return render_template('create_post.html', title='New Post', form=form, legend='New Post')
def new_post(): form = PostForm() if form.validate_on_submit(): #include all the attributes of a post when creating a post post = Post(title=form.title.data, content=form.content.data, author=current_user) db.session.add(post) db.session.commit() flash('Your post has been created!', 'success') return redirect(url_for('main.home')) return render_template('create_post.html', title='New Post', form=form, lengend='New Post')
def search(): #TODO: Validation problem with csrf token on pagination if not g.search_form.validate(): return redirect(url_for('main.explore')) page = request.args.get('page', 1, type=int) posts, total = Post.search(g.search_form.q.data, page, current_app.config['POSTS_PER_PAGE']) next_url = url_for('main.search', q=g.search_form.q.data, page=page+1) \ if total > page * current_app.config['POSTS_PER_PAGE'] else None prev_url = url_for('main.search', q=g.search_form.q.data, page=page-1) \ if page > 1 else None return render_template('search.html', title=_('Search'), posts=posts, next_url=next_url, prev_url=prev_url)
def test_follow_posts(self): # create four users u1 = User(username='******', email='*****@*****.**') u2 = User(username='******', email='*****@*****.**') u3 = User(username='******', email='*****@*****.**') u4 = User(username='******', email='*****@*****.**') db.session.add_all([u1, u2, u3, u4]) # create four posts now = datetime.utcnow() p1 = Post(body="post from john", author=u1, created_at=now + timedelta(seconds=1)) p2 = Post(body="post from susan", author=u2, created_at=now + timedelta(seconds=4)) p3 = Post(body="post from mary", author=u3, created_at=now + timedelta(seconds=3)) p4 = Post(body="post from david", author=u4, created_at=now + timedelta(seconds=2)) db.session.add_all([p1, p2, p3, p4]) db.session.commit() # setup the followers u1.follow(u2) # john follows susan u1.follow(u4) # john follows david u2.follow(u3) # susan follows mary u3.follow(u4) # mary follows david db.session.commit() # check the followed posts of each user f1 = u1.followed_posts().all() f2 = u2.followed_posts().all() f3 = u3.followed_posts().all() f4 = u4.followed_posts().all() self.assertEqual(f1, [p2, p4, p1]) self.assertEqual(f2, [p2, p3]) self.assertEqual(f3, [p3, p4]) self.assertEqual(f4, [p4])
def test_follow_posts(self): user1 = User(username='******', email='*****@*****.**') user2 = User(username='******', email='*****@*****.**') user3 = User(username='******', email='*****@*****.**') user4 = User(username='******', email='*****@*****.**') db.session.add_all([user1, user2, user3, user4]) now = datetime.utcnow() post1 = Post(body='post from sara', author=user1, timestamp=now - timedelta(seconds=8)) post2 = Post(body='post from rob', author=user2, timestamp=now - timedelta(seconds=6)) post3 = Post(body='post from mara', author=user3, timestamp=now - timedelta(seconds=4)) post4 = Post(body='post from jack', author=user4, timestamp=now - timedelta(seconds=2)) db.session.add_all([post1, post2, post3, post4]) db.session.commit() user1.follow(user2) user1.follow(user4) user2.follow(user3) user3.follow(user4) f1 = user1.followed_posts().all() f2 = user2.followed_posts().all() f3 = user3.followed_posts().all() f4 = user4.followed_posts().all() self.assertEqual(f1, [post4, post2, post1]) self.assertEqual(f2, [post3, post2]) self.assertEqual(f3, [post4, post3]) self.assertEqual(f4, [post4])
def index(): form = forms.PostForm() if form.validate_on_submit(): post = Post(body=form.post.data, author=current_user) db.session.add(post) db.session.commit() flash(_("You post is live now")) return redirect(url_for('main.index')) page = request.args.get('page', 1, type=int) posts = current_user.followed_posts()\ .paginate(page, current_app.config['POSTS_PER_PAGE'], False) next_url = url_for('main.index', page=posts.next_num) if posts.has_next else None prev_url = url_for('main.index', page=posts.prev_num) if posts.has_prev else None return render_template('index.html', title='Home', form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
from flask_app import create_app, db from flask_app.models import User, Post from secrets import initial_users app = create_app() if __name__ == '__main__': with app.app_context(): for user in initial_users: u = User(username=user['username'], email=user['email'], about_me=user['about_me']) u.set_password(user['password']) db.session.add(u) db.session.commit() p = Post(body='my first post!', author=u) db.session.add(p) p = Post(body='my second post!', author=u) db.session.add(p) p = Post(body='now im bored', author=u) db.session.add(p) db.session.commit() print('DB created successfully')