def new_product(cat_id): if not user_logged_in(): flash("Login required for this operation", "warning") return redirect(url_for('login')) category = session.query(Category).filter(Category.id == cat_id).first() # Show Eror 404 if a category is not found if not category: return render_template('error404.html', title="Category"), 404 if request.method == 'POST': # Validate the form data res = validate_product(request.form) if res != "OK": flash(res, "warning") return redirect(url_for('new_product', cat_id=cat_id)) # Upload the product picture filename = "" picfile = request.files.get('picfile') if picfile and allowed_file(picfile.filename): filename = upload_unique(picfile) # Save the new product product = Product(name=request.form['name'], description=request.form['description'], picture_file=filename, category_id=cat_id) session.add(product) session.commit() flash("Product successfully created", "success") return redirect(url_for('show_category', id=cat_id)) else: categories = session.query(Category).all() # Redirect urls for submission and cancellaton params = dict(submit_url=url_for('new_product', cat_id=category.id), cancel_url=url_for('show_category', id=category.id)) return render_template('new_product.html', category=category, categories=categories, params=params)
def addProduct(url_name): category = session.query(Category).filter_by(url_name=url_name).one() if 'username' not in login_session: return redirect('/login') if request.method == 'POST': newProduct = Product(name=request.form['name'], short_desc=request.form['shortdesc'], price=request.form['price'], category=category, user_id=login_session['user_id']) session.add(newProduct) flash('%s was added Successfully to the %s category' % (newProduct.name, category.name)) session.commit() return redirect( url_for('showCategoryProducts', url_name=category.url_name)) else: return render_template('newProduct.html', navitems=navitems, category=category)
def products_new(category_id): """Add a new product to a category.""" global google_user_id if 'credentials' not in flask.session: return render_template('login_check.html') if request.method == 'POST': if google_user_id != "": user = google_user_id product_new = Product(name=request.form['name'], description=request.form['description'], price=request.form['price'], category_id=category_id, user=user) session.add(product_new) session.commit() return redirect(url_for('products_list', category_id=category_id)) else: return render_template('products_new.html', category_id=category_id)
def new_product_alt(): if not user_logged_in(): flash("Login required for this operation", "warning") return redirect(url_for('login')) if request.method == 'POST': # Validate the form data res = validate_product(request.form) if res != "OK": flash(res, "warning") return redirect(url_for('new_product_alt')) # Upload the product picture filename = "" picfile = request.files.get('picfile') if picfile and allowed_file(picfile.filename): filename = upload_unique(picfile) # Save the new product product = Product(name=request.form['name'], description=request.form['description'], category_id=int(request.form['category']), picture_file=filename) session.add(product) session.commit() flash("Product successfully created", "success") return redirect(url_for('show_category', id=product.category_id)) else: categories = session.query(Category).all() # Redirect urls for submission and cancellation params = dict(submit_url=url_for('new_product_alt'), cancel_url=url_for('show_home_page')) return render_template('new_product.html', categories=categories, params=params)
rows.append(row) for row in rows[1:]: order = Order(order_id=row[0], product_code=row[1], order_type=row[2], delivery_distance=row[3], delivery_time=row[4]) session.add(order) session.commit() filename = "test3.csv" rows = [] with open(filename, 'r') as csvfile: csvreader = csv.reader(csvfile) for row in csvreader: rows.append(row) for row in rows: order = Product(product_id=row[0], product_code=row[1], composition=row[2], color=row[3], pattern=row[4], weave=row[5], image_link=row[6]) session.add(order) session.commit()
session.add(user3) session.commit() print("Users added successfully to the database!") # Create Outerwear Category with Products # Outerwear Category Creation category1 = Category(user_id=1, name="Outerwear", url_name="outerwear") session.add(category1) session.commit() # Outerwear Product Creation product1 = Product(user_id=1, name="Long Sleeve ZipUp", short_desc="Lightweight layer for chilly trail runs", price="$60.00", category=category1) session.add(product1) session.commit() product2 = Product(user_id=2, name="TriClimate Jacket", short_desc="All-weather protection all year round", price="$199.99", category=category1) session.add(product2) session.commit()