Skip to content

Commit

Permalink
fix: Move thumbnail updation to different patch
Browse files Browse the repository at this point in the history
- Thumbnail updation handled via different patch
- create_website_items will only have one purpose
- added progress bar to `create_website_items`
- code cleanup
  • Loading branch information
marination committed Oct 20, 2021
1 parent ac8014e commit 348a961
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 47 deletions.
3 changes: 2 additions & 1 deletion erpnext/patches.txt
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ erpnext.patches.v13_0.reset_clearance_date_for_intracompany_payment_entries
erpnext.patches.v13_0.custom_fields_for_taxjar_integration
erpnext.patches.v13_0.set_operation_time_based_on_operating_cost
erpnext.patches.v13_0.validate_options_for_data_field
erpnext.patches.v13_0.create_website_items #19-10-2021
erpnext.patches.v13_0.create_website_items #30-09-2021
erpnext.patches.v13_0.populate_e_commerce_settings
erpnext.patches.v13_0.make_homepage_products_website_items
erpnext.patches.v13_0.update_dates_in_tax_withholding_category
Expand All @@ -324,3 +324,4 @@ erpnext.patches.v13_0.set_status_in_maintenance_schedule_table
erpnext.patches.v13_0.add_default_interview_notification_templates
erpnext.patches.v13_0.trim_sales_invoice_custom_field_length
erpnext.patches.v13_0.requeue_failed_reposts
erpnext.patches.v13_0.fetch_thumbnail_in_website_items
71 changes: 25 additions & 46 deletions erpnext/patches/v13_0/create_website_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,51 +43,30 @@ def execute():
fields=item_fields,
or_filters=or_filters
)
total_count = len(items)

for count, item in enumerate(items, start=1):
if frappe.db.exists("Website Item", {"item_code": item.item_code}):
continue

# make new website item from item (publish item)
website_item = make_website_item(item, save=False)
website_item.ranking = item.get("weightage")

for field in web_fields_to_map:
website_item.update({field: item.get(field)})

website_item.save()

# move Website Item Group & Website Specification table to Website Item
for doctype in ("Website Item Group", "Item Website Specification"):
frappe.db.set_value(
doctype,
{"parenttype": "Item", "parent": item.item_code}, # filters
{"parenttype": "Website Item", "parent": website_item.name} # value dict
)

count = 0
for item in items:
web_item_exists = frappe.db.exists("Website Item", {"item_code": item.item_code})
thumbnail_column_exists = "thumbnail" in item_table_fields

if web_item_exists and thumbnail_column_exists:
# if website item already exists check for empty thumbnail
# if empty, fetch thumbnail from Item master
web_item_doc = frappe.db.get_values(
"Website Item",
filters={
"item_code": item.item_code
},
fieldname=["website_image", "thumbnail", "name"],
as_dict=True
)[0]

if web_item_doc.get("website_image") and not web_item_doc.get("thumbnail"):
thumbnail = frappe.db.get_value("Item", item.item_code, "thumbnail")
frappe.db.set_value("Website Item", web_item_doc.name, "thumbnail", thumbnail)
else:
# else make new website item from item (publish item)
website_item = make_website_item(item, save=False)
website_item.ranking = item.get("weightage")

for field in web_fields_to_map:
website_item.update({field: item.get(field)})

website_item.save()

# move Website Item Group & Website Specification table to Website Item
for doctype in ("Website Item Group", "Item Website Specification"):
web_item, item_code = website_item.name, item.item_code
frappe.db.sql(f"""
Update
`tab{doctype}`
set
parenttype = 'Website Item',
parent = '{web_item}'
where
parenttype = 'Item'
and parent = '{item_code}'
""")

count += 1
if count % 20 == 0: # commit after every 20 items
frappe.db.commit()
frappe.db.commit()

frappe.utils.update_progress_bar('Creating Website Items', count, total_count)
16 changes: 16 additions & 0 deletions erpnext/patches/v13_0/fetch_thumbnail_in_website_items.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import frappe


def execute():
if frappe.db.has_column("Item", "thumbnail"):
website_item = frappe.qb.DocType("Website Item").as_("wi")
item = frappe.qb.DocType("Item")

frappe.qb.update(website_item).inner_join(item).on(
website_item.item_code == item.item_code
).set(
website_item.thumbnail, item.thumbnail
).where(
website_item.website_image.notnull()
& website_item.thumbnail.isnull()
).run()

0 comments on commit 348a961

Please sign in to comment.