Skip to content

Commit

Permalink
fix: added patch and item group fields
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitwaghchaure committed Oct 12, 2023
1 parent 7955f8b commit 926cf0d
Show file tree
Hide file tree
Showing 20 changed files with 853 additions and 155 deletions.
3 changes: 2 additions & 1 deletion webshop/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
]
my_account_context = "webshop.webshop.shopping_cart.utils.update_my_account_context"

website_generators = ["Website Item"]
website_generators = ["Website Item", "Item Group"]

override_doctype_class = {
"Payment Request": "webshop.webshop.override_doctype.payment_request.PaymentRequest",
"Item Group": "webshop.webshop.doctype.override_doctype.item_group.WebshopItemGroup",
}

doctype_js = {
Expand Down
3 changes: 3 additions & 0 deletions webshop/patches/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

__version__ = '0.0.1'

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 webshop/patches/copy_custom_field_filters_to_website_item.py
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
)
)
85 changes: 85 additions & 0 deletions webshop/patches/create_website_items.py
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)
11 changes: 11 additions & 0 deletions webshop/patches/fetch_thumbnail_in_website_items.py
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()
15 changes: 15 additions & 0 deletions webshop/patches/make_homepage_products_website_items.py
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()
68 changes: 68 additions & 0 deletions webshop/patches/populate_e_commerce_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import frappe
from frappe.utils import cint


def execute():
frappe.reload_doc("webshop", "doctype", "webshop_settings")
frappe.reload_doc("portal", "doctype", "website_filter_field")
frappe.reload_doc("portal", "doctype", "website_attribute")

products_settings_fields = [
"hide_variants",
"products_per_page",
"enable_attribute_filters",
"enable_field_filters",
]

shopping_cart_settings_fields = [
"enabled",
"show_attachments",
"show_price",
"show_stock_availability",
"enable_variants",
"show_contact_us_button",
"show_quantity_in_website",
"show_apply_coupon_code_in_website",
"allow_items_not_in_stock",
"company",
"price_list",
"default_customer_group",
"quotation_series",
"enable_checkout",
"payment_success_url",
"payment_gateway_account",
"save_quotations_as_draft",
]

settings = frappe.get_doc("E Commerce Settings")

def map_into_e_commerce_settings(doctype, fields):
singles = frappe.qb.DocType("Singles")
query = (
frappe.qb.from_(singles)
.select(singles["field"], singles.value)
.where((singles.doctype == doctype) & (singles["field"].isin(fields)))
)
data = query.run(as_dict=True)

# {'enable_attribute_filters': '1', ...}
mapper = {row.field: row.value for row in data}

for key, value in mapper.items():
value = cint(value) if (value and value.isdigit()) else value
settings.update({key: value})

settings.save()

# shift data to E Commerce Settings
map_into_e_commerce_settings("Products Settings", products_settings_fields)
map_into_e_commerce_settings("Shopping Cart Settings", shopping_cart_settings_fields)

# move filters and attributes tables to E Commerce Settings from Products Settings
for doctype in ("Website Filter Field", "Website Attribute"):
frappe.db.set_value(
doctype,
{"parent": "Products Settings"},
{"parenttype": "E Commerce Settings", "parent": "E Commerce Settings"},
update_modified=False,
)
9 changes: 9 additions & 0 deletions webshop/patches/shopping_cart_to_ecommerce.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import click
import frappe


def execute():

frappe.delete_doc("DocType", "Shopping Cart Settings", ignore_missing=True)
frappe.delete_doc("DocType", "Products Settings", ignore_missing=True)
frappe.delete_doc("DocType", "Supplier Item Group", ignore_missing=True)
Loading

0 comments on commit 926cf0d

Please sign in to comment.