def saveCatalog(): data = json.loads(request.form['data']) if not data: return "0" order = 1 super_id = None for item in data: id = int(item['id'].split('_')[1]) indent = int(item['indent']) name_cs = item['name_cs'] name_jp = item['name_jp'] if id > 0: c = Catalog.query.filter_by(id=id).first() else: c = Catalog() db.session.add(c) db.session.commit() if indent >= 0: if indent == 0: super_id = c.id c.super_id = None c.order = order else: # Check whether child wasn't moved to another parent, # if so, add this parent to all products that have this child in catalog if c.super_id != super_id: cataloged_products_to_update = CatalogedProducts.query.filter_by(catalog_id=c.id).all() for cpu in cataloged_products_to_update: control_catalog_product = CatalogedProducts.query.filter_by(product_id=cpu.product_id)\ .filter_by(catalog_id=super_id).first() if not control_catalog_product: product = Product.query.filter_by(id=cpu.product_id).first() if product: cp = CatalogedProducts() cp.product_id = cpu.product_id cp.catalog_id = super_id db.session.add(cp) db.session.commit() c.super_id = super_id c.order = order c.name_CS = name_cs c.name_JP = name_jp db.session.add(c) order += 1 else: for cp in c.products: db.session.delete(cp) db.session.delete(c) db.session.commit() return "ok"
def addProduct(): form = AddProductForm() makers = Maker.query.all() if makers: form.maker.choices = [(a.id, a.name) for a in makers] if form.validate_on_submit(): product = Product() product.code = form.code.data product.maker_id = form.maker.data product.maker_code = form.maker_code.data product.desc_CS = form.desc_CS.data product.desc_JP = form.desc_JP.data product.long_desc = form.long_desc.data product.detailed_desc = form.detailed_desc.data product.subcategory_desc = form.subcategory_desc.data product.keywords = form.keywords.data product.price_unit = form.price_unit.data product.price_retail = form.price_retail.data product.qty_stock = form.qty_stock.data product.limited_flg = form.limited_flg.data product.axm_node = form.axm_node.data if form.package_size.data == '': product.package_size = None else: product.package_size = form.package_size.data category_id = Maker.query.filter_by(id=product.maker_id).first().category_id if category_id: product.category_id = category_id db.session.add(product) db.session.commit() term_ids_str = request.form.getlist('term') for id in term_ids_str: t = CatalogedProducts() t.product_id = product.id t.catalog_id = int(id) db.session.add(t) db.session.commit() flash(gettext("New product successfully added.")) return redirect(url_for("editProduct", id=product.id)) catalog = prepare_catalog() return render_template("product/addProduct.html", title=gettext('Add new product'), catalog=catalog, form=form)
def editProduct(id=0): # for stock: return to the same page stock_page = request.args.get('stock_page') if request.args.get('stock_page') else 1 product = Product.query.filter_by(id=id).first() if product == None: flash(gettext('Product not found.')) return redirect(url_for('stock', page=stock_page)) form = AddProductForm(obj=product) makers = Maker.query.all() if makers: form.maker.choices = [(a.id, a.name) for a in makers] #for existing code validation form.request = request if form.validate_on_submit(): #delete product if 'delete' in request.form: product.active_flg = False db.session.add(product) db.session.commit() flash(gettext('Product has been deleted.')) return redirect(url_for("stock", page=stock_page)) #update catalog terms new_ids_str = request.form.getlist('term') new_ids = [] for id in new_ids_str: new_ids.append(int(id)) old_ids = [] for term in product.catalog_terms: old_ids.append(term.catalog_id) for id in old_ids: if id not in new_ids: t = CatalogedProducts.query\ .filter_by(catalog_id=id)\ .filter_by(product_id=product.id)\ .first() db.session.delete(t) for id in new_ids: if id not in old_ids: t = CatalogedProducts() t.product_id = product.id t.catalog_id = id db.session.add(t) db.session.commit() #update product product.code = form.code.data product.maker_id = form.maker.data product.maker_code = form.maker_code.data product.desc_CS = form.desc_CS.data product.desc_JP = form.desc_JP.data product.long_desc = form.long_desc.data product.detailed_desc = form.detailed_desc.data product.subcategory_desc = form.subcategory_desc.data product.keywords = form.keywords.data product.price_unit = form.price_unit.data product.price_retail = form.price_retail.data product.qty_stock = form.qty_stock.data product.limited_flg = form.limited_flg.data product.axm_node = form.axm_node.data if form.package_size.data == '': product.package_size = None else: product.package_size = form.package_size.data category_id = Maker.query.filter_by(id=product.maker_id).first().category_id if category_id: product.category_id = category_id db.session.add(product) db.session.commit() flash(gettext("Product successfully changed.")) return redirect(url_for("stock", page=stock_page)) selected_maker = product.maker_id imgUrls = getImgUrls(product.id) catalog = prepare_catalog() terms = product.catalog_terms terms.sort(key=lambda x: x.catalog.order) selected_catalog_terms = [] for term in terms: if term.catalog.super_id == None: level = 0 else: level = 1 selected_catalog_terms.append([term.catalog.id, level, unicode(term.catalog.name_CS)]) return render_template('product/editProduct.html', title=gettext("Edit Product"), product=product, catalog=catalog, imgUrls=imgUrls, selected_maker=selected_maker, selected_catalog_terms=selected_catalog_terms, stock_page=stock_page, form=form)