def edit_product():

	this = 'edit'
	
	getid = request.args.get('id')

	washlist = Wash.wash_check().all()
	sizelist = Size.size_check().all()
	recommend = Recommend.recommend_check().all()
	towclass = []
	for cls in recommend:
		towclass.append({'id':cls.id, 'topid':cls.topid, 'titles':cls.titles})
	recommend = json.dumps(get_Children(0,towclass))

	classify = Classify.classify_check().all()
	towclass = []
	for cls in classify:
		towclass.append({'id':cls.classid, 'text':cls.classname, 'topid':cls.topid})
	classifylist = json.dumps(getaChildren(0,towclass))

	form = EditProductForm()


	productData = Product.query.filter_by(proid = getid).first()
	pid = productData.pid
	
	if productData:
		form.proid.data        = productData.proid
		form.pid.data          = productData.pid
		form.new_p.data        = productData.new_p
		form.covers.data       = productData.covers
		form.oldcovers.data    = productData.covers
		form.oldpid.data       = productData.oldpid
		form.creatorid.data    = productData.creatorid 
		form.teamid.data       = productData.teamid
		form.proname.data      = productData.proname
		form.price.data        = productData.price
		form.model_height.data = productData.model_height
		form.fabric.data       = productData.fabric
		form.lining.data       = productData.lining
		form.size_table.data   = productData.size_table
		form.weights.data      = productData.weights
		form.the_net.data      = productData.the_net
		form.first_order.data  = productData.first_order
		form.again_order.data  = productData.again_order
		form.shipping.data     = productData.shipping
		form.flying.data       = productData.flying
		form.place.data        = productData.place
		form.size.data         = productData.size
		form.colorid.data      = productData.colorid
		form.display.data      = productData.display

		form.text_centont.data = productData.text_centont

	if form.validate_on_submit():
		productData.proid = int(request.form.get('proid'))
		productData.pid = int(request.form.get('pid'))
		productData.oldpid = int(request.form.get('oldpid'))
		productData.new_p = int(request.form.get('new_p'))
		productData.creatorid = int(request.form.get('creatorid'))
		productData.teamid = int(request.form.get('teamid'))
		productData.proname = request.form.get('proname')
		productData.price = request.form.get('price')
		productData.model_height = request.form.get('model_height')
		productData.fabric = request.form.get('fabric')
		productData.lining = request.form.get('lining')
		productData.wash = request.form.get('wash')
		productData.size_table = request.form.get('size_table')
		productData.weights = request.form.get('weights')
		productData.the_net = request.form.get('the_net')
		productData.first_order = request.form.get('first_order')
		productData.again_order = request.form.get('again_order')
		productData.shipping = request.form.get('shipping')
		productData.flying = request.form.get('flying')
		productData.place = request.form.get('place')
		productData.size = request.form.get('size')
		productData.colorid = request.form.get('colorid')
		productData.display = request.form.get('display')

		html_parser = HTMLParser.HTMLParser()
		html_con = request.form.get('text_centont')
		productData.text_centont = html_parser.unescape(html_con)

		# 处理封面图片
		getcovers = request.form.get('covers')
		oldcovers = request.form.get('oldcovers')
		print (getcovers,oldcovers)
		if getcovers == '':
			productData.covers = oldcovers

			try:
				db_session.add(productData)
				db_session.commit()
			except:
				flash("数据库错误!")
				return redirect('%s%s' %('/manage/add_product?pid=',pid))
		if getcovers != oldcovers and getcovers != '':
			productData.covers = getcovers

			try:
				db_session.add(productData)
				db_session.commit()
			except:
				flash("数据库错误!")
				return redirect('%s%s' %('/manage/add_product?pid=',pid))

			if oldcovers != '' and oldcovers != None and oldcovers != '0':
				deli = db_session.query(Images).filter(Images.id == oldcovers).first();
				imgurl = deli.picurl
				imgurl = actros_split(imgurl)
				
				delImage(imgurl)
				db_session.delete(deli)
				db_session.commit()

		

		proid = int(request.form.get('proid'))


		color_check = productData.colors.all()

		# 颜色部分
		colortitle = request.form.getlist('colortitle')
		color = request.form.getlist('color')
		number = request.form.getlist('number')
		picurl = request.form.getlist('picid')
		cover = request.form.getlist('cover')

		if len(color_check) == len(color) :
			for i in range(len(color_check)):
				color_check[i].colortitle = colortitle[i]
				color_check[i].color = color[i]
				color_check[i].number = number[i]
				color_check[i].picurl = picurl[i]
				color_check[i].cover = cover[i]

		elif len(color_check) < len(color) :
			for i in range(len(color_check)):
				color_check[i].colortitle = colortitle[i]
				color_check[i].color = color[i]
				color_check[i].number = number[i]
				color_check[i].picurl = picurl[i]
				color_check[i].cover = cover[i]

			color_list = [ProColor(
						colortitle = str(colortitle[i]),
						color = str(color[i]),
						number = str(number[i]),
						cover = cover[i],
						proid = proid
					) for i in range(len(color_check),len(color))]
			db_session.add_all(color_list)
			db_session.commit()
			db_session.flush()
			'''
				调用图片排序添加proid函数
				因为是增加了新的颜色组,所以第一个参数使用 update
				最后一个参数加入新添加的数据 color_list
			'''
			give_picid('update',picurl,color_check,color_list)  #调用图片排序添加proid函数

		db_session.commit()
		db_session.flush()

		give_picid('up',picurl,color_check,None) #调用图片排序添加proid函数

		db_session.commit()
		db_session.close()

		flash("添加成功,<span id='time'>3</span>秒后自动跳转管理页。")
		return redirect('%s%s' %('/manage/add_product?pid=',pid))


	return render_template(
		"edit_product.html", 
		this = this,
		pid = pid,
		thdata = productData,
		washlist = washlist,
		sizelist = sizelist,
		recommend= recommend,
		classifylist=classifylist,
		pagename='product',
		form=form)
