Exemple #1
0
def newItem():
    """This page will be for adding a new item."""
    error = None
    login_already = current_user.is_authenticated()
    #   This function will return to homepage if it found user is not login.
    #  	There are same setting in pages below which are able to edit the database.
    if login_already:
        if request.method == "POST":
            error = vacant_input()
            if not error:
                """go on input database if no empty imput. If there any, 
				   reload and labels of the empty form will turn red."""
                time_now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                """ Get currunt time and insert to create_time. 
				 	This will use for index items display order"""
                i = Item(create_time=time_now,
                         name=request.form['name'],
                         discription=request.form['discription'],
                         category=request.form['category'])
                session.add(i)
                session.flush()
                i.photo = upload_image(i.id)
                session.commit()
                return redirect("/")
            c = session.query(Category).all()
            return render_template("add.html",
                                   c=c,
                                   login_already=login_already,
                                   error=error)
        c = session.query(Category).all()
        return render_template("add.html",
                               c=c,
                               login_already=login_already,
                               error=error)
    else:
        return redirect("/")