示例#1
0
def create_product(request):	
	try:
		if request.method == "POST":
			product_form = ProductForm(request.POST)
			if product_form.is_valid():
				# save basic information of product
				p = product_form.save()

				# save ingredients
				ingredients = request.POST.getlist("ingredients")
				p.ingredients = ingredients

				# save price policy
				price_policies = request.POST.getlist("price_policies")
				p.price_policies = [PricePolicy(**eval(e)) for e in price_policies]

				p.save()

				# save photos
				writeFiles(p.photo_base_folder, request.FILES.getlist('file'))

				result = {'status': 1, 'redirect_url': '/products/'}
			else:
				result = {'form': str(product_form), 'status': 0}
			return HttpResponse(simplejson.dumps(result), mimetype='application/json')	
		else:
			product_form = ProductForm()
			context = RequestContext(request, {'product_form': product_form})
			return render_to_response('operate_product.html', context)
	except Exception as e:
		print e
示例#2
0
def update_product(request, product_id):
	product = Product.objects.get(id=product_id)
	try:
		if request.method == "POST":
			product_form = ProductForm(request.POST, instance=product)
			if product_form.is_valid():
				# save basic information of product
				product = product_form.save()

				# save ingredients
				ingredients = request.POST.getlist("ingredients")	
				product.ingredients = ingredients
				
				# save price policy
				price_policies = request.POST.getlist("price_policies")
				product.price_policies = [PricePolicy(**eval(e)) for e in price_policies]

				product.save()

				# update photos
				photo_dir = product.photo_base_folder
				print request.POST.getlist("existing_files")
				removeTrashFiles(photo_dir, request.POST.getlist("existing_files"))
				writeFiles(photo_dir, request.FILES.getlist('file'))

				result = {'status': 1, 'redirect_url': '/products/'}		
			else:
				result = {'form': str(product_form), 'status': 0}
			return HttpResponse(simplejson.dumps(result), mimetype='application/json')	
		else:	
			product_form = ProductForm(instance=product)		
			context = RequestContext(request, {
				'product_form': product_form, 
				'ingredients': product.ingredients,
				'price_policies': product.price_policies,
				'photo_urls': product.photo_urls})
			return render_to_response('operate_product.html', context)
	except Exception as e:
		print e