def add_product():

	pid = int(request.args.get('pid'))

	this = 'add'

	washlist = Wash.wash_check().all()
	sizelist = Size.size_check().all()
	recommend = Recommend.recommend_check().all()
	towclass = []
	for cls in recommend:
		towclass.append({'id':cls.id, 'topid':cls.topid, 'titles':cls.titles})
	recommend = json.dumps(get_Children(0,towclass))

	classify = Classify.classify_check().all()
	towclass = []
	for cls in classify:
		towclass.append({'id':cls.classid, 'text':cls.classname, 'topid':cls.topid})
	classifylist = json.dumps(getaChildren(0,towclass))

	form = AddProductForm()
	if form.validate_on_submit():

		pid = int(request.form.get('pid'))
		oldpid = int(request.form.get('oldpid'))
		creatorid = int(request.form.get('creatorid'))
		teamid = int(request.form.get('teamid'))
		new_p = int(request.form.get('new_p'))
		covers = request.form.get('covers')
		proname = request.form.get('proname')
		price = request.form.get('price')
		model_height = request.form.get('model_height')
		fabric = request.form.get('fabric')
		lining = request.form.get('lining')
		wash = request.form.get('wash')
		size_table = request.form.get('size_table')
		weights = request.form.get('weights')
		the_net = request.form.get('the_net')
		first_order = request.form.get('first_order')
		again_order = request.form.get('again_order')
		shipping = request.form.get('shipping')
		flying = request.form.get('flying')
		place = request.form.get('place')
		size = request.form.get('size')
		colorid = request.form.get('colorid')
		display = request.form.get('display')

		html_parser = HTMLParser.HTMLParser()
		html_con = request.form.get('text_centont')
		text_centont = html_parser.unescape(html_con)

		if covers == '' or covers == None:
			covers = 0

		product = Product(
			pid          = pid,
			oldpid       = oldpid,
			new_p        = new_p,
			covers       = covers,
			creatorid    = creatorid,
			teamid       = teamid,
			proname      = proname,
			price        = price,
			text_centont = text_centont,
			model_height = model_height,
			fabric       = fabric,
			lining       = lining,
			wash         = wash,
			size_table   = size_table,
			weights      = weights,
			the_net      = the_net,
			first_order  = first_order,
			again_order  = again_order,
			shipping     = shipping,
			flying       = flying,
			place        = place,
			size         = size,
			colorid      = colorid,
			display      = display,
			add_time     = datetime.datetime.now()
		)

		product_check = db_session.query(Product).first()
		if len(proname):
			try:
				db_session.add(product)
				db_session.commit()
				db_session.flush()
				pro_id = product.proid
			except:
				flash("数据库错误!")
				return redirect('%s%s' %('/manage/add_product?pid=',pid))


			# 颜色部分
			colortitle = request.form.getlist('colortitle')
			color = request.form.getlist('color')
			number = request.form.getlist('number')
			proid = pro_id
			picurl = request.form.getlist('picid')
			cover = request.form.getlist('cover')
			# print picurl
			color_list = [ProColor(
							colortitle = str(colortitle[i]),
							color = str(color[i]),
							number = str(number[i]),
							cover = cover[i],
							picurl = picurl[i],
							proid = proid
						) for i in range(len(color))]
			color_check = db_session.query(ProColor).first()
			db_session.add_all(color_list)
			db_session.commit()
			db_session.flush()
			# 开始处理复杂的图片所属颜色id和排序问题

			# 开始处理得到排序
			give_picid('up',picurl,color_list,None) #调用图片排序添加proid函数

			db_session.commit()
			db_session.close()

			flash("添加成功,<span id='time'>3</span>秒后自动跳转管理页。")
			return redirect('%s%s' %('/manage/add_product?pid=',pid))
	# print recommend
	return render_template(
		"edit_product.html", 
		pagename='product',
		this = this,
		pid = pid,
		washlist = washlist,
		sizelist=sizelist,
		recommend= recommend,
		classifylist=classifylist,
		form = form)