File packages/item_upload.py changed (mode: 100644) (index 381919e..c5bcfb3) |
... |
... |
def itemUpload(flatfile, intern, stocklist, folder, input_data, filename): |
48 |
48 |
# Get sets of all colors and sizes for each parent |
# Get sets of all colors and sizes for each parent |
49 |
49 |
# to find if there are some with only one attribute value for all childs |
# to find if there are some with only one attribute value for all childs |
50 |
50 |
color_size_sets = {} |
color_size_sets = {} |
51 |
|
color_size_sets = findSimilarAttr(flatfile) |
|
|
51 |
|
color_size_sets = findSimilarAttr(flatfile, ['size_name']) |
52 |
52 |
|
|
53 |
53 |
package_properties = getProperties(flatfile) |
package_properties = getProperties(flatfile) |
54 |
54 |
group_parent = '' |
group_parent = '' |
|
... |
... |
def getProperties(flatfile): |
310 |
310 |
properties['width'] = int(float(row['package_width'])) |
properties['width'] = int(float(row['package_width'])) |
311 |
311 |
properties['weight'] = int(float(row['package_weight'])) |
properties['weight'] = int(float(row['package_weight'])) |
312 |
312 |
except ValueError as err: |
except ValueError as err: |
313 |
|
error.errorPrint("Parent has no package measurements", err, |
|
314 |
|
sys.exc_info()[2].tb_lineno) |
|
|
313 |
|
error.errorPrint( |
|
314 |
|
msg="Parent has no package measurements", err=err, |
|
315 |
|
linenumber=sys.exc_info()[2].tb_lineno) |
315 |
316 |
sys.exit() |
sys.exit() |
|
317 |
|
except KeyError as err: |
|
318 |
|
msg = str(f"getProperties key: {err} not found") |
|
319 |
|
error.errorPrint(msg=msg, err='', |
|
320 |
|
linenumber=sys.exc_info()[2].tb_lineno) |
316 |
321 |
except Exception as err: |
except Exception as err: |
317 |
|
error.errorPrint("getProperties setting values failed", err, |
|
318 |
|
sys.exc_info()[2].tb_lineno) |
|
|
322 |
|
error.errorPrint( |
|
323 |
|
msg="getProperties setting values failed", err=err, |
|
324 |
|
linenumber=sys.exc_info()[2].tb_lineno) |
319 |
325 |
|
|
320 |
326 |
return properties |
return properties |
321 |
327 |
|
|
322 |
328 |
def getAttributes(dataset, sets): |
def getAttributes(dataset, sets): |
|
329 |
|
""" |
|
330 |
|
Parameter: |
|
331 |
|
dataset [Dictionary] => row of the flatfile as dictionary |
|
332 |
|
sets [Dictionary] => attribute sets mapped to parent sku's by |
|
333 |
|
"findSimilarAttr()" |
|
334 |
|
|
|
335 |
|
Description: |
|
336 |
|
Build the attribute string of the color and size |
|
337 |
|
Elastic Sync upload format: |
|
338 |
|
{attr. name}:{value};{attr.2 name}:{value} |
|
339 |
|
|
|
340 |
|
Return: |
|
341 |
|
output_string |
|
342 |
|
""" |
323 |
343 |
|
|
324 |
344 |
output_string = '' |
output_string = '' |
325 |
345 |
try: |
try: |
326 |
346 |
if dataset['parent_sku'] in list(sets.keys()): |
if dataset['parent_sku'] in list(sets.keys()): |
327 |
|
if len(sets[dataset['parent_sku']]['color']) > 1: |
|
328 |
|
output_string = 'color_name:' + dataset['color_name'] |
|
|
347 |
|
output_string = 'color_name:' + dataset['color_name'] |
329 |
348 |
else: |
else: |
330 |
349 |
print("{0} not found in {1}" |
print("{0} not found in {1}" |
331 |
350 |
.format(dataset['parent_sku'], ','.join(list(sets.keys())))) |
.format(dataset['parent_sku'], ','.join(list(sets.keys())))) |
|
... |
... |
def getAttributes(dataset, sets): |
333 |
352 |
error.errorPrint("Adding of color attribute failed", err, |
error.errorPrint("Adding of color attribute failed", err, |
334 |
353 |
sys.exc_info()[2].tb_lineno) |
sys.exc_info()[2].tb_lineno) |
335 |
354 |
try: |
try: |
336 |
|
if len(sets[dataset['parent_sku']]['size']) > 1: |
|
|
355 |
|
if len(sets[dataset['parent_sku']]['size_name']) > 1: |
337 |
356 |
if not output_string: |
if not output_string: |
338 |
357 |
output_string = 'size_name:' + dataset['size_name'] |
output_string = 'size_name:' + dataset['size_name'] |
339 |
358 |
else: |
else: |
|
... |
... |
def getAttributes(dataset, sets): |
343 |
362 |
sys.exc_info()[2].tb_lineno) |
sys.exc_info()[2].tb_lineno) |
344 |
363 |
return output_string |
return output_string |
345 |
364 |
|
|
346 |
|
def findSimilarAttr(flatfile): |
|
|
365 |
|
def findSimilarAttr(flatfile, attribute): |
|
366 |
|
""" |
|
367 |
|
Parameter: |
|
368 |
|
flatfile [Dictionary] => Dictionary with a path and a |
|
369 |
|
encoding string |
|
370 |
|
attribute [List of Strings] => Names of the columns in the flatfile |
|
371 |
|
|
|
372 |
|
Description: |
|
373 |
|
Detect the amount different elements of a attribute within a |
|
374 |
|
group of variations of the same parent variation. |
|
375 |
|
|
|
376 |
|
Return: |
|
377 |
|
data => Dictionary of parent sku's mapped to |
|
378 |
|
sets of different values |
|
379 |
|
""" |
347 |
380 |
|
|
348 |
381 |
data = {} |
data = {} |
349 |
382 |
|
|
|
... |
... |
def findSimilarAttr(flatfile): |
351 |
384 |
reader = csv.DictReader(item, delimiter=";") |
reader = csv.DictReader(item, delimiter=";") |
352 |
385 |
|
|
353 |
386 |
for row in reader: |
for row in reader: |
354 |
|
# If it is a parent create a new dictionary with 2 sets for color and size |
|
355 |
|
if row['parent_child'].lower() == 'parent': |
|
356 |
|
color = set() |
|
357 |
|
size = set() |
|
358 |
|
data[row['item_sku']] = {'color':color, 'size':size} |
|
359 |
|
# If it is a child search through the data dictionary for a match |
|
|
387 |
|
p_sku = row['parent_sku'] |
|
388 |
|
if p_sku and p_sku not in data.keys(): |
|
389 |
|
data[p_sku] = dict() |
|
390 |
|
for attr in attribute: |
|
391 |
|
data[p_sku][attr] = set() |
360 |
392 |
if row['parent_child'] == 'child': |
if row['parent_child'] == 'child': |
361 |
|
for line in data: |
|
362 |
|
if row['parent_sku'] == line: |
|
363 |
|
data[row['parent_sku']]['color'].add(row['color_name']) |
|
364 |
|
data[row['parent_sku']]['size'].add(row['size_name']) |
|
|
393 |
|
for attr in attribute: |
|
394 |
|
data[p_sku][attr].add(row[attr]) |
365 |
395 |
return data |
return data |
366 |
396 |
|
|
367 |
397 |
def sortProducts(dataset): |
def sortProducts(dataset): |