Exemplo n.º 1
0
    def post(self, request):
        print(request.FILES)
        csv_file = request.FILES['file']
        try:
            if not csv_file.name.endswith('.csv'):
                return self.error_response(code='HTTP_400_BAD_REQUEST',
                                           message=NOT_A_CSV_FORMAT)
            data_set = csv_file.read().decode('UTF-8')
            io_string = io.StringIO(data_set)
            next(io_string)
            bulk_mgr = BulkCreateManager(chunk_size=20)
            for row in csv.reader(io_string, delimiter=',', quotechar="|"):
                print(row)
                category = Category.objects.filter(
                    name__icontains=row[2]).last()
                existing_product = Product.objects.filter(product_id=row[0])
                if existing_product:
                    update_values = {
                        'name': row[1],
                        'category': category,
                        'size': row[3],
                        'color': row[4],
                        'quantity': row[5],
                        'description': row[6],
                        'brand': row[7],
                        'mrp': row[8],
                        'hsn_code': row[12],
                        'tax_rate': row[13],
                        'moq': row[14],
                        'unit': row[15]
                    }
                    if row[9]:
                        update_values['offer_prize'] = row[9]
                    if row[10]:
                        update_values['lowest_selling_rate'] = row[10]
                    if row[11]:
                        update_values['highest_selling_rate'] = row[11]

                    existing_product.update(**update_values)

                else:
                    product_id = row[0]
                    try:
                        if not product_id:
                            product_id = id_generator()
                    except Exception as e:
                        db_logger.exception(e)
                    shop = Shop.objects.filter(user=request.user).last()
                    product = Product(name=row[1],
                                      category=category,
                                      size=row[3],
                                      color=row[4],
                                      quantity=row[5],
                                      description=row[6],
                                      brand=row[7],
                                      mrp=row[8],
                                      hsn_code=row[12],
                                      product_id=product_id,
                                      tax_rate=row[13],
                                      moq=row[14],
                                      unit=row[15],
                                      shop=shop)
                    if row[9]:
                        product.offer_prize = row[9]
                    if row[10]:
                        product.lowest_selling_rate = row[10]
                    if row[11]:
                        product.highest_selling_rate = row[11]

                    bulk_mgr.add(product)
            bulk_mgr.done()
            return self.success_response(code='HTTP_200_OK',
                                         message=DATA_SAVED_SUCCESSFULLY)
        except Exception as e:
            print(e)
            db_logger.exception(e)
            return self.error_response(code='HTTP_500_INTERNAL_SERVER_ERROR',
                                       message=GENERAL_ERROR)