How can upload csv file using pandas

# ------------------------- Import products by CSV file --------------------------------------

from rest_framework.decorators import api_view

import urllib

import os

from django.core.files import File # you need this somewhere

import pandas as pd

def delete_exception_product(pk):

Product.objects.get(pk=pk).delete()

print("Exception Product deleted")

@api_view(['POST'])

def import_product_from_csv(request):

# Basic Info

seller_id = 3

color_parent_id = 21 # Select filter_id from filter where filter_title='Color'

size_parent_id = 18 # Select filter_id from filter where filter_title='Size'

all_size_id = [19, 20, 25, 66, 67, 68, 69, 137, 138] # ['XXXS', 'XXS', 'XS', 'S', 'M', 'L', 'XL', 'XXL', 'XXXL']

if not request.FILES['feed_product'].name.endswith('.xlsx'):

return Response({"message": "File is not CSV type"}, status=status.HTTP_400_BAD_REQUEST)

file_name = request.FILES['feed_product'].name # women_dress.csv

header_middle = []

filter_dict = {}

total_filter = 0

if file_name == 'women_kurta_sets.xlsx':

browse_node = 17 # Women > Indian & Fusion > Ethnic Sets> Kurta Sets

filter_dict = {"bundles": 74, "material": 9, "closure": 77, "occasion": 111,

"ornamentation": 112, "print_pattern_type": 113, "wash_care": 114, "weave_type": 76,

"transparancy": 116, "sleeve_length": 117, "neckline": 118, "stitch": 119, "length": 120,

"hemline": 121}

header_middle = list(filter_dict.keys())

total_filter = 14

elif file_name == 'women_accessories.csv':

browse_node = 83 # Women > Fashion > Clothing > Accessories

pass

header_first = ['product_category', 'product_name', 'sku', 'brand_name', 'style_code', 'country_origin',

'description', 'handling_time', 'local_delivery_charge', 'zonal_delivery_charge',

'national_delivery_charge', 'browse_node', 'color', 'size']

header_last = ['qty', 'list_price', 'discount_price', 'mrp', 'image_url',

'other_image_url_1', 'other_image_url_2', 'other_image_url_3', 'other_image_url_4',

'other_image_url_5',

'other_image_url_6', 'other_image_url_7', 'other_image_url_8', 'other_image_url_9',

'other_image_url_10', 'chart_name', 'bust', 'front_length', 'to_fit_waist', 'waist', 'hips',

'shoulder', 'inseam_length']

header_name = header_first + header_middle + header_last

csv_raw_data = pd.read_excel(request.FILES.get('feed_product'), header=0, names=header_name)

error_dict = {"basic_other_info_error": [], "variant_error": [], "image_error": [], "size_chart_error": []}

total_product = len(csv_raw_data['product_name'])

total_success = 0

for j in range(0, total_product):

product_name = csv_raw_data['product_name'][j]

sku = csv_raw_data['sku'][j]

pv = ProductVariation.objects.filter(sku=sku)

if pv:

error_dict["variant_error"].append(f"Duplicate entry {sku} for {product_name} (Row line: {j + 2})")

continue

try:

color = Filter.objects.get(parent_id=color_parent_id, filter_title=csv_raw_data['color'][j])

color_id = color.filter_id

except Exception as e:

error_dict['basic_other_info_error'].append(

f"Color - {csv_raw_data['color'][j]} is invalid for {product_name} (Row line: {j + 2})")

continue

try:

size = Filter.objects.get(parent_id=size_parent_id, filter_title=csv_raw_data['size'][j])

size_id = size.filter_id

except Exception as e:

print(e)

error_dict['basic_other_info_error'].append(

f"Size - {csv_raw_data['size'][j]} is invalid for {product_name} (Row line: {j + 2})")

continue

if total_filter == 0:

error_dict["basic_other_info_error"].append(

f"Minimum 1 filter is required for {product_name} (Row line: {j + 2})")

continue

try:

browse_node = int(csv_raw_data['browse_node'][j])

filter_id_list = []

for parent_filter_key in filter_dict:

