[fix] patch and rename Variant Attribute to Item Variant Attribute

This commit is contained in:
Anand Doshi 2015-08-18 17:30:44 +05:30
parent b6c5b21dd0
commit afbe39a559
8 changed files with 2152 additions and 2144 deletions

View File

@ -8,12 +8,11 @@ import MySQLdb
def execute(): def execute():
""" """
Structure History: Structure History:
1. Item and Item Attribute 1. Item and Item Variant
2. Item, Variant Attribute, Manage Variants and Manage Variant Items 2. Item, Variant Attribute, Manage Variants and Manage Variant Items
3. Item, Variant Attribute (latest) 3. Item, Item Variant Attribute, Item Attribute and Item Attribute Type (latest)
""" """
frappe.db.reload_doctype("Item") rename_and_reload_doctypes()
frappe.db.reload_doctype("Variant Attribute")
variant_templates = frappe.get_all("Item", filters={"has_variants": 1}, limit_page_length=1) variant_templates = frappe.get_all("Item", filters={"has_variants": 1}, limit_page_length=1)
if not variant_templates: if not variant_templates:
@ -21,7 +20,7 @@ def execute():
# so no point in running the patch # so no point in running the patch
return return
variant_attributes = frappe.get_all("Variant Attribute", fields=["*"], limit_page_length=1) variant_attributes = frappe.get_all("Item Variant Attribute", fields=["*"], limit_page_length=1)
if variant_attributes: if variant_attributes:
# manage variant patch is already applied # manage variant patch is already applied
@ -35,9 +34,18 @@ def execute():
except MySQLdb.ProgrammingError: except MySQLdb.ProgrammingError:
print "`tabItem Variant` not found" print "`tabItem Variant` not found"
def rename_and_reload_doctypes():
if "tabVariant Attribute" in frappe.db.get_tables():
frappe.rename_doc("DocType", "Variant Attribute", "Item Variant Attribute")
frappe.reload_doctype("Item")
frappe.reload_doctype("Item Variant Attribute")
frappe.reload_doctype("Item Attribute Value")
frappe.reload_doctype("Item Attribute")
def migrate_manage_variants(): def migrate_manage_variants():
item_attribute = {} item_attribute = {}
for d in frappe.db.sql("""select DISTINCT va.attribute, i.variant_of from `tabVariant Attribute` va, `tabItem` i \ for d in frappe.db.sql("""select DISTINCT va.attribute, i.variant_of from `tabItem Variant Attribute` va, `tabItem` i \
where va.parent = i.name""", as_dict=1): where va.parent = i.name""", as_dict=1):
item_attribute.setdefault(d.variant_of, []).append({"attribute": d.attribute}) item_attribute.setdefault(d.variant_of, []).append({"attribute": d.attribute})
@ -54,7 +62,7 @@ def migrate_item_variants():
for item in frappe.get_all("Item", filters={"has_variants": 1}): for item in frappe.get_all("Item", filters={"has_variants": 1}):
all_variants = frappe.get_all("Item", filters={"variant_of": item.name}, fields=["name", "description"]) all_variants = frappe.get_all("Item", filters={"variant_of": item.name}, fields=["name", "description"])
item_attributes = frappe.db.sql("""select distinct item_attribute, item_attribute_value item_attributes = frappe.db.sql("""select distinct item_attribute, item_attribute_value
from `tabItem Attribute` where parent=%s""", item.name) from `tabItem Variant` where parent=%s""", item.name)
attribute_value_options = {} attribute_value_options = {}
for attribute, value in item_attributes: for attribute, value in item_attributes:
@ -77,10 +85,10 @@ def migrate_item_variants():
save_attributes_in_variant(variant, combination) save_attributes_in_variant(variant, combination)
break break
frappe.delete_doc("DocType", "Item Attribute") frappe.delete_doc("DocType", "Item Variant")
def save_attributes_in_template(item, attribute_value_options): def save_attributes_in_template(item, attribute_value_options):
# store attribute in Variant Attribute table for template # store attribute in Item Variant Attribute table for template
template = frappe.get_doc("Item", item) template = frappe.get_doc("Item", item)
template.set("attributes", [{"attribute": attribute} for attribute in attribute_value_options.keys()]) template.set("attributes", [{"attribute": attribute} for attribute in attribute_value_options.keys()])
template.save() template.save()

