def preview_post(): form = PostForm() session['post_preview'] = form.data post = Post(**{k: v for k, v in session['post_preview'].items() if k not in ('next', 'tags')}) post.tags = [Tag(name=tag) for tag in form.tags.data] return render_template('admin/post_preview.html', title='Post preview', post=post)
def seed(): from app.main.models import Post p1 = Post() p1.body = "Hello, welcome to the World!" db.session.add(p1) db.session.commit() pass
def test_follow_posts(self): user1 = User(first_name='Test1', last_name='User', email='*****@*****.**') user2 = User(first_name='Test2', last_name='User', email='*****@*****.**') user3 = User(first_name='Test3', last_name='User', email='*****@*****.**') user4 = User(first_name='Test4', last_name='User', email='*****@*****.**') db.session.add_all([user1, user2, user3, user4]) p1 = Post(title='Test Post 1', body='Test post body 1', author=user1, timestamp=datetime.now() + timedelta(seconds=1)) p2 = Post(title='Test Post 2', body='Test post body 2', author=user2, timestamp=datetime.now() + timedelta(seconds=2)) p3 = Post(title='Test Post 3', body='Test post body 3', author=user3, timestamp=datetime.now() + timedelta(seconds=3)) p4 = Post(title='Test Post 4', body='Test post body 4', author=user4, timestamp=datetime.now() + timedelta(seconds=4)) db.session.add_all([p1, p2, p3, p4]) db.session.commit() user1.follow(user2) user1.follow(user4) user2.follow(user3) user3.follow(user4) db.session.commit() f1 = user1.followed_posts().all() f2 = user2.followed_posts().all() f3 = user3.followed_posts().all() f4 = user4.followed_posts().all() self.assertEqual(f1, [p4, p2, p1]) self.assertEqual(f2, [p3, p2]) self.assertEqual(f3, [p4, p3]) self.assertEqual(f4, [p4])
def fake_posts(count): categories = Category.query.filter_by(level=CategoryLevels.LEVEL_III).all() user = User.query.filter_by(email='*****@*****.**').first() for i in range(count): category = random.choice(categories) post = Post( title=' '.join(fake.words(nb=random.randint(5, 15))), content='\n'.join(fake.paragraphs(nb=random.randint(5, 10))), ) post.category = category post.author = user.id db.session.add(post)
def add_article(): form = AddArticleForm() if form.validate_on_submit(): title = form.title.data body = form.body.data tag_name = form.keywords.data tag = Tags.objects(name=tag_name).first() post = Post(title=title, body=body) post.tags.append(tag) post.save() return redirect(url_for('admin.article')) return render_template('admin/add-article.html', form=form)
def index(): form = PostForm() if form.validate_on_submit(): language = guess_language(form.post.data) if language == 'UNKNOWN' or len(language) > 5: language = '' post = Post(body=form.post.data, author=current_user, language=language) db.session.add(post) db.session.commit() flash(_('Your post is now live!')) 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='pep', form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
def search(): 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, timestamp=now + timedelta(seconds=1)) p2 = Post(body="post from susan", author=u2, timestamp=now + timedelta(seconds=4)) p3 = Post(body="post from mary", author=u3, timestamp=now + timedelta(seconds=3)) p4 = Post(body="post from david", author=u4, timestamp=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 create(): form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, body=form.body.data, author=current_user) db.session.add(post) db.session.commit() flash('Your post has been published!') return redirect(url_for('main.user', user_id=current_user.id)) return render_template("create.html", title="Create Post", form=form)
def index(): form = PostForm() if form.validate_on_submit(): language = guess_language(form.post.data) if language == 'UNKNOWN' or len(language) > 5: language = '' post = Post(body=form.post.data, author=current_user, language=language) db.session.add(post) db.session.commit() flash('Your post is now live!') return redirect(url_for('main.index')) posts = current_user.followed_posts().all() return render_template('main/index.html', title='Home Page', posts=posts, form=form)
def upload(): form = UploadForm() if request.method == 'POST': email = request.files['email'] image = request.files['image'] if '@' not in email: # prompt for invalid email address return redirect(request.url) if 'image' not in request.files: return redirect(request.url) if image.filename == '': return redirect(request.url) # Check that our filename is in the 'approved' array if image and image.filename.split('.')[1] in [ 'jpg', 'jpeg', 'png', 'bmp' ]: # name and save the file filename = secure_filename(image.filename) image.save(os.path.join(basedir, 'app', 'static', filename)) # add our new post to the database post = Post(datetime=datetime.utcnow(), email=form.email.data, first_name=form.first_name.data, last_name=form.last_name.data, description=form.description.data, filename=filename) db.session.add(post) flash("Successfully added {}".format(form.description.data)) return redirect(url_for('main.index')) else: # flash(bad file extension) return redirect(request.url) # bad file name else: return render_template('upload.html', form=form)
def test_save(self): post_1 = Post(title='Hello World! ß', short_text='foobar') post_1.save() self.assertEqual(post_1.slug, 'hello-world-ss')