def newItem(): # Add item base on category name. """ Renders a form for input of a new item - GET request. if I get a post -redirect to 'showItem' after creating new item. """ # ADD LOGIN PERMISSION # Protect app modification from non-users # If a username is not detected for a given request. # Lets redirect to login page. if 'username' not in login_session: return redirect('/login') categories = session.query(Category).all() # Add SQLAlchemy statements if request.method == 'POST': # This is key to retreiving category from the form. try: category = (session.query(Category).filter_by( name=request.form.get('category')).one_or_none()) except None: # If a NoneType object is returned return PageNotFound newItem = Item(category=category, title=request.form['title'], description=request.form['description'], price=request.form['price'], user_id=login_session['user_id']) # access the file from the files dictionary # on request object: #file = request.files['file'] # Process optional item image. image_file = request.files['file'] if image_file and allowed_file(image_file.filename): filename = secure_filename(image_file.filename) if os.path.isdir(app.config['UPLOAD_FOLDER']) is False: os.mkdir(app.config['UPLOAD_FOLDER']) image_file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) newItem.image_filename = filename elif request.form['basic_url']: newItem.basic_url = request.form['basic_url'] session.add(newItem) session.commit() flash('New Item %s successfully Created' % newItem.title) # Now define the url variable path to the newItem created. creator = getUserInfo(newItem.user_id) # Show response to my post request in the client. return redirect( url_for('showItem', category_name=category_name, category_id=category_id, item_title=item_title, item_id=item_id, creator=creator)) else: return render_template('newitem.html', categories=categories)