View File

@ -828,7 +828,7 @@
"in_list_view": 0, "in_list_view": 0,
"label": "Attributes", "label": "Attributes",
"no_copy": 1, "no_copy": 1,
"options": "Variant Attribute", "options": "Item Variant Attribute",
"permlevel": 0, "permlevel": 0,
"precision": "", "precision": "",
"print_hide": 0, "print_hide": 0,
@ -1929,7 +1929,7 @@
"issingle": 0, "issingle": 0,
"istable": 0, "istable": 0,
"max_attachments": 1, "max_attachments": 1,
"modified": "2015-08-12 10:28:54.242793", "modified": "2015-08-18 10:28:54.242793",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "Stock", "module": "Stock",
"name": "Item", "name": "Item",

View File

@ -500,7 +500,7 @@ def get_variant(item, args):
for d in args: for d in args:
if d in numeric_attributes: if d in numeric_attributes:
values = frappe.db.sql("""select from_range, to_range, increment from `tabVariant Attribute` \ values = frappe.db.sql("""select from_range, to_range, increment from `tabItem Variant Attribute` \
where parent = %s and attribute = %s""", (item, d), as_dict=1)[0] where parent = %s and attribute = %s""", (item, d), as_dict=1)[0]
if (not values.from_range < cint(args[d]) < values.to_range) or ((cint(args[d]) - values.from_range) % values.increment != 0): if (not values.from_range < cint(args[d]) < values.to_range) or ((cint(args[d]) - values.from_range) % values.increment != 0):
@ -518,11 +518,11 @@ def get_variant(item, args):
conds+= " and " conds+= " and "
attributes+= ", " attributes+= ", "
conds += """ exists(select iv.name from `tabVariant Attribute` iv where iv.parent = i.name and conds += """ exists(select iv.name from `tabItem Variant Attribute` iv where iv.parent = i.name and
iv.attribute= "{0}" and iv.attribute_value= "{1}")""".format(d, args[d]) iv.attribute= "{0}" and iv.attribute_value= "{1}")""".format(d, args[d])
attributes += "'{0}'".format(d) attributes += "'{0}'".format(d)
conds += """and not exists (select iv.name from `tabVariant Attribute` iv where iv.parent = i.name and conds += """and not exists (select iv.name from `tabItem Variant Attribute` iv where iv.parent = i.name and
iv.attribute not in ({0}))""".format(attributes) iv.attribute not in ({0}))""".format(attributes)
variant= frappe.db.sql("""select i.name from tabItem i where {0}""".format(conds)) variant= frappe.db.sql("""select i.name from tabItem i where {0}""".format(conds))

View File

@ -39,7 +39,7 @@ class ItemAttribute(Document):
for d in self.item_attribute_values: for d in self.item_attribute_values:
attribute_values.append(d.attribute_value) attribute_values.append(d.attribute_value)
variant_attributes = frappe.db.sql("select DISTINCT attribute_value from `tabVariant Attribute` where attribute=%s", self.name) variant_attributes = frappe.db.sql("select DISTINCT attribute_value from `tabItem Variant Attribute` where attribute=%s", self.name)
if variant_attributes: if variant_attributes:
for d in variant_attributes: for d in variant_attributes:
if d[0] and d[0] not in attribute_values: if d[0] and d[0] not in attribute_values:

View File

