def product_add(request,message=None): if request.POST: product_form = ProductForm(request.POST,request.FILES,prefix='product') action_form = ProductActionInlineForm(request.POST,prefix='action') if product_form.is_valid(): product = product_form.save() #set the user who added it product.added_by = request.user #set the slug product.slug = slugify(product.name) if action_form.is_valid(): #send the new product to the action form action = action_form.save() action.product = product action.save() #resave the product to finish up product.save() #create the revision revision.user = request.user revision.comment = "created" #save the citations citation_from_json(request.POST['product-citations_json'],product) return HttpResponseRedirect(product.get_absolute_url()) else: message = "Please correct the errors below" else: product_form = ProductForm(prefix='product') action_form = ProductActionInlineForm(prefix='action') message = "Add the product details below" return render_to_response("targets/product_add.html", {"product_form": product_form, "action_form":action_form, "message":message}, context_instance = RequestContext(request))
def product_edit(request,slug): product = get_object_or_404(Product,slug=slug) if request.POST: product_form = ProductForm(request.POST,request.FILES,instance=product) if product_form.is_valid(): product = product_form.save() product.edited_by.add(request.user) product.save() #create the revision revision.user = request.user revision.comment = "Changed %s" % ", ".join(product_form.changed_data) #save the citations citation_from_json(request.POST['citations_json'],product) return HttpResponseRedirect(product.get_absolute_url()) else: message = "Please correct the errors below" else: product_form = ProductForm(instance=product) message = "Edit the product details below" return render_to_response("targets/product_edit.html", {"message":message,"product_form": product_form}, context_instance = RequestContext(request))