Example #1
0
def new_post():
    form = PostForm()
    if form.validate_on_submit() :
        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,legend='New Post')
Example #2
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
        db.session.commit()
        flash('Your post has been updated','success')
        return redirect(url_for('posts.post',post_id=post.id))
    elif(request.method=="GET"):
        form.title.data=post.title
        form.content.data=post.content
    return render_template('create_post.html',title="Update Post",form=form,legend='Post Update')
Example #3
0
def update_post(post_id):
    form = PostForm()
    post = Post.query.get_or_404(post_id)
    if current_user.id != post.author.id :
        abort(403)
    
    if form.validate_on_submit():
        post.title = form.title.data
        post.content = form.content.data
        db.session.commit()
        flash('Your post has been updated','success')
        return redirect(url_for('posts.post',post_id=post.id))

    elif request.method == 'GET':
        form.content.data = post.content
        form.title.data = post.title

    return render_template('create_post.html',title='Update Post',legend='Update Post',form=form)
Example #4
0
def new_post():
    form = PostForm()
    posts = Post.query.all()
    if form.validate_on_submit():
        post = Post(
            title=form.title.data,
            content=form.content.data,
            author=current_user,
            date_posted=datetime.utcnow().replace(tzinfo=pytz.utc).astimezone(
                pytz.timezone("Australia/Brisbane")))
        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,
                           posts=posts,
                           legend="New Post")
Example #5
0
def update_post(post_id):
    posts = Post.query.all()
    post = Post.query.get_or_404(post_id)
    if post.author == current_user or current_user.administrator == 1:
        form = PostForm()
        if form.validate_on_submit():
            post.title = form.title.data
            post.content = form.content.data
            db.session.commit()
            flash("Your post has been updated!", "success")
            return redirect(url_for("main.home"))
        elif request.method == "GET":
            form.title.data = post.title
            form.content.data = post.content
        return render_template("create_post.html",
                               title="Update Post",
                               form=form,
                               posts=posts,
                               legend="Update Post")
    else:
        abort(403)