Skip to content

Instantly share code, notes, and snippets.

@ianrussel
Created July 15, 2024 12:18
Show Gist options
  • Save ianrussel/d3fd392f4275f35fec5fff71de01aee7 to your computer and use it in GitHub Desktop.
Save ianrussel/d3fd392f4275f35fec5fff71de01aee7 to your computer and use it in GitHub Desktop.
product meta upload
def run(d, meta_keys_list: list, alternative_filter_keys: list = None):
db_connector.connect()
try:
# Build filter conditions based on provided keys
conditions = False
if alternative_filter_keys:
conditions = [getattr(Product, key) == d[key] for key in alternative_filter_keys if key in d]
if conditions:
product_row = db_connector.session.query(Product).filter(and_(*conditions)).first()
else:
product_row = db_connector.session.query(Product).filter(Product.id == int(d['id'])).first()
else:
product_row = db_connector.session.query(Product).filter(Product.id == int(d['id'])).first()
combined_list = meta_keys_list + ['id', 'URL']
if product_row:
try:
# print(f" product row {product_row.id}")
# Dynamically update attributes except 'id'
for key, value in d.items():
if key == 'model_number':
value = str(value)
if key == 'asin':
value = str(value)
# print(f" the key {key} the valyue {value}")
# print(f" key {key} value {value}")
if key not in combined_list and hasattr(product_row, key):
setattr(product_row, key, value)
# Special case for 'brand' and product_slug
if key == 'brand':
product_row.brand_filter = value
if key == 'product_slug_new':
product_row.product_slug = value
if key == 'colour':
product_row.color = value
if key in allowed_keys:
list_value = []
if ',' in value:
list_value = [item.strip() for item in value.split(',')]
else:
list_value = [value]
for list_val in list_value:
identifiers_data = {
"product_id": product_row.id,
"label": key,
"value": list_val
}
update_create_identifiers(identifiers_data)
db_connector.session.commit()
except Exception as e:
print(f" eeeee {e} key {key} value {value} dd {d}")
db_connector.session.rollback()
meta_keys_to_consider = meta_keys_list
for meta_key in meta_keys_to_consider:
if meta_key in d and d[meta_key]:
meta_value = d[meta_key]
# Only proceed if meta_value is not empty or None
if meta_value:
if meta_key == 'main_description':
""" change to raw_description, add some rules"""
meta_key = 'raw_description'
paragraphs = meta_value.split("\n")
paragraphs_with_tags = [f"<p>{paragraph}</p>" for paragraph in paragraphs if paragraph]
meta_value = "\n\n".join(paragraphs_with_tags)
meta_value = meta_value.replace("--", "-")
meta_value = meta_value.replace("—", "-")
""""""
try:
# print("success")
# Retrieve the Meta record with the given product_id and meta_key
if conditions:
id_to_query = product_row.id
else:
id_to_query = d['id']
existing_meta = db_connector.session.query(Meta).filter(
Meta.product_id == id_to_query, Meta.meta_key == meta_key
).first()
# If the Meta record exists, update its meta_value attribute
if existing_meta:
existing_meta.meta_value = meta_value
else:
# Create a new instance and add it to the session
new_meta = Meta(product_id=id_to_query, meta_key=meta_key, meta_value=meta_value)
db_connector.session.add(new_meta)
# Commit the changes to the database
db_connector.session.commit()
except Exception as e:
# Handle any exceptions that occur during the database operation
print(f"Database operation failed: {e}")
db_connector.session.rollback()
""" to do update the parent and parent draft tables"""
update_parent_draft(d)
except Exception as e:
print(f" exception occured while upating product meta {e} on {d}")
# db_connector.session.rollback()
traceback.print_exc() # Print the full traceback
db_connector.session.rollback()
finally:
db_connector.session.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment