Example #1
0
def threads_create():

    categories = [(c.id, c.name) for c in Category.query.all()]

    if request.method == "GET":
        form = ThreadForm()
        form.categories.choices = categories
        return render_template("threads/new.html", form=form)

    form = ThreadForm(request.form)
    form.categories.choices = categories

    if not form.validate():
        return render_template("threads/new.html", form=form)

    t = Thread(form.title.data)
    t.account_id = current_user.id

    categories = form.categories.data
    for c_id in categories:
        c = Category.query.get(c_id)
        t.categories.append(c)

    db.session().add(t)
    db.session().flush()

    m = Message(form.content.data)
    m.thread_id = t.id
    m.account_id = current_user.id

    db.session().add(m)
    db.session().commit()

    return redirect(url_for("threads_index"))
Example #2
0
def threads_create():
    try:
        form = ThreadForm(CombinedMultiDict((request.files, request.form)))

        if not form.validate():
            return render_template(
                "threads/newthread.html",
                form=form,
                error=
                "Invalid input. Make sure you have content in your comment and that any attachments are in correct formats."
            )

        image = form.image.data
        image_id = None
        if image is not None:
            image_id = add_image(image)

        t = Thread(form.title.data)
        t.account_id = current_user.id

        db.session().add(t)
        db.session().commit()

        c = Comment(form.comment.data)
        c.account_id = current_user.id
        c.thread_id = t.id

        if image_id:
            c.image_id = image_id

        db.session().add(c)
        db.session().commit()

        delete_extra_threads()

        return redirect(url_for("main"))
    except:
        print("Something went wrong.")
        db.session().rollback()

        if t:
            db.session().delete(t)
        if image:
            db.session().delete(image)

    return redirect(url_for("page_404"))
Example #3
0
def threads_create():
    form = ThreadForm(request.form)
    form.name.data = form.name.data.strip()

    if not form.validate():
        return redirect(request.referrer)

    t = Thread(form.name.data)
    t.desc = form.desc.data
    t.account_id = current_user.id
    t.section_id = form.section_id.data
    t.hidden = form.hidden.data

    db.session().add(t)
    db.session().commit()

    return redirect(url_for("show_thread", thread_id=t.id))
Example #4
0
def threads_create():
    form = ThreadForm()
    form.category.choices = [(c.id, c.name) for c in Category.query.all()]

    if form.validate_on_submit():
        t = Thread(form.title.data, form.text.data)
        t.creator = current_user.username
        t.account_id = current_user.id
        t.category_id = form.category.data
        t.category_name = Category.query.filter_by(
            id=form.category.data).first().name

        db.session().add(t)
        db.session().commit()

        return redirect(url_for("threads_index", page_num=1))

    return render_template("threads/new.html", form=form)