brotherton-erpnext/erpnext/patches/v5_0/update_item_description_and_image.py

54 lines
1.9 KiB
Python
Raw Normal View History

# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
2015-02-18 15:18:26 +00:00
# License: GNU General Public License v3. See license.txt
import frappe
from frappe.website.utils import find_first_image
from frappe.utils import cstr
import re
def execute():
2015-03-25 09:21:05 +00:00
item_details = frappe._dict()
for d in frappe.db.sql("select name, description_html, description from `tabItem`", as_dict=1):
description = cstr(d.description_html).strip() or cstr(d.description).strip()
image_url, new_desc = extract_image_and_description(description)
2015-03-25 09:21:05 +00:00
item_details.setdefault(d.name, frappe._dict({
"old_description": description,
"new_description": new_desc,
"image_url": image_url
}))
2015-02-18 15:18:26 +00:00
dt_list= ["Purchase Order Item","Supplier Quotation Item", "BOM", "BOM Explosion Item" , \
"BOM Item", "Opportunity Item" , "Quotation Item" , "Sales Order Item" , "Delivery Note Item" , \
"Material Request Item" , "Purchase Receipt Item" , "Stock Entry Detail"]
for dt in dt_list:
frappe.reload_doctype(dt)
records = frappe.db.sql("""select name, `{0}` as item_code, description from `tab{1}`
2015-03-25 09:21:05 +00:00
where description is not null and image is null and description like '%%<img%%'"""
.format("item" if dt=="BOM" else "item_code", dt), as_dict=1)
2015-03-25 09:21:05 +00:00
count = 1
for d in records:
2015-05-01 10:20:52 +00:00
if d.item_code and item_details.get(d.item_code) \
and cstr(d.description) == item_details.get(d.item_code).old_description:
2015-03-25 09:21:05 +00:00
image_url = item_details.get(d.item_code).image_url
desc = item_details.get(d.item_code).new_description
else:
image_url, desc = extract_image_and_description(cstr(d.description))
2015-02-18 15:18:26 +00:00
2015-03-25 09:21:05 +00:00
if image_url:
frappe.db.sql("""update `tab{0}` set description = %s, image = %s
where name = %s """.format(dt), (desc, image_url, d.name))
2015-03-25 09:21:05 +00:00
count += 1
if count % 500 == 0:
frappe.db.commit()
def extract_image_and_description(data):
image_url = find_first_image(data)
desc = re.sub("\<img[^>]+\>", "", data)
return image_url, desc