Ejemplo n.º 1
0
def addProduct(request):
    if request.method=='POST':
        print request.POST
        ##print request.POST
        product_id=request.POST['productid']
        subcategory_count=request.POST['count']
        if (subcategory_count==""):
            subcategory_count=0
        subcategory_count=int(subcategory_count)
        subcategory_count+=1
        category=request.POST['category']
        subcategory=[]
        if (subcategory_count>0):
            for each in range(1,subcategory_count):
                temp=('subcategory'+str(each))
                subcategory.append(request.POST[temp])
        print subcategory
        #form Category_objects
        if (len(subcategory)==1):
            category_objects=Category.objects.filter(category_name=category,subcategory_1=subcategory[0])
        elif (len(subcategory)==2):
            category_objects=Category.objects.filter(category_name=category,subcategory_1=subcategory[0],subcategory_2=subcategory[1])
        elif (len(subcategory)==3):
            category_objects=Category.objects.filter(category_name=category,subcategory_1=subcategory[0],subcategory_2=subcategory[1],subcategory_3=subcategory[2])
        else:
            category_objects=Category.objects.filter(category_name=category,subcategory_1=None,subcategory_2=None,subcategory_3=None)
        """saved_category=[]
        saved_category.append(category_objects.subcategory_1);
        saved_category.append(category_objects.subcategory_2);
        saved_category.append(category_objects.subcategory_3);
        for each in range(0,len(subcategory)):
            if(saved_category[each]!=subcategory[each]):
                category_objects=create_new_category(category,subcategory)"""
        if len(category_objects) == 0:
            category_objects=create_new_category(category, subcategory)
            category_id=category_objects.id
        else:
            category_id=category_objects[0].id
        brand=request.POST['brand']
        unit_price=request.POST['unit_price']
        model=request.POST['model']
        description=request.POST['description']
        product_name=request.POST['product_name']
        currenttime=datetime.datetime.now()
        product_item=Products()
        product_item.product_name=product_name
        product_item.product_id=product_id
        product_item.category_id=category_id
        product_item.brand=brand
        product_item.model_no=model
        product_item.short_description=description
        product_item.baseUnitPrice=unit_price
        product_item.arrival=currenttime
        product_item.save()
        print product_item
        return HttpResponseRedirect('/Home/Admin/')