fix: Move thumbnail updation to different patch

- Thumbnail updation handled via different patch
- create_website_items will only have one purpose
- added progress bar to `create_website_items`
- code cleanup
This commit is contained in:
marination 2021-10-20 14:08:14 +05:30
parent 871e9ca6af
commit dc42c45ebb
3 changed files with 37 additions and 41 deletions

View File

@ -290,7 +290,7 @@ erpnext.patches.v13_0.validate_options_for_data_field
erpnext.patches.v13_0.create_gst_payment_entry_fields
erpnext.patches.v14_0.delete_shopify_doctypes
erpnext.patches.v13_0.fix_invoice_statuses
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.replace_supplier_item_group_with_party_specific_item
@ -318,3 +318,4 @@ erpnext.patches.v13_0.update_category_in_ltds_certificate
erpnext.patches.v13_0.create_pan_field_for_india #2
erpnext.patches.v14_0.delete_hub_doctypes
erpnext.patches.v13_0.create_ksa_vat_custom_fields
erpnext.patches.v13_0.fetch_thumbnail_in_website_items

View File

@ -41,51 +41,30 @@ def execute():
fields=item_fields,
or_filters=or_filters
)
total_count = len(items)
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
for count, item in enumerate(items, start=1):
if frappe.db.exists("Website Item", {"item_code": item.item_code}):
continue
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]
# make new website item from item (publish item)
website_item = make_website_item(item, save=False)
website_item.ranking = item.get("weightage")
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)})
for field in web_fields_to_map:
website_item.update({field: item.get(field)})
website_item.save()
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
)
# 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.utils.update_progress_bar('Creating Website Items', count, total_count)

View File

@ -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()