categroy_filter = Filter.objects.values('filter_id').filter(

parent_id=filter_dict[parent_filter_key],

filter_title=csv_raw_data[parent_filter_key][j].strip())

if len(categroy_filter) == 0:

error_dict['basic_other_info_error'].append(

f"filter - {parent_filter_key} is invalid for {product_name} (Row line: {j + 2})")

continue

elif len(categroy_filter) > 1:

error_dict['basic_other_info_error'].append(

f"filter - {parent_filter_key} is Conflict situations arise for {product_name} (Row line: {j + 2})")

continue

else:

filter_id_list.append(categroy_filter[0:1][0]['filter_id'])

if len(filter_id_list) != total_filter:

error_dict['basic_other_info_error'].append(

f"filter - filter is mismatch for {product_name} (Row line: {j + 2})")

continue

except Exception as e:

error_dict['basic_other_info_error'].append(

f"filter - filter is invalid for {product_name} (Row line: {j + 2})")

continue

brand_name = csv_raw_data['brand_name'][j]

style_code = csv_raw_data['style_code'][j]

country_origin = csv_raw_data['country_origin'][j]

description = csv_raw_data['description'][j]

handling_time = csv_raw_data['handling_time'][j]

local_delivery_charge = csv_raw_data['local_delivery_charge'][j]

zonal_delivery_charge = csv_raw_data['zonal_delivery_charge'][j]

national_delivery_charge = csv_raw_data['national_delivery_charge'][j]

qty = csv_raw_data['qty'][j]

list_price = csv_raw_data['list_price'][j]

discount_price = csv_raw_data['discount_price'][j]

mrp = csv_raw_data['mrp'][j]

image_url = csv_raw_data['image_url'][j]

# Add Product basic info and others

try:

# product = Product.objects.create(

# product_name=product_name, brand_name=brand_name,

# seller_id=seller_id,

# featured_seller=False,

# category_id=browse_node, style_code=style_code,

# country_origin=country_origin, description=description,

# local_delivery_charge=int(local_delivery_charge),

# handling_time=str(handling_time),

# zonal_delivery_charge=int(zonal_delivery_charge),

# national_delivery_charge=int(national_delivery_charge)

# )

product, created = Product.objects.get_or_create(

product_name=product_name, brand_name=brand_name,

seller_id=seller_id,

featured_seller=False,

category_id=browse_node, style_code=style_code,

country_origin=country_origin

)

product.description = description

product.local_delivery_charge = int(local_delivery_charge)

product.handling_time = str(handling_time)

product.zonal_delivery_charge = int(zonal_delivery_charge)

product.national_delivery_charge = int(national_delivery_charge)

product.save()

print(f" __________Basic Product Info added for product {j + 1}_____________")

print(f" __________Other Product Info added for product {j + 1} _____________")

print("Product ID", product.product_id)

product_id = product.product_id

try:

product_filter_ids = [ProductFilter.objects.create(

product_id=product_id, filter_id=f_id) for f_id in filter_id_list]

print(f" __________Product Filter added for product {j + 1}_____________")

print(product_filter_ids)

except Exception as e:

error_dict['basic_other_info_error'].append(

f"filter - Product Filter is not created for {product_name} (Row line: {j + 2})")

delete_exception_product(product_id)

continue

# Add Product Variation

try:

product_variation = ProductVariation.objects.create(

product_id=product_id, sku=sku, color_id=int(color_id),

size_id=int(size_id), qty=int(qty), mrp=int(mrp),

list_price=int(list_price), discount_price=int(discount_price)

)

print(f" __________Variant added for product {j + 1} _____________")

# Add Product Image

try:

if len(image_url) > 10:

result = urllib.request.urlretrieve(image_url)

product_image = ProductImage(product_id=product_id,

variant_id=color_id)

product_image.image_url.save(os.path.basename(image_url),

File(open(result[0], 'rb')))

product_image.save()

print(f" ______Main Image uploaded from server to cdn for product {j + 1}___")

for index in range(1, 11):

if len(image_url) > 10:

