def product_register(): form = ProductForm() if request.method == "POST" and form.validate(): name = form.name.data brand = form.brand.data style = form.style.data abv = form.abv.data ibu = form.ibu.data estcal = form.estcal.data c, conn = connection() x = c.execute("SELECT * FROM beer WHERE name LIKE %s", [thwart(name)]) if int(x) > 0: flash("That beer is already in the system, please choose another.") return render_template("product_register.html", form=form) else: c.execute( "INSERT INTO beer (name, brandName, Style, abv, ibu, estCal) VALUES (%s, %s, %s, %s, %s, %s)", (thwart(name), thwart(brand), thwart(style), thwart( str(abv)), thwart(str(ibu)), thwart(str(estcal)))) conn.commit() flash("Thanks for registering your beer!") c.close() conn.close() gc.collect() return redirect(url_for("homepage")) return render_template("product_register.html", form=form)
def share(): if request.method == 'GET': form = ProductForm() else: form = ProductForm(request.form) if form.validate(): if form.create_product(g.user) is not None: return redirect(url_for('home')) return render_template('share_product.html', form=form)
def product_create(): """Provide HTML form to create a new product.""" form = ProductForm(request.form) if request.method == 'POST' and form.validate(): mongo.db.products.insert_one(form.data) # Success. Send user back to full product list. return redirect(url_for('products_list')) # Either first load or validation error at this point. return render_template('product/edit.html', form=form)
class ProductHandler(BaseHandler): @admin_required def get(self, product_id=None, **kwargs): """Return a product to edit or an empty form to create""" template = 'admin/product/new.html' context = { 'form': self.form, } # render edit form if product_id is not None: product = Product.get_by_id(product_id) if product: self.form = ProductForm(obj=product) self.form.tags.data = ', '.join(product.tags) context.update({ 'form': self.form }) template = 'admin/product/edit.html' else: return redirect('/admin/shop/') # render new form return self.render_response(template, **context) @admin_required def post(self, product_id=None, **kwargs): """Handle submitted form data""" # validate form if self.form.validate(): name = self.form.name.data description = self.form.description.data price = self.form.price.data unit = self.form.unit.data live = self.form.live.data tags = self.form.tags.data language = self.form.language.data if tags is not None: tags = [tag.strip() for tag in tags.split(',') if tag != ''] # save edit form if product_id: product = Product.get_by_id(product_id) product.name = name product.description = description product.price = price product.unit = unit product.live = live product.tags = tags product.language = language # save new form else: product = Product(name=name, description=description, price=price, unit=unit, live=live, tags=tags, language=language) if product.put(): return redirect('/admin/shop/products/') return self.get(**kwargs) @cached_property def form(self): """Form instance as cached_property""" return ProductForm(self.request)
def product_edit(product_id): form = ProductForm(request.form) product = mongo.db.products.find_one({ "_id": ObjectId(product_id) }) if request.method == 'POST' and form.validate(): mongo.db.products.replace_one({"_id": ObjectId(product_id)}, form.data) # Success. Send user back to full product list. return redirect(url_for('products_list')) # Either first load or validation error at this point. return render_template('product/edit_product.html', product=product, form=form)
def product_edit(product_id): """Provide HTML form to edit a product.""" form = ProductForm(request.form) if request.method == 'POST' and form.validate(): mongo.db.products.update_one({'_id':ObjectId(product_id)},{'$set':{'name':request.form['name'], 'description':request.form['description'], 'price':request.form['price']}}) # Success. Send user back to full product list. return redirect(url_for('products_list')) # Either first load or validation error at this point. return render_template('product/edit.html', form=form)
def product_edit(product_id): # return 'Form to edit product #.'.format(product_id) """Provide HTML form to edit an existing product.""" product = MONGO.db.products.find_one({"_id": ObjectId(product_id)}) form = ProductForm(request.form) if request.method == 'POST' and form.validate(): MONGO.db.products.replace_one({'_id': ObjectId(product_id)}, form.data) # Success. Send user back to full product list. return redirect(url_for('products_list')) # Either first load or validation error at this point. return render_template('product/edit.html', form=form, product=product)
def product_edit(product_id): """Provide HTML form to edit a given product.""" product = mongo.db.products.find_one({"_id": ObjectId(product_id)}) if product is None: abort(404) form = ProductForm(request.form, data=product) if request.method == 'POST' and form.validate(): mongo.db.products.replace_one(product, form.data) # Success. Send the user back to the detail view. return redirect(url_for('products_list')) return render_template('product/edit.html', form=form)
def new_product(): # add a new product form = ProductForm(request.form) if request.method == 'POST' and form.validate(): # saving new product product = Master() save_changes(product, form, new=True) flash('New product added successfully!') return redirect('/') return render_template('new_product.html', form=form)
def product_edit(product_id): # Query: get Product object by ID. product = mongo.db.products.find_one({ "_id": ObjectId(product_id) }) if product is None: # Abort with Not Found. abort(404) form = ProductForm(request.form, name=product["name"], description=product["description"], price=product["price"]) if request.method == 'POST' and form.validate(): mongo.db.products.replace_one({ "_id": ObjectId(product_id) }, form.data) return redirect(url_for('products_list')) return render_template('product/edit.html', form=form)
def product_view(): errors = [] if request.method == 'POST': form = ProductForm(request.form) if form.validate(): product = form.data products.append(product) else: for errors_list in form.errors.values(): if type(errors_list) == list: for error in errors_list: errors.append(error) return render_template('products.html', products=products, errors=errors)
def product_edit(product_id): product = mongo.db.products.find_one({"_id": ObjectId(product_id)}) if product is None: # Abort with Not Found. abort(404) form = ProductForm(request.form) if request.method == 'GET': form.name.data = product.get('name') form.description.data = product.get('description') form.price.data = product.get('price') return render_template('product/edit.html', form=form) elif request.method == 'POST' and form.validate(): mongo.db.products.replace_one({'name': product.get('name')}, form.data) return redirect(url_for('products_list'))
def new_product(): """ Add a new product """ form = ProductForm(request.form) if request.method == 'POST' and form.validate(): # save the album product = Product() save_product(product, form, new=True) flash('Product created successfully!') return redirect('/sell') return render_template('new_product.html', form=form)
def product_edit(product_id): product = mongo.db.products.find_one({"_id": ObjectId(product_id)}) if product is None: # Abort with Not Found. abort(404) #Fill form with current data update = ProductForm(request.form) update.fill(product["name"], product["description"], product["price"]) #Create an empty form for the update form = ProductForm(request.form) if request.method == 'POST' and form.validate(): update.fill(form.name.data, form.description.data, form.price.data) mongo.db.products.update_one(product, {"$set": update.data}) return redirect(url_for('products_list')) return render_template('product/edit.html', form=update)
def product_edit(product_id): product = mongo.db.products.find_one({ "_id": ObjectId(product_id) }) if product is None: # Abort with Not Found. abort(404) form = ProductForm(request.form) if request.method == 'POST' and form.validate(): mongo.db.products.find_one_and_update({ "_id": ObjectId(product_id) }, { "$set": form.data }) return redirect(url_for('products_list')) form.name.data = product['name'] form.description.data = product['description'] form.price.data = product['price'] return render_template('product/edit.html', form=form)
def new_product(): # add a new product form = ProductForm(request.form) if request.method == 'POST' and form.validate(): # saving new product product = Master() save_changes(product, form, new=True) file = request.files['inputFile'] newFile = FileContents(name=file.filename, data=file.read()) db_session.add(newFile) db_session.commit() flash('New product added successfully!') return redirect('/') return render_template('new_product.html', form=form)
def product_edit(product_id): form = ProductForm(request.form) try: product = mongo.db.products.find_one({"_id": ObjectId(product_id)}) except: product = None if not product: # Abort with Not Found. abort(404) if request.method == 'POST' and form.validate(): mongo.db.products.replace_one({'_id': ObjectId(product_id)}, form.data) # Success. Send user back to full product list. return redirect(url_for('product_detail', product_id=product_id)) # Either first load or validation error at this point. return render_template('product/update.html', form=form, product=product)
def product_edit(product_id): product = mongo.db.products.find_one({"_id": ObjectId(product_id)}) if product is None: abort(404) form = ProductForm(request.form) if request.method == 'POST' and form.validate(): mongo.db.products.update_one({"_id": ObjectId(product_id)}, { '$set': { 'name': form.data['name'], 'description': form.data['description'], 'price': form.data['price'] } }) return redirect(url_for('products_list')) form.fill(product) return render_template('product/edit.html', form=form)
def product_edit(product_id): form = ProductForm(request.form) product = mongo.db.products.find_one({"_id": ObjectId(product_id)}) if request.method == 'POST' and form.validate(): mongo.db.products.update_one({'name': product['name']}, { "$set": { "name": form.name.data, "description": form.description.data, "price": form.price.data } }) # Success. Send user back to full product list. return redirect(url_for('products_list')) form.name.data = product['name'] form.description.data = product['description'] form.price.data = product['price'] # Either first load or validation error at this point. return render_template('product/edit.html', form=form)
def new_product(): # product = Product(name=name, price=price) # db.session.add(product) # db.session.commit() # # return ('Added {}'.format(product.id)) if request.method == 'POST': form = ProductForm(request.form) if form.validate(): product = Product() form.populate_obj(product) db.session.add(product) db.session.commit() print('Saved', product.name, product.price) else: print('Invalid') else: form = ProductForm() return render_template('product.html', form=form)
def add(): """ Route for products add """ # request Form data form = ProductForm(request.form) # Get categories collection from database categories = mongo.db.categories.find().sort("name", 1) # Get allergens collection from database allergens = mongo.db.allergens.find().sort("name", 1) # Validate form if request.method == "POST": # Get product name, manufacturer and category product_name = form.name.data.lower() product_manufacturer = form.manufacturer.data.lower() product_category = request.form.get("categorySelector").lower() allergen_list = allergen_get_selected_checkboxes(allergens) # Check if category has been selected from drop down cstr = ("add Product. " + "If you would like to add a product category, " + "please contact the site Administrator") proceed = False if product_check(product_name) and form.validate(): product_category = category_get_selection(cstr) if product_category: proceed = True if proceed: product_category_id = category_get_id(product_category) # Check if any allergens are selected if allergen_list: proceed = True else: # Display flash message flash(("Please select allergens " + "that the product is Free From. " + "If you would like to add an Allergen, " + "please contact the site Administrator"), "warning") proceed = False if proceed: allergen_id_list = allergen_get_id_list(allergen_list) if proceed: # Get product rating product_rating = form.rating.data product_rating = None if product_rating == "" else int( product_rating) if product_rating: proceed = True else: # Display flash message flash("Please rate product", "warning") proceed = False if proceed: # Get product review product_review = form.review.data # Get user name user_name = session["user"] # Get user id from user name user_id = user_get(user_name)["_id"] # Set new product variable new_product = { "name": product_name, "manufacturer": product_manufacturer, "user_id": user_id, "category_id": product_category_id, "barcode": "", "free_from_allergens": allergen_id_list, "reviews": [{ "user_id": user_id, "rating": product_rating, "review": product_review }] } # Add new record to the database mongo.db.products.insert_one(new_product) # Display flash message flash(product_name + " succesfully added" + " to products", "success") form.name.data = None form.manufacturer.data = None form.rating.data = None form.review.data = None product_id = mongo.db.products.find_one({"name": product_name})["_id"] return redirect(url_for('products.view', product_id=product_id)) return render_template("product_add.html", categories=categories.rewind(), allergens=allergens.rewind(), product_category=product_category, selected_allergens=allergen_list, form=form) form.rating.data = None return render_template("product_add.html", categories=categories.rewind(), allergens=allergens.rewind(), form=form)
class ProductHandler(BaseHandler): @admin_required def get(self, product_id=None, **kwargs): """Return a product to edit or an empty form to create""" template = 'admin/product/new.html' context = { 'form': self.form, } # render edit form if product_id is not None: product = Product.get_by_id(product_id) if product: self.form = ProductForm(obj=product) self.form.tags.data = ', '.join(product.tags) context.update({'form': self.form}) template = 'admin/product/edit.html' else: return redirect('/admin/shop/') # render new form return self.render_response(template, **context) @admin_required def post(self, product_id=None, **kwargs): """Handle submitted form data""" # validate form if self.form.validate(): name = self.form.name.data description = self.form.description.data price = self.form.price.data unit = self.form.unit.data live = self.form.live.data tags = self.form.tags.data language = self.form.language.data if tags is not None: tags = [tag.strip() for tag in tags.split(',') if tag != ''] # save edit form if product_id: product = Product.get_by_id(product_id) product.name = name product.description = description product.price = price product.unit = unit product.live = live product.tags = tags product.language = language # save new form else: product = Product(name=name, description=description, price=price, unit=unit, live=live, tags=tags, language=language) if product.put(): return redirect('/admin/shop/products/') return self.get(**kwargs) @cached_property def form(self): """Form instance as cached_property""" return ProductForm(self.request)