def quotes_update(quote_id, child_id):

    quote = Quote.query.get(quote_id)
    form = QuoteForm(request.form)

    # Talletetaan lomakkeelle kategoriavalinnat, tarpeen sivun uudelleennäytössä
    cates = Category.query.all()
    c_list = [(i.name, i.name) for i in cates]
    form = QuoteForm()
    form.categories.choices = c_list

    if not form.validate():

        return render_template("quotes/modifystate.html",
                               form=form,
                               quote_id=quote_id,
                               child_id=child_id)

    quote.quotecategory.clear()
    db.session().commit()

    quote.quote = form.name.data
    quote.agesaid = form.age.data

    allcategories = form.categories.data

    # käydään läpi kaikki valitut kategoriat ja lisätään ne sanonnalle
    for category in allcategories:
        c = Category.findCategory(category)
        quote.quotecategory.append(c)

    db.session().commit()

    return redirect(url_for("quotes_childquotes", child_id=child_id))
def quotes_create(child_id):

    form = QuoteForm(request.form)

    cates = Category.query.all()
    c_list = [(i.name, i.name) for i in cates]

    form = QuoteForm()
    form.categories.choices = c_list

    if not form.validate():

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

    q = Quote(quote=form.name.data, agesaid=form.age.data, child_id=child_id)

    # Haetaan valitut kategoriat ja lisätään ne yksitellen sanonnalle
    allcategories = form.categories.data

    for category in allcategories:

        c = Category.findCategory(category)

        q.quotecategory.append(c)

    db.session.add(q)
    db.session().commit()

    return redirect(url_for("quotes_childquotes", child_id=child_id))
def quotes_by_category():

    #Otataan talteen kategoria id, jotta voidaan hakea oikean kategorian sanonnat
    form = CategorySelectForm(request.form)
    name = form.selection.data

    category = Category.findCategory(name)
    category_id = category.getId()

    return redirect(
        url_for("quotes_by", page=1, category_id=category_id, name=name))