Ejemplo n.º 1
0
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        if form.image.data:
            picture = post_img(form.image.data)
            post = Post(title=form.title.data,
                        content=form.content.data,
                        anonymous=form.anonymous.data,
                        image=picture,
                        author=current_user)
            db.session.add(post)
            db.session.commit()
            flash('Your Post Has been Created!', 'success')
            return redirect(url_for('home'))
        else:
            post = Post(title=form.title.data,
                        content=form.content.data,
                        anonymous=form.anonymous.data,
                        author=current_user)
            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')
Ejemplo n.º 2
0
def home():
    form = HomeForm()
    if form.validate_on_submit():
        if form.image.data:
            picture = post_img(form.image.data)
            post = Post(title=form.title.data,
                        content=form.content.data,
                        anonymous=form.anonymous.data,
                        image=picture,
                        author=current_user)
        else:
            post = Post(title=form.title.data,
                        content=form.content.data,
                        anonymous=form.anonymous.data,
                        author=current_user)
        db.session.add(post)
        db.session.commit()
        flash('Your Post Has been Created!', 'info')
        return redirect(url_for('home'))
    page = request.args.get('page', 1, type=int)
    posts = Post.query.order_by(Post.date_posted.desc()).paginate(per_page=20,
                                                                  page=page)
    users = User.query.all()
    suggested_friends = set()
    for user in users:
        if user != current_user and current_user.is_authenticated and not current_user.is_following(
                user):
            suggested_friends.add(user)
    if current_user.is_authenticated:
        image_file = url_for('static',
                             filename='profile_pics/' +
                             current_user.image_file)
        hour = datetime.now().hour
        greeting = "Good morning" if 5 <= hour < 12 else "Good afternoon" if hour < 18 else "Good evening"
        return render_template('home.html',
                               title="Home",
                               form=form,
                               posts=posts,
                               image_file=image_file,
                               users=users,
                               greeting=greeting,
                               suggested_friends=suggested_friends)
    else:
        #return redirect(url_for('login'))
        return render_template('home.html', title="Home", posts=posts)
Ejemplo n.º 3
0
def update_post(post_id):
    post = Post.query.get_or_404(post_id)
    if post.author != current_user:
        abort(403)
    form = PostForm()
    if form.validate_on_submit():
        post.title = form.title.data
        post.content = form.content.data
        if form.image.data:
            post.image = post_img(form.image.data)
        db.session.commit()
        flash('Your post has been updated!', 'success')
        return redirect(url_for('post', post_id=post.id))
    elif request.method == 'GET':
        form.title.data = post.title
        form.content.data = post.content
        form.image.data = post.image
    return render_template('create_post.html',
                           title='Update Post',
                           form=form,
                           legend='Update Post')
Ejemplo n.º 4
0
def home():
    form = HomeForm()
    if form.validate_on_submit():
        if form.image.data:
            picture = post_img(form.image.data)
            post = Post(title=form.title.data,
                        content=form.content.data,
                        anonymous=form.anonymous.data,
                        image=picture,
                        author=current_user)
        else:
            post = Post(title=form.title.data,
                        content=form.content.data,
                        anonymous=form.anonymous.data,
                        author=current_user)
        db.session.add(post)
        db.session.commit()
        flash('Your Post Has been Created!', 'success')
        return redirect(url_for('home'))
    page = request.args.get('page', 1, type=int)
    posts = Post.query.order_by(Post.date_posted.desc()).paginate(per_page=50,
                                                                  page=page)
    users = User.query.all()
    if current_user.is_authenticated:
        image_file = url_for('static',
                             filename='profile_pics/' +
                             current_user.image_file)
        return render_template('home.html',
                               title="Home",
                               form=form,
                               posts=posts,
                               image_file=image_file,
                               users=users)
    else:
        #return redirect(url_for('login'))
        return render_template('home.html', title="Home", posts=posts)