예제 #1
0
def register():
    form = RegisterForm()
    if form.validate_on_submit():

        if User.query.filter_by(email=form.email.data).first():
            print(User.query.filter_by(email=form.email.data).first())
            # User already exists
            flash("You've already signed up with that email, log in instead!")
            return redirect(url_for('login'))

        hash_and_salted_password = generate_password_hash(
            form.password.data, method='pbkdf2:sha256', salt_length=8)
        new_user = User(
            email=form.email.data,
            name=form.name.data,
            password=hash_and_salted_password,
        )
        db_session.add(new_user)
        db_session.commit()
        login_user(new_user)
        return redirect(url_for("get_all_posts"))

    return render_template("register.html",
                           form=form,
                           current_user=current_user)
예제 #2
0
def add_new_post():
    form = CreatePostForm()
    if form.validate_on_submit():
        new_post = BlogPost(title=form.title.data,
                            subtitle=form.subtitle.data,
                            body=form.body.data,
                            img_url=form.img_url.data,
                            author=current_user,
                            date=date.today().strftime("%B %d, %Y"))
        db_session.add(new_post)
        db_session.commit()
        return redirect(url_for("get_all_posts"))

    return render_template("make-post.html",
                           form=form,
                           current_user=current_user)
예제 #3
0
def edit_post(post_id):
    post = BlogPost.query.get(post_id)
    edit_form = CreatePostForm(title=post.title,
                               subtitle=post.subtitle,
                               img_url=post.img_url,
                               author=current_user,
                               body=post.body)
    if edit_form.validate_on_submit():
        post.title = edit_form.title.data
        post.subtitle = edit_form.subtitle.data
        post.img_url = edit_form.img_url.data
        post.body = edit_form.body.data
        db_session.commit()
        return redirect(url_for("show_post", post_id=post.id))

    return render_template("make-post.html",
                           form=edit_form,
                           is_edit=True,
                           current_user=current_user)
예제 #4
0
def show_post(post_id):
    form = CommentForm()
    requested_post = BlogPost.query.get(post_id)

    if form.validate_on_submit():
        if not current_user.is_authenticated:
            flash("You need to login or register to comment.")
            return redirect(url_for("login"))

        new_comment = Comment(text=form.comment_text.data,
                              comment_author=current_user,
                              parent_post=requested_post)
        db_session.add(new_comment)
        db_session.commit()

    return render_template("post.html",
                           post=requested_post,
                           form=form,
                           current_user=current_user)
예제 #5
0
def delete_post(post_id):
    post_to_delete = BlogPost.query.get(post_id)
    db_session.delete(post_to_delete)
    db_session.commit()
    return redirect(url_for('get_all_posts'))