Skip to content

Commit

Permalink
Merge pull request frappe#28005 from marination/patch-thumbnail-perf
Browse files Browse the repository at this point in the history
fix: Fetch thumbnail from Item master instead of regenerating
  • Loading branch information
marination committed Oct 20, 2021
2 parents 44cc600 + baf9c18 commit da95fc6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 33 deletions.
2 changes: 1 addition & 1 deletion erpnext/e_commerce/doctype/website_item/website_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def validate_website_image(self):

def make_thumbnail(self):
"""Make a thumbnail of `website_image`"""
if frappe.flags.in_import:
if frappe.flags.in_import or frappe.flags.in_migrate:
return

import requests.exceptions
Expand Down
1 change: 1 addition & 0 deletions erpnext/patches.txt
Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,4 @@ erpnext.patches.v13_0.add_default_interview_notification_templates
erpnext.patches.v13_0.trim_sales_invoice_custom_field_length
erpnext.patches.v13_0.enable_scheduler_job_for_item_reposting
erpnext.patches.v13_0.requeue_failed_reposts
erpnext.patches.v13_0.fetch_thumbnail_in_website_items
58 changes: 26 additions & 32 deletions erpnext/patches/v13_0/create_website_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ def execute():
item_fields = ["item_code", "item_name", "item_group", "stock_uom", "brand", "image",
"has_variants", "variant_of", "description", "weightage"]
web_fields_to_map = ["route", "slideshow", "website_image_alt",
"website_warehouse", "web_long_description", "website_content"]
"website_warehouse", "web_long_description", "website_content", "thumbnail"]

# get all valid columns (fields) from Item master DB schema
item_table_fields = frappe.db.sql("desc `tabItem`", as_dict=1)
item_table_fields = [d.get('Field') for d in item_table_fields]

Expand All @@ -42,37 +43,30 @@ def execute():
fields=item_fields,
or_filters=or_filters
)
total_count = len(items)

count = 0
for item in items:
for count, item in enumerate(items, start=1):
if frappe.db.exists("Website Item", {"item_code": item.item_code}):
# if website item already exists check for empty thumbnail
web_item_doc = frappe.get_doc("Website Item", {"item_code": item.item_code})
if web_item_doc.website_image and not web_item_doc.thumbnail:
web_item_doc.make_thumbnail()
web_item_doc.save()
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
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
)

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 da95fc6

Please sign in to comment.