#5 - cleanup code

This commit is contained in:
beren
2025-05-08 12:09:30 +02:00
parent 832205450f
commit 4668b2704f
13 changed files with 79 additions and 6843 deletions

View File

@ -425,6 +425,7 @@ class MediaManager(OdsReader):
print(f"Erreur suppression {img_id} :", response.status_code, response.text)
def delete_all_images(self):
print('iciiiii')
images = self.get_all_images()
for img in images:
img_id = img['id']
@ -489,7 +490,6 @@ class CategoryManager(OdsReader):
"slug":slug
}
if self.find_id_by_slug(slug):
#self.error_log.append(f"Catégorie contenant comme slug '{slug}' existe déjà")
logger.debug(f"Catégorie contenant comme slug '{slug}' existe déjà")
else:
try:
@ -520,8 +520,6 @@ class CategoryManager(OdsReader):
return cat['id']
def find_media_id_by_slug(self, media_slug):
#print(f"media_slug = {media_slug}")
#pprint.pprint(self.medias.items())
for id, slug in self.medias.items():
if media_slug == slug:
return id
@ -538,10 +536,8 @@ class CategoryManager(OdsReader):
def update_data_categories(self, search_value=None):
if search_value:
print("la")
json_data = self.get_category_line_by_value(search_value)
else:
print("oula")
json_data = self.get_all_category_lines()
for category in json_data:
self.create_category(category['Nom'], category['Description'], category['Slug'])
@ -660,24 +656,23 @@ class ProductManager(OdsReader):
else:
print(f"error")
def create_product(self, product_data):
if self.find_id_by_slug(product_data['slug']):
#self.error_log.append(f"Produit contenant comme slug '{product_data['slug']}' existe déjà")
logger.debug(f"Produit contenant comme slug '{product_data['slug']}' existe déjà")
else:
try:
response = self.wcapi.post("products/", product_data)
if response.status_code == 201:
# Le produit a été créé avec succès
logger.info(f"Produit créé avec succès. ID: {response.json()['id']}")
def create_product(self, product_data):
try:
response = self.wcapi.post("products/", product_data)
if response.status_code == 201:
# Le produit a été créé avec succès
logger.info(f"Produit créé avec succès. ID: {response.json()['id']}")
else:
if self.find_id_by_slug(product_data['slug']):
pass
else:
# Le produit n'a pas été créé, mais il y a une réponse avec un code d'erreur
logger.error(f"Erreur lors de la création du produit. Code: {response.status_code}, Message: {response.text}")
except Exception as e:
logger.error(f"Erreur inattendue lors de l'envoi du produit à WooCommerce: {e}")
except Exception as e:
logger.error(f"Erreur inattendue lors de l'envoi du produit à WooCommerce: {e}")
def update_data_product(self, product_data, categories, medias):
json_data = self.get_all_product_lines()
def update_data_product(self, product_data, categories, medias, json_data):
#json_data = self.get_all_product_lines()
for product in json_data:
self.create_product(product_data)
product_id = self.find_id_by_slug(product_data['slug'])
@ -1247,8 +1242,18 @@ class WooCommerceManager(OdsReader):
for product in product_data:
self.update_product_tab_by_id(product['id'])
def create_or_update_product(self, product_data, attributes, tabs, categories, medias, json_data):
try:
self.product_manager.update_data_product(product_data=product_data, categories=categories, medias=medias, json_data=json_data)
self.update_product_attribute(attributes=attributes, product_data=product_data)
product_id = self.product_manager.find_id_by_slug(product_data['slug'])
self.update_product_variations(product_data)
self.tab_manager.create_or_update_for_product(product_id=product_id, tabs=tabs)
except Exception as e:
print(f"Erreur lors de la mise à jour du produit: {e}")
logger.exception(f"Erreur lors de la mise à jour du produit: {e}")
def create_or_update_product(self, product_data, attributes, tabs, categories, medias):
"""def create_or_update_product(self, product_data, attributes, tabs, categories, medias):
try:
self.product_manager.update_data_product(product_data=product_data, categories=categories, medias=medias)
self.update_product_attribute(attributes=attributes, product_data=product_data)
@ -1257,7 +1262,7 @@ class WooCommerceManager(OdsReader):
self.tab_manager.create_or_update_for_product(product_id=product_id, tabs=tabs)
except Exception as e:
print(f"Erreur lors de la mise à jour du produit: {e}")
logger.exception(f"Erreur lors de la mise à jour du produit: {e}")
logger.exception(f"Erreur lors de la mise à jour du produit: {e}")"""
def get_product_lines(self, search_value=None):
if search_value:
@ -1314,7 +1319,7 @@ class WooCommerceManager(OdsReader):
# ... associated medias
medias = self.get_list_media_id_for_product(product_line['Media Slugs'])
# create or update product
self.create_or_update_product(product_data=product_data, attributes=attributes, tabs=tabs, categories=categories, medias=medias)
@ -1325,50 +1330,56 @@ class WooCommerceManager(OdsReader):
self.product_manager.medias = medias
# read provided file
#reader = OdsReader(filename)
#json_data = self.fetch_all_product_rows(range_start, range_end)
for product_line in self.fetch_all_product_rows(range_start, range_end):
# standard product data
product_data = {
'name' : product_line['Nom'],
'price': product_line['Prix'],
'regular_price': product_line['Prix'],
'stock_quantity': product_line['Stock'],
'manage_stock':True,
'weight':str(product_line['Poids']),
'sku':str(product_line['Numéro de référence']),
'description': product_line['Description'],
'short_description': product_line['Courte Description'],
'slug':product_line['Slug']
}
if product_line['Type'] == "parfums":
product_data['type'] = "variable"
if self.product_manager.find_id_by_slug(product_line['Slug']):
logger.debug(f"Produit contenant comme slug '{product_line['Slug']}' existe déjà")
else:
product_data['type'] = "simple"
# standard product data
product_data = {
'name' : product_line['Nom'],
'price': product_line['Prix'],
'regular_price': product_line['Prix'],
'stock_quantity': product_line['Stock'],
'manage_stock':True,
'weight':str(product_line['Poids']),
'sku':str(product_line['Numéro de référence']),
'description': product_line['Description'],
'short_description': product_line['Courte Description'],
'slug':product_line['Slug']
}
if product_line['Type'] == "parfums":
product_data['type'] = "variable"
else:
product_data['type'] = "simple"
attributes = {
"Temps de combustion" : product_line['Temps de combustion'],
"Type de cire" : product_line['Type de cire'],
"Mèche" : product_line['Mèche'],
"Fabrication" : product_line['Fabrication'],
"Composition" : product_line['Composition'],
"Ingrédients et engagements" : product_line['Ingrédients et engagements'],
"Parfums" : product_line['Parfums']
}
tabs ={
#"Description" : product_line["Description"],
"Conseils d'utilisation" : product_line["Conseils dutilisation"],
"Précautions articles" : product_line["Précautions articles"],
#"Allergènes" : product_line["Allergènes"]
}
# ... associated categories
categories = self.get_list_category_for_product(product_line['Catégories'])
# ... associated medias
medias = self.get_list_media_id_for_product(product_line['Media Slugs'])
# create or update product
self.create_or_update_product(product_data=product_data, attributes=attributes, tabs=tabs, categories=categories, medias=medias)
attributes = {
"Temps de combustion" : product_line['Temps de combustion'],
"Type de cire" : product_line['Type de cire'],
"Mèche" : product_line['Mèche'],
"Fabrication" : product_line['Fabrication'],
"Composition" : product_line['Composition'],
"Ingrédients et engagements" : product_line['Ingrédients et engagements'],
"Parfums" : product_line['Parfums']
}
tabs ={
#"Description" : product_line["Description"],
"Conseils d'utilisation" : product_line["Conseils dutilisation"],
"Précautions articles" : product_line["Précautions articles"],
#"Allergènes" : product_line["Allergènes"]
}
# ... associated categories
categories = self.get_list_category_for_product(product_line['Catégories'])
# ... associated medias
print(f"product_line['Media Slugs'] = {product_line['Media Slugs']}")
medias = self.get_list_media_id_for_product(product_line['Media Slugs'])
# create or update product
self.create_or_update_product(product_data=product_data, attributes=attributes, tabs=tabs, categories=categories, medias=medias, json_data=product_line)
"""def put_social_data(self):
response = requests.post(url,
@ -1517,14 +1528,15 @@ if __name__ == "__main__":
#seo_manager = SeoManager(ath=ath, filename_ods=FILENAME_ODS)
#pages = seo_manager.get_all_pages()
#seo_manager.update_seo_page()
#media_manager = MediaManager(ath=ath)
media_manager = MediaManager(ath=ath, filename_ods=FILENAME_ODS)
#media_manager.delete_media_by_slug('fondtzel-perlimpinpin')
#media_manager.upload_media()
#media_manager.delete_all_images()
#media_manager.assign_image_logo()
#category_manager = CategoryManager(wcapi=wcapi,ath=ath)
#category_manager.delete_all_category()
order_manager = OrderManager(wcapi=wcapi,ath=ath)
order_manager.delete_all_orders()
#order_manager = OrderManager(wcapi=wcapi,ath=ath)
#order_manager.delete_all_orders()
#product_manager = ProductManager(wcapi=wcapi,ath=ath)
#product_manager.delete_all_product()
#medias=media_manager.get_all_as_slug_dict()