File packages/image_upload.py changed (mode: 100644) (index 9f18cd2..439c2c9) |
1 |
1 |
import csv |
import csv |
2 |
|
from sortedcontainers import SortedDict |
|
3 |
2 |
import re |
import re |
4 |
3 |
import sys |
import sys |
|
4 |
|
import inspect |
|
5 |
|
from sortedcontainers import SortedDict |
5 |
6 |
from packages import item_upload, barcode |
from packages import item_upload, barcode |
|
7 |
|
from packages.item_upload import errorPrint, warnPrint |
6 |
8 |
|
|
7 |
9 |
def searchSpecialImage(image): |
def searchSpecialImage(image): |
8 |
|
if(re.search(r'( SWATCH|SIZE )', image)): |
|
9 |
|
return True |
|
10 |
|
else: |
|
11 |
|
return False |
|
|
10 |
|
return bool(re.search(r'( SWATCH|SIZE )', image)) |
12 |
11 |
|
|
13 |
12 |
def getColorAttributeID(attributefile, product): |
def getColorAttributeID(attributefile, product): |
14 |
13 |
|
|
15 |
14 |
attributeid = '' |
attributeid = '' |
16 |
|
with open(attributefile['path'], mode = 'r', encoding = attributefile['encoding']) as item: |
|
|
15 |
|
with open(attributefile['path'], |
|
16 |
|
mode='r', encoding=attributefile['encoding']) as item: |
17 |
17 |
reader = csv.DictReader(item, delimiter=';') |
reader = csv.DictReader(item, delimiter=';') |
18 |
18 |
|
|
19 |
19 |
try: |
try: |
20 |
20 |
for row in reader: |
for row in reader: |
21 |
|
if(row['AttributeValue.backendName'] == product['color_name']): |
|
|
21 |
|
if row['AttributeValue.backendName'] == product['color_name']: |
22 |
22 |
attributeid = row['AttributeValue.id'] |
attributeid = row['AttributeValue.id'] |
23 |
|
if(not( attributeid )): |
|
24 |
|
print("For SKU : {0}, Color : {1} not found!\n".format(product['item_sku'], product['color_name'])) |
|
25 |
|
except Exception as err: |
|
26 |
|
print("ERROR @ Color ID search: line: {0}, err: {1}".format(sys.exc_info()[2].tb_lineno, err)) |
|
|
23 |
|
if not attributeid: |
|
24 |
|
warn = f"Color:{product['color_name']} not found in {product['item_sku']}!\n" |
|
25 |
|
warnPrint(warn, |
|
26 |
|
inspect.currentframe().f_back.f_lineno) |
|
27 |
|
except KeyError as err: |
|
28 |
|
errorPrint("key not found in attribute file", err, |
|
29 |
|
sys.exc_info()[2].tb_lineno) |
27 |
30 |
|
|
28 |
31 |
return attributeid |
return attributeid |
29 |
32 |
|
|
|
... |
... |
def getColorAttributeID(attributefile, product): |
31 |
34 |
def imageUpload(flatfile, attributefile, exportfile, uploadfolder, filename): |
def imageUpload(flatfile, attributefile, exportfile, uploadfolder, filename): |
32 |
35 |
|
|
33 |
36 |
try: |
try: |
34 |
|
Data = SortedDict() |
|
|
37 |
|
data = SortedDict() |
35 |
38 |
|
|
36 |
39 |
column_names = ['VariationID', 'Multi-URL', 'connect-variation', 'mandant', |
column_names = ['VariationID', 'Multi-URL', 'connect-variation', 'mandant', |
37 |
40 |
'listing', 'connect-color'] |
'listing', 'connect-color'] |
38 |
|
attributeID = '' |
|
|
41 |
|
attribute_id = '' |
39 |
42 |
variation_id = 0 |
variation_id = 0 |
40 |
43 |
|
|
41 |
44 |
with open(flatfile['path'], mode='r', encoding=flatfile['encoding']) as item: |
with open(flatfile['path'], mode='r', encoding=flatfile['encoding']) as item: |
42 |
45 |
reader = csv.DictReader(item, delimiter=';') |
reader = csv.DictReader(item, delimiter=';') |
43 |
|
for index, row in enumerate( reader ): |
|
|
46 |
|
for index, row in enumerate(reader): |
44 |
47 |
linkstring = '' |
linkstring = '' |
45 |
48 |
imglinks = [ |
imglinks = [ |
46 |
49 |
row['main_image_url'], |
row['main_image_url'], |
|
... |
... |
def imageUpload(flatfile, attributefile, exportfile, uploadfolder, filename): |
55 |
58 |
] |
] |
56 |
59 |
|
|
57 |
60 |
num = 1 |
num = 1 |
58 |
|
variation_id = item_upload.get_variationid(exportfile=exportfile, sku=row['item_sku']) |
|
|
61 |
|
variation_id = item_upload.getVariationId( |
|
62 |
|
exportfile=exportfile, sku=row['item_sku']) |
59 |
63 |
try: |
try: |
60 |
|
if(imglinks[0]): |
|
61 |
|
for img in imglinks: |
|
62 |
|
if(not(searchSpecialImage(img))): |
|
63 |
|
if(not(linkstring)): |
|
64 |
|
if(img): |
|
65 |
|
linkstring += img + ';' + str( num ) |
|
66 |
|
num += 1 |
|
67 |
|
else: |
|
68 |
|
if(img): |
|
69 |
|
linkstring += ',' + img + ';' + str( num ) |
|
70 |
|
num += 1 |
|
71 |
|
if(searchSpecialImage(img)): |
|
72 |
|
print("\n{0} is a special image\n".format(img)) |
|
73 |
|
if(not(linkstring)): |
|
74 |
|
if(img): |
|
75 |
|
linkstring += img + ';' + str( num ) |
|
76 |
|
num += 1 |
|
77 |
|
else: |
|
78 |
|
if(img): |
|
79 |
|
linkstring += ',' + img + ';' + str( num ) |
|
80 |
|
num += 1 |
|
|
64 |
|
if not imglinks[0]: |
|
65 |
|
break |
|
66 |
|
for img in [i for i in imglinks if i]: |
|
67 |
|
if not searchSpecialImage(img): |
|
68 |
|
if not linkstring: |
|
69 |
|
linkstring += f"{img};{str(num)}" |
|
70 |
|
num += 1 |
|
71 |
|
continue |
|
72 |
|
linkstring += f",{img};{str(num)}" |
|
73 |
|
num += 1 |
|
74 |
|
continue |
|
75 |
|
print(f"\n{img} is a special image\n") |
|
76 |
|
if not linkstring: |
|
77 |
|
linkstring += f"{img};{str(num)}" |
|
78 |
|
num += 1 |
|
79 |
|
continue |
|
80 |
|
linkstring += f",{img};{str(num)}" |
|
81 |
|
num += 1 |
81 |
82 |
|
|
82 |
83 |
|
|
83 |
84 |
except Exception as err: |
except Exception as err: |
84 |
|
print("Error @ linkstring building line no: {0} : {1}".format(sys.exc_info()[2].tb_lineno, err)) |
|
|
85 |
|
errorPrint("Link string building failed", err, |
|
86 |
|
sys.exc_info()[2].tb_lineno) |
85 |
87 |
|
|
86 |
88 |
try: |
try: |
87 |
|
attributeID = getColorAttributeID(attributefile=attributefile, product=row) |
|
|
89 |
|
attribute_id = getColorAttributeID( |
|
90 |
|
attributefile=attributefile, product=row) |
88 |
91 |
except Exception as err: |
except Exception as err: |
89 |
|
print("Error @ get Color Attribute ID {0}\n".format(err)) |
|
|
92 |
|
warnPrint( |
|
93 |
|
f"get attribute ID of color {row['color_name']} failed", |
|
94 |
|
linenumber=inspect.currentframe().f_back.f_lineno, |
|
95 |
|
err=err) |
90 |
96 |
|
|
91 |
97 |
|
|
92 |
|
values=[variation_id, linkstring, 1, -1, |
|
93 |
|
-1, attributeID] |
|
|
98 |
|
values = [variation_id, linkstring, 1, -1, |
|
99 |
|
-1, attribute_id] |
94 |
100 |
|
|
95 |
|
Data[row['item_sku']] = dict(zip(column_names, values)) |
|
|
101 |
|
data[row['item_sku']] = dict(zip(column_names, values)) |
96 |
102 |
|
|
97 |
103 |
except Exception as err: |
except Exception as err: |
98 |
|
print("Error @ imageupload line: {0} : {1}".format(sys.exc_info()[2].tb_lineno, err)) |
|
|
104 |
|
errorPrint(f"flatfile read failed on index:{index}", |
|
105 |
|
err, sys.exc_info()[2].tb_lineno) |
99 |
106 |
|
|
100 |
|
barcode.writeCSV(dataobject=Data, name='Image_', columns=column_names, upload_path=uploadfolder, item=filename) |
|
101 |
|
return Data |
|
|
107 |
|
barcode.writeCSV(dataobject=data, name='Image_', columns=column_names, |
|
108 |
|
upload_path=uploadfolder, item=filename) |
|
109 |
|
return data |