@ -0,0 +1,217 @@
{
"allow_copy": 0,
"allow_import": 1,
"allow_rename": 0,
"autoname": "",
"creation": "2015-05-19 05:12:30.344797",
"custom": 0,
"docstatus": 0,
"doctype": "DocType",
"document_type": "Other",
"fields": [
{
"allow_on_submit": 0,
"fieldname": "attribute",
"fieldtype": "Link",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 1,
"label": "Attribute",
"no_copy": 0,
"options": "Item Attribute",
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 1,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"fieldname": "column_break_2",
"fieldtype": "Column Break",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"depends_on": "",
"fieldname": "attribute_value",
"fieldtype": "Data",
"hidden": 1,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 1,
"label": "Attribute Value",
"no_copy": 0,
"options": "",
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"depends_on": "has_variants",
"fieldname": "numeric_values",
"fieldtype": "Check",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"label": "Numeric Values",
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"depends_on": "numeric_values",
"fieldname": "section_break_4",
"fieldtype": "Section Break",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"depends_on": "",
"fieldname": "from_range",
"fieldtype": "Float",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"label": "From Range",
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"depends_on": "",
"fieldname": "increment",
"fieldtype": "Float",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"label": "Increment",
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"fieldname": "column_break_8",
"fieldtype": "Column Break",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"depends_on": "",
"fieldname": "to_range",
"fieldtype": "Float",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"label": "To Range",
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
}
],
"hide_heading": 0,
"hide_toolbar": 0,
"icon": "",
"in_create": 0,
"in_dialog": 0,
"is_submittable": 0,
"issingle": 0,
"istable": 1,
"modified": "2015-08-18 02:47:07.959104",
"modified_by": "Administrator",
"module": "Stock",
"name": "Item Variant Attribute",
"name_case": "",
"owner": "Administrator",
"permissions": [],
"read_only": 0,
"read_only_onload": 0,
"sort_field": "modified",
"sort_order": "DESC"
}

View File

@ -6,5 +6,5 @@ from __future__ import unicode_literals
import frappe import frappe
from frappe.model.document import Document from frappe.model.document import Document
class VariantAttribute(Document): class ItemVariantAttribute(Document):
pass pass

View File

@ -1,217 +0,0 @@
{
"allow_copy": 0,
"allow_import": 1,
"allow_rename": 0,
"autoname": "",
"creation": "2015-05-19 05:12:30.344797",
"custom": 0,
"docstatus": 0,
"doctype": "DocType",
"document_type": "Other",
"fields": [
{
"allow_on_submit": 0,
"fieldname": "attribute",
"fieldtype": "Link",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 1,
"label": "Attribute",
"no_copy": 0,
"options": "Item Attribute",
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 1,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"fieldname": "column_break_2",
"fieldtype": "Column Break",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"depends_on": "",
"fieldname": "attribute_value",
"fieldtype": "Data",
"hidden": 1,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 1,
"label": "Attribute Value",
"no_copy": 0,
"options": "",
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"depends_on": "has_variants",
"fieldname": "numeric_values",
"fieldtype": "Check",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"label": "Numeric Values",
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"depends_on": "numeric_values",
"fieldname": "section_break_4",
"fieldtype": "Section Break",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"depends_on": "",
"fieldname": "from_range",
"fieldtype": "Float",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"label": "From Range",
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"depends_on": "",
"fieldname": "increment",
"fieldtype": "Float",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"label": "Increment",
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"fieldname": "column_break_8",
"fieldtype": "Column Break",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"depends_on": "",
"fieldname": "to_range",
"fieldtype": "Float",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"label": "To Range",
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
}
],
"hide_heading": 0,
"hide_toolbar": 0,
"icon": "",
"in_create": 0,
"in_dialog": 0,
"is_submittable": 0,
"issingle": 0,
"istable": 1,
"modified": "2015-08-12 02:47:07.959104",
"modified_by": "Administrator",
"module": "Stock",
"name": "Variant Attribute",
"name_case": "",
"owner": "Administrator",
"permissions": [],
"read_only": 0,
"read_only_onload": 0,
"sort_field": "modified",
"sort_order": "DESC"
}