def addNewProduct(): form = AddNewProductForm() if request.method == "POST" and form.validate(): # Add the new product. newProduct = Product(form.name.data, float(form.price.data), int(form.stock.data)) newProduct.category = form.category.data newProduct.description = form.description.data db.session.add(newProduct) db.session.commit() # We should, also, add the product specifications. # Specifications are sent as a json string. We do this because it # is really hard to generate dynamic for fields on the client with wtforms. # The solution is to have a single string field generated with wtforms and # to simulate the rest of the string fields on the client, when the client # will submit the form, the values of that fields will be collected and saved # in that master field. We use a javascript object on the client to track # the fields an their values, so at the submission the master field will # contain the json representation of that object. specifications = json.loads(form.specifications.data) for spec_name, spec_value in specifications.iteritems(): db.session.add(ProductSpecifications(newProduct.id, spec_name, spec_value)) db.session.commit() # Now add the images. pictures = json.loads(form.pictures.data) for pictureLink in pictures: db.session.add(ProductPictures(pictureLink, newProduct.id)) db.session.commit() # Now that the product has an id we can add the rest of the components. # First, if the product's category is not already in the database we should add it. category = Categories.query.filter_by(name=newProduct.category).first() if category is None: newCategory = Categories(newProduct.category) db.session.add(newCategory) db.session.commit() else: # The product category may exist, but is unavailable, because there # are no products available left in it. We should make it available. if not category.available: category.available = True db.session.add(category) db.session.commit() return redirect(url_for('productAddedSuccessfully', name=newProduct.name)) flashErrors(form.errors, flash) return render_template('add_new_product.html', form=form)
#################################### ########### product.py ############# #################################### product = Product() product.name = "nuevo producto" product.description = "a product description" product.sku = "123" product.brand = "a brand" product.color = "red" product.size = "10" product.image = "an image" product.manufacturer = "giani" product.category = "zapatos" product.brand = "giani" print "product.py" print "print : {}".format(product.Print()) print "save: {}".format(product.Save()) print "print : {}".format(product.Print()) print "init : {}".format(product.InitBySku("123")) print "list : {}".format(product.GetList(0, 10)) #print "remove : {}".format(product.Remove()) print "\n\n" #################################### ########### kardex.py ############## ####################################