Example #1
0
    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST, request.FILES)

        if form.is_valid():
            # <process form cleaned data>
            product_positions = dict()

            product_indexes = [p[p.rfind('_')+1:] for p in form.data.keys() if p.startswith('sub_select_')]
            unique_product_indexes = set(product_indexes)
            product_indexes_list = list(unique_product_indexes)

            sub_details = [p for p in form.data.keys() if p.startswith('sub_select_') and p.endswith('_p_' + product_indexes_list[0])]
            sub_details_count = len(sub_details)

            for product_index in product_indexes_list:
                product_positions[product_index] = dict()
                for i in range(0, sub_details_count):
                    product_positions[product_index][i] = form.data['sub_select_' + str(i) + '_p_' + product_index]
                product_positions[product_index]['count'] = form.data['count_input_p_' + product_index]

            new_product = Product(name=form.data['name'],
                              description=form.data['description'],
                              productStatus=1,
                              price=form.data['price'],
                              manufacturer=Manufacturer.objects.filter(id=1)[0],
                              category=Category.objects.filter(id=form.data['category'])[0],
                              seller=request.user)

            new_product.save()

            value_product_position = ValueProductPosition(product_detail_value=ProductDetailValue.objects.filter(id=form.data['select_0'])[0],
                                                          product=new_product)
            value_product_position.save()

            for key in product_positions.keys():
                product_position = ProductPosition(product=new_product, count=product_positions[key]['count'])
                product_position.save()
                for sub_key in product_positions[key].keys():
                    if sub_key == 'count':
                        continue
                    value_product_position = ValueProductPosition(product_detail_value=ProductDetailValue.objects.filter(id=product_positions[key][sub_key])[0],
                                                          product=new_product, product_position=product_position)
                    value_product_position.save()

            for file_key in request.FILES.keys():
                main_image = ProductImage(image=request.FILES[file_key], isMain=file_key == 'main_image', product=new_product)
                main_image.save()

            return render(request, 'index.html')

        return render(request, self.template_name, {'form': form})
def product(request):
	print "Product"
	print request.POST.get('title')
	print request.POST.get('amount')
	print request.session['user_id']
	product = Product()
	product.title = request.POST.get('title')
	product.amount = request.POST.get('amount')
	product.user = User.objects.get(id=request.session['user_id'])
	product.created_at = datetime.now()
	product.save()
	return redirect('/dashboard')
Example #3
0
    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST, request.FILES)

        if form.is_valid():
            # <process form cleaned data>
            product_positions = dict()

            product_indexes = [
                p[p.rfind('_') + 1:] for p in form.data.keys()
                if p.startswith('sub_select_')
            ]
            unique_product_indexes = set(product_indexes)
            product_indexes_list = list(unique_product_indexes)

            sub_details = [
                p for p in form.data.keys() if p.startswith('sub_select_')
                and p.endswith('_p_' + product_indexes_list[0])
            ]
            sub_details_count = len(sub_details)

            for product_index in product_indexes_list:
                product_positions[product_index] = dict()
                for i in range(0, sub_details_count):
                    product_positions[product_index][i] = form.data[
                        'sub_select_' + str(i) + '_p_' + product_index]
                product_positions[product_index]['count'] = form.data[
                    'count_input_p_' + product_index]

            new_product = Product(
                name=form.data['name'],
                description=form.data['description'],
                productStatus=1,
                price=form.data['price'],
                manufacturer=Manufacturer.objects.filter(id=1)[0],
                category=Category.objects.filter(id=form.data['category'])[0],
                seller=request.user)

            new_product.save()

            value_product_position = ValueProductPosition(
                product_detail_value=ProductDetailValue.objects.filter(
                    id=form.data['select_0'])[0],
                product=new_product)
            value_product_position.save()

            for key in product_positions.keys():
                product_position = ProductPosition(
                    product=new_product, count=product_positions[key]['count'])
                product_position.save()
                for sub_key in product_positions[key].keys():
                    if sub_key == 'count':
                        continue
                    value_product_position = ValueProductPosition(
                        product_detail_value=ProductDetailValue.objects.filter(
                            id=product_positions[key][sub_key])[0],
                        product=new_product,
                        product_position=product_position)
                    value_product_position.save()

            for file_key in request.FILES.keys():
                main_image = ProductImage(image=request.FILES[file_key],
                                          isMain=file_key == 'main_image',
                                          product=new_product)
                main_image.save()

            return render(request, 'index.html')

        return render(request, self.template_name, {'form': form})