result = urllib.request.urlretrieve(csv_raw_data[f"other_image_url_{index}"][j])

product_image = ProductImage(product_id=product_id,

variant_id=color_id)

product_image.image_url.save(os.path.basename(csv_raw_data[f"other_image_url_{index}"][j]),

File(open(result[0], 'rb')))

product_image.save()

print(f" ______Other Image uploaded from server to cdn for product {j + 1}___")

# Add Product Size chart

try:

chart_name = csv_raw_data['chart_name'][j]

bust_list = csv_raw_data['bust'][j].split(",")

front_length_list = csv_raw_data['front_length'][j].split(",")

to_fit_waist_list = csv_raw_data['to_fit_waist'][j].split(",")

waist_list = csv_raw_data['waist'][j].split(",")

hips_list = csv_raw_data['hips'][j].split(",")

shoulder_list = csv_raw_data['shoulder'][j].split(",")

inseam_length_list = csv_raw_data['inseam_length'][j].split(",")

if (len(bust_list) == len(all_size_id) and len(front_length_list) == len(all_size_id) and len(

to_fit_waist_list) == len(all_size_id)

and len(waist_list) == len(all_size_id) and len(hips_list) == len(all_size_id) and len(

shoulder_list) == len(all_size_id)

and len(inseam_length_list) == len(all_size_id)):

for s_index in range(0, len(all_size_id)):

SizeChart.objects.create(

size_id=all_size_id[s_index],

product_id=product_id,

bust=bust_list[s_index],

front_length=front_length_list[s_index],

to_fit_waist=to_fit_waist_list[s_index],

waist=waist_list[s_index],

hips=hips_list[s_index],

shoulder=shoulder_list[s_index],

inseam_length=inseam_length_list[s_index],

chart_name=chart_name

)

total_success += 1

print(f" ______Size chart created for product {j + 1}___")

else:

error_dict['size_chart_error'].append(

f"product Size chart value is invalid for {product_name} (Row line: {j + 2})")

delete_exception_product(product_id)

continue

except Exception as e:

print(e)

error_dict['size_chart_error'].append(

f"product Size chart is not created for {product_name} (Row line: {j + 2})")

delete_exception_product(product_id)

continue

except Exception as e:

error_dict['image_error'].append(

f"Some product image is not being uploaded for {product_name} (Row line: {j + 2})")

delete_exception_product(product_id)

continue

except Exception as e:

error_dict['variant_error'].append(

f"product variation is not created for {product_name} (Row line: {j + 2})")

delete_exception_product(product_id)

continue

except Exception as e:

error_dict['basic_other_info_error'].append(

f"product is not created for {product_name} (Row line: {j + 2})")

continue

total_error = len(error_dict['basic_other_info_error']) \

+ len(error_dict['variant_error']) \

+ len(error_dict['image_error']) \

+ len(error_dict['size_chart_error'])

return Response(

{

"success": f"{total_success} products are uploaded out of {total_product}"

, "error_count": total_error

, "errors": error_dict

}, status=201)

View More...

How can make join with django orm?

categroy_filter = CategoryFilter.objects.values('filter__filter_id', 'filter__filter_title').filter(
category_id=1,
filter__filter_title="xxxxx")
get_first_filter_id = categroy_filter[0:1][0]['filter__filter_id']
View More...

How can convert excel to csv in python?

 excel_raw_data = pd.read_excel(request.FILES.get('feed_product'))
# Convert excel to csv file
excel_raw_data.to_csv("feed_product.csv", index=None, header=True)
# read csv file
csv_raw_data = pd.DataFrame(pd.read_csv("feed_product.csv"))
View More...

How can upload image from url in Django(python)

import urllib
import os
from django.core.files import File # you need this somewhere
image_url = request.data['image_url']
result = urllib.request.urlretrieve(image_url)
product_image = ProductImage(product_id=1, variant_id=46)
product_image.image_url.save(os.path.basename(image_url), File(open(result[0], 'rb')))
serializer = ProductImageSerializer(data=product_image)
View More...

1 2 3 Next Last