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 13, 2023
1 parent 7955f8b commit 7897206
Show file tree
Hide file tree
Showing 27 changed files with 1,832 additions and 969 deletions.
27 changes: 21 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ jobs:
name: Server

services:
mariadb:
mysql:
image: mariadb:10.6
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_ROOT_PASSWORD: 'root'
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval=5s --health-timeout=2s --health-retries=3
options: --health-cmd="mariadb-admin ping" --health-interval=5s --health-timeout=2s --health-retries=3

steps:
- name: Clone
Expand All @@ -39,9 +39,12 @@ jobs:
- name: Setup Node
uses: actions/setup-node@v2
with:
node-version: 14
node-version: 18
check-latest: true

- name: Add to Hosts
run: echo "127.0.0.1 test_site" | sudo tee -a /etc/hosts

- name: Cache pip
uses: actions/cache@v2
with:
Expand All @@ -51,6 +54,18 @@ jobs:
${{ runner.os }}-pip-
${{ runner.os }}-
- name: Cache node modules
uses: actions/cache@v2
env:
cache-name: cache-node-modules
with:
path: ~/.npm
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: 'echo "::set-output name=dir::$(yarn cache dir)"'
Expand All @@ -67,8 +82,8 @@ jobs:
run: |
pip install frappe-bench
bench init --skip-redis-config-generation --skip-assets --python "$(which python)" ~/frappe-bench
mysql --host 127.0.0.1 --port 3306 -u root -proot -e "SET GLOBAL character_set_server = 'utf8mb4'"
mysql --host 127.0.0.1 --port 3306 -u root -proot -e "SET GLOBAL collation_server = 'utf8mb4_unicode_ci'"
mariadb --host 127.0.0.1 --port 3306 -u root -proot -e "SET GLOBAL character_set_server = 'utf8mb4'"
mariadb --host 127.0.0.1 --port 3306 -u root -proot -e "SET GLOBAL collation_server = 'utf8mb4_unicode_ci'"
- name: Get Deps
working-directory: /home/runner/frappe-bench
Expand Down
4 changes: 3 additions & 1 deletion webshop/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
]
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",
"Item": "webshop.webshop.doctype.override_doctype.item.WebshopItem",
}

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

0 comments on commit 7897206

Please sign in to comment.