210 lines
6.8 KiB
Python
210 lines
6.8 KiB
Python
import gspread
|
|
import json
|
|
import codecs
|
|
import sys
|
|
|
|
SERVICE_ACCOUNT_FILENAME = "../../secrets/shinnex-424321-b88b144bc9ef.json"
|
|
GOOGLE_SPEADSHEET_KEY = "1gZkjykb55aLWumBxHxj_ZUq_ktjGK4FlJsH_9qg-ThU" # from url
|
|
WORKSHEET_PRODUCTS = "Produkte"
|
|
WORKSHEET_FILAMENTS = "Filamente"
|
|
|
|
|
|
def add_underscore_at_position(text, position):
|
|
# Split the string into a list of words
|
|
words = text.split()
|
|
|
|
# Check if the text contains less than two words
|
|
if len(words) < 2:
|
|
return text
|
|
|
|
# Check if the position is valid
|
|
if position < 0 or position >= len(words) - 1:
|
|
raise ValueError("Invalid position for underscore")
|
|
|
|
# Add the underscore between the words at the specified position
|
|
words[position] = words[position] + '_' + words[position + 1]
|
|
|
|
# Remove the word at the next position since it's already added
|
|
del words[position + 1]
|
|
|
|
# Join the list back into a string
|
|
result = ' '.join(words)
|
|
|
|
return result
|
|
|
|
|
|
# values by google sheets are returned like "Gold (Gl\u00e4nzend)"
|
|
def decode_unicode_escapes(text):
|
|
# Decode Unicode escape sequences
|
|
return codecs.decode(text, 'unicode_escape')
|
|
|
|
|
|
def update_grouptask_products_list_options(index_json_path, products_list):
|
|
with open(index_json_path, 'r', encoding='utf-8') as json_file:
|
|
data = json.load(json_file)
|
|
|
|
options_list = []
|
|
|
|
for product in products_list:
|
|
# replace _ with " " just for better style in dashboard
|
|
product_name = product['name'].replace("_", " ")
|
|
|
|
option = f"#{product['id']} {product_name}, {product['product_variant']}, {product['product_characteristic_line_1']}, {product['product_characteristic_line_2']}"
|
|
options_list.append(option)
|
|
|
|
for task in data['tasks']:
|
|
for parameter in task['parameters']:
|
|
if parameter['parameterName'] == 'product_type_id':
|
|
parameter['options'] = options_list
|
|
|
|
with open(index_json_path, 'w', encoding='utf-8') as output_file:
|
|
json.dump(data, output_file, ensure_ascii=False, indent=2)
|
|
|
|
|
|
def update_grouptask_filaments_list_options(index_json_path, filaments_list):
|
|
with open(index_json_path, 'r', encoding='utf-8') as json_file:
|
|
data = json.load(json_file)
|
|
|
|
options_list = []
|
|
|
|
for filament in filaments_list:
|
|
option = f"{filament['id']} {filament['name_color']}, {filament['material']}, {filament['manufacturer']}"
|
|
options_list.append(option)
|
|
|
|
for task in data['tasks']:
|
|
for parameter in task['parameters']:
|
|
if parameter['parameterName'] == 'filament_type_id':
|
|
parameter['options'] = options_list
|
|
|
|
with open(index_json_path, 'w', encoding='utf-8') as output_file:
|
|
json.dump(data, output_file, ensure_ascii=False, indent=2)
|
|
|
|
|
|
def google_sheets_products():
|
|
gc = gspread.service_account(filename=SERVICE_ACCOUNT_FILENAME)
|
|
|
|
spreadsheet = gc.open_by_key(GOOGLE_SPEADSHEET_KEY)
|
|
|
|
worksheet = spreadsheet.worksheet(WORKSHEET_PRODUCTS)
|
|
|
|
data = worksheet.get_all_values()
|
|
|
|
# auto get the row index numbers for the columns
|
|
|
|
products = []
|
|
|
|
rowIndexProductId = -1
|
|
rowIndexProductName = -1
|
|
rowIndexProductVariant = -1
|
|
rowIndexProductCharacteristicLine1 = -1
|
|
rowIndexProductCharacteristicLine2 = -1
|
|
rowIndexNameSplitAtPosition = -1
|
|
|
|
for i in range(len(data[1])):
|
|
row = data[1][i]
|
|
|
|
if row == "#id":
|
|
rowIndexProductId = i
|
|
elif row == "Name":
|
|
rowIndexProductName = i
|
|
elif row == "Produktvariante":
|
|
rowIndexProductVariant = i
|
|
elif row == "Produktcharakteristik Zeile 1":
|
|
rowIndexProductCharacteristicLine1 = i
|
|
elif row == "Produktcharakteristik Zeile 2":
|
|
rowIndexProductCharacteristicLine2 = i
|
|
elif row == "Name trennen bei Position":
|
|
rowIndexNameSplitAtPosition = i
|
|
|
|
if rowIndexProductId == -1 or rowIndexProductId == -1 or rowIndexProductName == -1 or rowIndexProductVariant == -1 or rowIndexProductCharacteristicLine1 == -1 or rowIndexProductCharacteristicLine2 == -1:
|
|
sys.exit(
|
|
f"Failed to get row index. Please check if the row names are equal to the names of the google sheet table header of {WORKSHEET_PRODUCTS}")
|
|
|
|
# adding products to list
|
|
|
|
for row in data[2:]:
|
|
if row[0] == "":
|
|
continue
|
|
|
|
products.append({
|
|
"id": row[rowIndexProductId],
|
|
"name": add_underscore_at_position(row[1], int(row[rowIndexNameSplitAtPosition])),
|
|
"product_variant": row[rowIndexProductVariant].upper(),
|
|
"product_characteristic_line_1": row[rowIndexProductCharacteristicLine1],
|
|
"product_characteristic_line_2": row[rowIndexProductCharacteristicLine2],
|
|
})
|
|
|
|
products_dict = {"products": products}
|
|
|
|
# write the products list to a JSON file
|
|
with open('../../groupsData/google-sheet-products.json', 'w', encoding='utf-8') as json_file:
|
|
json.dump(products_dict, json_file, ensure_ascii=False, indent=2)
|
|
|
|
for file_path in ["../../groups/shx-product-label/index.json",
|
|
"../../groups/shx-all-in-one-order/index.json",
|
|
"../../groups/shx-intern-product-bag-label/index.json"]:
|
|
update_grouptask_products_list_options(file_path, products)
|
|
|
|
print("Finished. Do not forget to reload the group configuration by clicking on the 'Reload' button above the table on the right side.")
|
|
|
|
|
|
def google_sheets_filaments():
|
|
gc = gspread.service_account(filename=SERVICE_ACCOUNT_FILENAME)
|
|
|
|
spreadsheet = gc.open_by_key(GOOGLE_SPEADSHEET_KEY)
|
|
|
|
worksheet = spreadsheet.worksheet(WORKSHEET_FILAMENTS)
|
|
|
|
data = worksheet.get_all_values()
|
|
|
|
# auto get the row index numbers for the columns
|
|
|
|
filaments = []
|
|
|
|
rowIndexFilamentId = -1
|
|
rowIndexFilamentNameColor = -1
|
|
rowIndexFilamentMaterial = -1
|
|
rowIndexFilamentManufacturer = -1
|
|
|
|
for i in range(len(data[0])):
|
|
row = data[0][i]
|
|
|
|
if row == "id":
|
|
rowIndexFilamentId = i
|
|
elif row == "Name/Farbe":
|
|
rowIndexFilamentNameColor = i
|
|
elif row == "Material":
|
|
rowIndexFilamentMaterial = i
|
|
elif row == "Hersteller":
|
|
rowIndexFilamentManufacturer = i
|
|
|
|
if rowIndexFilamentId == -1 or rowIndexFilamentNameColor == -1 or rowIndexFilamentMaterial == -1 or rowIndexFilamentManufacturer == -1:
|
|
sys.exit(
|
|
f"Failed to get row index. Please check if the row names are equal to the names of the google sheet table header of {WORKSHEET_FILAMENTS}")
|
|
|
|
# adding filaments to list
|
|
|
|
for row in data[2:]:
|
|
if row[0] == "":
|
|
continue
|
|
|
|
filaments.append({
|
|
"id": row[rowIndexFilamentId],
|
|
"name_color": row[rowIndexFilamentNameColor],
|
|
"material": row[rowIndexFilamentMaterial],
|
|
"manufacturer": row[rowIndexFilamentManufacturer]
|
|
})
|
|
|
|
filaments_dict = {"filaments": filaments}
|
|
|
|
# write the filaments list to a JSON file
|
|
with open('../../groupsData/google-sheet-filaments.json', 'w', encoding='utf-8') as json_file:
|
|
json.dump(filaments_dict, json_file, ensure_ascii=False, indent=2)
|
|
|
|
update_grouptask_filaments_list_options(
|
|
"../../groups/shx-intern-filament-roll-label/index.json", filaments)
|
|
|
|
|
|
google_sheets_products()
|
|
google_sheets_filaments()
|