Пример #1
0
def addProduct(request):
	tflag = "add"
	# print request.user.id
	# A boolean value for telling the template whether the registration was successful.
	# Set to False initially. Code changes value to True when registration succeeds.
	# added = False
	
	if request.method == 'POST':
		product_form = ProductForm(request.POST)
		pic_form = productPicform(request.POST)
		if product_form.is_valid() :
			product = product_form.save(commit=False)
			product.sold_by = request.user
			product.save()
			# added = True
			# message = "Product: "+product.name+" is added successfullly."
			messages.success(request, 'Product: %s is successfullly added.' % product.name)

		if pic_form.is_valid():
			if 'picture' in request.FILES:
				pic = pic_form.save(commit=False)
				pic.product = product
				pic.picture = request.FILES['picture']
				pic.save()
		return HttpResponseRedirect(reverse('myaccount:myproducts'))
	else:
		product_form = ProductForm()
		pic_form = productPicform()

	context = {'product_form':product_form,'pic_form':pic_form}
	return render(request, 'myaccount/addproduct.html', context)
Пример #2
0
def editProduct(request,product_id):
	tflag = "edit"
	# print request.user.id
	# A boolean value for telling the template whether the registration was successful.
	# Set to False initially. Code changes value to True when registration succeeds.
	# added = False
	p = Product.objects.get(pk=product_id)
	try:
		pic_pre = productPic.objects.get(product = p)
	except productPic.DoesNotExist:
		pic_pre = None
	if p.sold_by != request.user:
		return HttpResponse("You don't have permission")
	if request.method == 'POST':
		product_form = ProductForm(request.POST,instance=p)
		pic_form = productPicform(request.POST,instance=pic_pre)
		if product_form.is_valid():
			product = product_form.save(commit=False)
			product.sold_by = request.user
			product.save()
			# added = True
			messages.success(request, 'Product: %s is successfullly updated.' % product.name)

		if pic_form.is_valid():
			pic_new = pic_form.save(commit=False)
			pic_new.product = product
			if 'picture' in request.FILES:
				# Delete the old picture
				if pic_pre:
					os.remove(os.path.join(settings.MEDIA_ROOT,pic_pre.picture.name))
				pic_new.picture = request.FILES['picture']
				pic_new.save()

		return HttpResponseRedirect(reverse('myaccount:myproducts'))
	else:
		product_form = ProductForm(instance=p)
		pic_form = productPicform(instance=pic_pre)
	context = {'product_form':product_form,'pic_form':pic_form,'tflag':tflag, 'product_id':product_id}
	return render(request, 'myaccount/addproduct.html', context)