def newItem(category_name): # return user to login page if not logged in. Deleting a category can only be done by authorized users if 'username' not in login_session: return redirect(url_for('showLogin')) if request.method == 'POST': #check if category name is inputted if (request.form['title'] and request.form['price']): # check if the post request has the file part, set default image if not uniqueItem(request.form['title']): flash('Item already exist. Please enter unique item name', 'info') return redirect(url_for('showItems',category_name=category_name)) filename = 'default.jpg' if 'file' in request.files: file = request.files['file'] if allowed_file(request.files['file'].filename): filename = getFileName(file) else: #for cases when upload file is not an image flash('File extension is not supported. Only images are welcome :)','info') return redirect(url_for('showItems',category_name=category_name)) title = request.form['title'] price = request.form['price'] description = request.form['description'] category = session.query(Category).filter_by(name=category_name).one() newItem = CategoryItem(title=title,category_id=category.id,description=description,price=price,status='A',image=filename, user_id = login_session['user_id']) session.add(newItem) session.commit() flash('Item was created successfully','success') else: flash('Item title or price are missing','danger') return redirect(url_for('showItems',category_name = category_name)) else: return render_template('newitem.html',category_name = category_name)
def addItemDetails(): """ Method: adds new items to catalog Returns: On GET: Form to create new user On POST: Redirect to main page after successful item creation """ if 'username' not in login_session: return redirect('/login') if request.method == 'POST': newItem = CategoryItem(title=request.form['title'], user_id=login_session['user_id'], description=request.form['description'], category_id=request.form['category_id']) session.add(newItem) flash('New Category Item Successfully Created') session.commit() return redirect(url_for('showCategories')) else: categories = session.query(Category).all() return render_template('newCategoryItem.html', categories=categories) return render_template('newCategoryItem.html')
Foosball = Category (user_id = 2, name = 'Foosball', image = 'foosball.jpg', status = 'A') session.add(Foosball) session.commit() Skating = Category (user_id = 2, name = 'Skating', image = 'skating.jpg', status = 'A') session.add(Skating) session.commit() Hockey = Category (user_id = 2, name = 'Hockey', image = 'hockey.jpg', status = 'A') session.add(Hockey) session.commit() # queries for adding items item1 = CategoryItem(title='Ball', price= 36, description='Orange soccer ball with black and white fifa like one', status = 'A', user_id = 2, category_id=1, image ='OPGHLH34CCQD.jpg') session.add(item1) session.commit() item2 = CategoryItem(title='Frisbee Board', price= 25, description='freisbee board in page and red colors', status = 'A', user_id = 2, category_id=4, image ='MJVNTIMQK6TS.jpg') session.add(item2) session.commit() item4 = CategoryItem(title='Goal Keeper Gloves', price= 49, description='Pro-level glove with built-in removable finger spines (for customizable protection), the "Pro" is the most popular Storelli glove model. Padded with top-grade German latex, the "Pro" delivers the best of grip with the extra confidence needed by hyper-competitive goalkeepers.', status = 'A', user_id = 2, category_id=1, image ='IAYXZ7T3PC25.jpg') session.add(item4) session.commit() item5 = CategoryItem(title='Soccer Wear', price= 40, description='100 % Polyester High Luster Jacquard Patterned Polyester With Moisture Management', status = 'A', user_id = 2, category_id=1, image ='O1RXH1MTZLRD.jpg') session.add(item5) session.commit()
# Category.__table__.drop() # CategoryItem.__table__.drop() #Create dummy user User1 = User(name="Fatim Alawami", email="*****@*****.**", picture='https://pbs.twimg.com/profile_images/2671170543/18debd694829ed78203a5a36dd364160_400x400.png') session.add(User1) session.commit() # Soccer Category category1 = Category(user_id=1, name="Soccer", image="fifa.jpg",status='A') session.add(category1) session.commit() CategoryItem2 = CategoryItem(user_id=1, title="Soccer ball", description="adidas 2018 FIFA World Cup Russia Telstar Glider Soccer Ball", price=20, image='fifa.jpg', category=category1,status='A') session.add(CategoryItem2) session.commit() CategoryItem1 = CategoryItem(user_id=1, title="Goalkeeper Shirt", description="Storelli Youth BodyShield 3/4 Soccer Goalkeeper Shirt", price=30, image='goalkeepershirt.png', category=category1,status='A') session.add(CategoryItem1) session.commit() CategoryItem3 = CategoryItem(user_id=1, title="Pullover", description="Mizuno Men's Elite 1/2-Zip Pullover", price=47, image='pullover.png', category=category1,status='A') session.add(CategoryItem3)