Beispiel #1
0
def edit_post(id):
    post = Post.query.get_or_404(id)

    if post not in current_user.posts:
        return abort(401)

    form = NewPostForm(obj=post)

    if form.validate_on_submit():
        for param, value in request.form.iteritems():
            if param in form.days.keys():
                setattr(post.days, param, bool(value))
            else:
                setattr(post, param, value)

        post.save()
        post.days.save()

        flash("{} saved successfully!".format(post.title))

        return redirect(url_for("user.posts"))
    else:
        flash_errors(form)

    return render_template("users/newpost.html", form=form, title="Edit Post")
Beispiel #2
0
def register():
    form = RegisterForm(request.form, csrf_enabled=False)
    if form.validate_on_submit():
        new_user = User.create(username=form.username.data,
                               email=form.email.data,
                               password=form.password.data,
                               active=True)
        flash("Thank you for registering. You can now log in.", 'success')
        return redirect(url_for('public.home'))
    else:
        flash_errors(form)
    return render_template('public/register.html', form=form)
Beispiel #3
0
def home():
    form = LoginForm(request.form)
    # Handle logging in
    if request.method == 'POST':
        if form.validate_on_submit():
            login_user(form.user)
            flash("You are logged in.", 'success')
            redirect_url = request.args.get("next") or url_for("user.posts")
            return redirect(redirect_url)
        else:
            flash_errors(form)
    return render_template("public/home.html", form=form)
Beispiel #4
0
def add_post():
    form = NewPostForm()

    if form.validate_on_submit():
        new_post = Post()
        form.populate_obj(new_post)
        current_user.posts.append(new_post)

        new_post.save()
        current_user.save()

        flash("Updated {}".format(new_post.title))

        return redirect(url_for("user.posts"))
    else:
        flash_errors(form)

    return render_template("users/newpost.html", form=form, title="New Post")