Beispiel #1
0
def add(request):
    context = {}
    context.update(csrf(request))
    if request.user.is_authenticated():
        context['login_mode'] = True
        context['user'] = request.user
        if request.user.is_superuser:
            context['user_admin'] = True
    form = AddProductForm()
    context['form'] = form
    if request.method == 'POST':
        form = AddProductForm(request.POST)

        if form.is_valid():
            product = form.save(commit=False)
            product.user = request.user
            product.save()
            dict = {'add': True, 'username': request.user.username}
            data = json.dumps(dict, cls=JSONEncoder)
            return HttpResponse(data, content_type="application/json")
        else:
            errors_dict = {}
            if form.errors:
                for error in form.errors:
                    e = form.errors[error]
                    errors_dict[error] = unicode(e)
            data = json.dumps(errors_dict, cls=JSONEncoder)
            return HttpResponse(data, content_type="application/json")

    return render_to_response('products/add-product.html', context)
Beispiel #2
0
	def view( self, request ):

		form = AddProductForm( request.REQUEST )

		update_info = {}
		error = {}	

		if form.is_valid():
			if 'id' not in request.REQUEST or request.REQUEST[ 'id' ] == AddChangeProductAction.ID_NOT_DEFINED:

				form.save()
				product = Product.objects.all().order_by( '-id' )[ 0 ]
			else:
				product = Product.objects.get( id = request.REQUEST[ 'id' ] )
				
				product.name = form.cleaned_data[ 'name' ]
				product.category = form.cleaned_data[ 'category' ]
				product.producer = form.cleaned_data[ 'producer' ]
				product.price = form.cleaned_data[ 'price' ]
				product.country = form.cleaned_data[ 'country' ]

				product.save()

			is_error = False

			update_info[ 'name' ] = product.name
			update_info[ 'product_category_name' ] = ProductCategory.objects.get( id = product.category_id ).name
			update_info[ 'producer_name' ] = Producer.objects.get( id = product.producer_id ).name
			update_info[ 'country' ] = Country.objects.get( id = product.country_id ).name
			update_info[ 'price' ] = product.price
			update_info[ 'id' ] = product.id
		else:
			is_error = True
			error[ 'error_description' ] = ""
			
		return JSONView.view(	self, request, 
									{ 
										'is_error': is_error, 
										'update_info': update_info 
									} )