-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: added patch and item group fields
- Loading branch information
1 parent
7955f8b
commit 7897206
Showing
27 changed files
with
1,832 additions
and
969 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
__version__ = '0.0.1' | ||
|
60 changes: 60 additions & 0 deletions
60
webshop/patches/convert_to_website_item_in_item_card_group_template.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import json | ||
from typing import List, Union | ||
|
||
import frappe | ||
|
||
from webshop.webshop.doctype.website_item.website_item import make_website_item | ||
|
||
|
||
def execute(): | ||
""" | ||
Convert all Item links to Website Item link values in | ||
exisitng 'Item Card Group' Web Page Block data. | ||
""" | ||
frappe.reload_doc("webshop", "web_template", "item_card_group") | ||
|
||
blocks = frappe.db.get_all( | ||
"Web Page Block", | ||
filters={"web_template": "Item Card Group"}, | ||
fields=["parent", "web_template_values", "name"], | ||
) | ||
|
||
fields = generate_fields_to_edit() | ||
|
||
for block in blocks: | ||
web_template_value = json.loads(block.get("web_template_values")) | ||
|
||
for field in fields: | ||
item = web_template_value.get(field) | ||
if not item: | ||
continue | ||
|
||
if frappe.db.exists("Website Item", {"item_code": item}): | ||
website_item = frappe.db.get_value("Website Item", {"item_code": item}) | ||
else: | ||
website_item = make_new_website_item(item) | ||
|
||
if website_item: | ||
web_template_value[field] = website_item | ||
|
||
frappe.db.set_value( | ||
"Web Page Block", block.name, "web_template_values", json.dumps(web_template_value) | ||
) | ||
|
||
|
||
def generate_fields_to_edit() -> List: | ||
fields = [] | ||
for i in range(1, 13): | ||
fields.append(f"card_{i}_item") # fields like 'card_1_item', etc. | ||
|
||
return fields | ||
|
||
|
||
def make_new_website_item(item: str) -> Union[str, None]: | ||
try: | ||
doc = frappe.get_doc("Item", item) | ||
web_item = make_website_item(doc) # returns [website_item.name, item_name] | ||
return web_item[0] | ||
except Exception: | ||
doc.log_error("Website Item creation failed") | ||
return None |
94 changes: 94 additions & 0 deletions
94
webshop/patches/copy_custom_field_filters_to_website_item.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import frappe | ||
from frappe.custom.doctype.custom_field.custom_field import create_custom_field | ||
|
||
|
||
def execute(): | ||
"Add Field Filters, that are not standard fields in Website Item, as Custom Fields." | ||
|
||
def move_table_multiselect_data(docfield): | ||
"Copy child table data (Table Multiselect) from Item to Website Item for a docfield." | ||
table_multiselect_data = get_table_multiselect_data(docfield) | ||
field = docfield.fieldname | ||
|
||
for row in table_multiselect_data: | ||
# add copied multiselect data rows in Website Item | ||
web_item = frappe.db.get_value("Website Item", {"item_code": row.parent}) | ||
web_item_doc = frappe.get_doc("Website Item", web_item) | ||
|
||
child_doc = frappe.new_doc(docfield.options, parent_doc=web_item_doc, parentfield=field) | ||
|
||
for field in ["name", "creation", "modified", "idx"]: | ||
row[field] = None | ||
|
||
child_doc.update(row) | ||
|
||
child_doc.parenttype = "Website Item" | ||
child_doc.parent = web_item | ||
|
||
child_doc.insert() | ||
|
||
def get_table_multiselect_data(docfield): | ||
child_table = frappe.qb.DocType(docfield.options) | ||
item = frappe.qb.DocType("Item") | ||
|
||
table_multiselect_data = ( # query table data for field | ||
frappe.qb.from_(child_table) | ||
.join(item) | ||
.on(item.item_code == child_table.parent) | ||
.select(child_table.star) | ||
.where((child_table.parentfield == docfield.fieldname) & (item.published_in_website == 1)) | ||
).run(as_dict=True) | ||
|
||
return table_multiselect_data | ||
|
||
settings = frappe.get_doc("E Commerce Settings") | ||
|
||
if not (settings.enable_field_filters or settings.filter_fields): | ||
return | ||
|
||
item_meta = frappe.get_meta("Item") | ||
valid_item_fields = [ | ||
df.fieldname for df in item_meta.fields if df.fieldtype in ["Link", "Table MultiSelect"] | ||
] | ||
|
||
web_item_meta = frappe.get_meta("Website Item") | ||
valid_web_item_fields = [ | ||
df.fieldname for df in web_item_meta.fields if df.fieldtype in ["Link", "Table MultiSelect"] | ||
] | ||
|
||
for row in settings.filter_fields: | ||
# skip if illegal field | ||
if row.fieldname not in valid_item_fields: | ||
continue | ||
|
||
# if Item field is not in Website Item, add it as a custom field | ||
if row.fieldname not in valid_web_item_fields: | ||
df = item_meta.get_field(row.fieldname) | ||
create_custom_field( | ||
"Website Item", | ||
dict( | ||
owner="Administrator", | ||
fieldname=df.fieldname, | ||
label=df.label, | ||
fieldtype=df.fieldtype, | ||
options=df.options, | ||
description=df.description, | ||
read_only=df.read_only, | ||
no_copy=df.no_copy, | ||
insert_after="on_backorder", | ||
), | ||
) | ||
|
||
# map field values | ||
if df.fieldtype == "Table MultiSelect": | ||
move_table_multiselect_data(df) | ||
else: | ||
frappe.db.sql( # nosemgrep | ||
""" | ||
UPDATE `tabWebsite Item` wi, `tabItem` i | ||
SET wi.{0} = i.{0} | ||
WHERE wi.item_code = i.item_code | ||
""".format( | ||
row.fieldname | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import frappe | ||
|
||
from webshop.webshop.doctype.website_item.website_item import make_website_item | ||
|
||
|
||
def execute(): | ||
frappe.reload_doc("webshop", "doctype", "website_item") | ||
frappe.reload_doc("webshop", "doctype", "website_item_tabbed_section") | ||
frappe.reload_doc("webshop", "doctype", "website_offer") | ||
frappe.reload_doc("webshop", "doctype", "recommended_items") | ||
frappe.reload_doc("webshop", "doctype", "webshop_settings") | ||
frappe.reload_doc("stock", "doctype", "item") | ||
|
||
item_fields = [ | ||
"item_code", | ||
"item_name", | ||
"item_group", | ||
"stock_uom", | ||
"brand", | ||
"has_variants", | ||
"variant_of", | ||
"description", | ||
"weightage", | ||
] | ||
web_fields_to_map = [ | ||
"route", | ||
"slideshow", | ||
"website_image_alt", | ||
"website_warehouse", | ||
"web_long_description", | ||
"website_content", | ||
"website_image", | ||
"thumbnail", | ||
] | ||
|
||
# get all valid columns (fields) from Item master DB schema | ||
item_table_fields = frappe.db.sql("desc `tabItem`", as_dict=1) # nosemgrep | ||
item_table_fields = [d.get("Field") for d in item_table_fields] | ||
|
||
# prepare fields to query from Item, check if the web field exists in Item master | ||
web_query_fields = [] | ||
for web_field in web_fields_to_map: | ||
if web_field in item_table_fields: | ||
web_query_fields.append(web_field) | ||
item_fields.append(web_field) | ||
|
||
# check if the filter fields exist in Item master | ||
or_filters = {} | ||
for field in ["show_in_website", "show_variant_in_website"]: | ||
if field in item_table_fields: | ||
or_filters[field] = 1 | ||
|
||
if not web_query_fields or not or_filters: | ||
# web fields to map are not present in Item master schema | ||
# most likely a fresh installation that doesnt need this patch | ||
return | ||
|
||
items = frappe.db.get_all("Item", 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 | ||
) | ||
|
||
if count % 20 == 0: # commit after every 20 items | ||
frappe.db.commit() | ||
|
||
frappe.utils.update_progress_bar("Creating Website Items", count, total_count) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import frappe | ||
|
||
|
||
def execute(): | ||
homepage = frappe.get_doc("Homepage") | ||
|
||
for row in homepage.products: | ||
web_item = frappe.db.get_value("Website Item", {"item_code": row.item_code}, "name") | ||
if not web_item: | ||
continue | ||
|
||
row.item_code = web_item | ||
|
||
homepage.flags.ignore_mandatory = True | ||
homepage.save() |
Oops, something went wrong.