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

File diff suppressed because it is too large Load Diff

View File

@ -341,7 +341,7 @@ class Item(WebsiteGenerator):
frappe.throw(_("Attribute {0} selected multiple times in Attributes Table".format(d.attribute))) frappe.throw(_("Attribute {0} selected multiple times in Attributes Table".format(d.attribute)))
else: else:
attributes.append(d.attribute) attributes.append(d.attribute)
def validate_variant_attributes(self): def validate_variant_attributes(self):
if self.variant_of: if self.variant_of:
args = {} args = {}
@ -349,7 +349,7 @@ class Item(WebsiteGenerator):
if not d.attribute_value: if not d.attribute_value:
frappe.throw(_("Please specify Attribute Value for attribute {0}").format(d.attribute)) frappe.throw(_("Please specify Attribute Value for attribute {0}").format(d.attribute))
args[d.attribute] = d.attribute_value args[d.attribute] = d.attribute_value
variant = get_variant(self.variant_of, args) variant = get_variant(self.variant_of, args)
if variant and not variant[0][0] == self.name: if variant and not variant[0][0] == self.name:
frappe.throw(_("Item variant {0} exists with same attributes".format(variant[0][0]) )) frappe.throw(_("Item variant {0} exists with same attributes".format(variant[0][0]) ))
@ -494,13 +494,13 @@ def get_variant(item, args):
numeric_attributes = [] numeric_attributes = []
for t in frappe.db.get_all("Item Attribute Value", fields=["parent", "attribute_value"]): for t in frappe.db.get_all("Item Attribute Value", fields=["parent", "attribute_value"]):
attributes.setdefault(t.parent, []).append(t.attribute_value) attributes.setdefault(t.parent, []).append(t.attribute_value)
for t in frappe.get_list("Item Attribute", filters={"numeric_values":1}): for t in frappe.get_list("Item Attribute", filters={"numeric_values":1}):
numeric_attributes.append(t.name) numeric_attributes.append(t.name)
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):
@ -517,17 +517,17 @@ def get_variant(item, args):
if conds: if conds:
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))
return variant return variant
@frappe.whitelist() @frappe.whitelist()
def create_variant(item, param): def create_variant(item, param):
args = json.loads(param) args = json.loads(param)
@ -542,7 +542,7 @@ def create_variant(item, param):
template = frappe.get_doc("Item", item) template = frappe.get_doc("Item", item)
copy_attributes_to_variant(template, variant) copy_attributes_to_variant(template, variant)
return variant return variant
def copy_attributes_to_variant(item, variant): def copy_attributes_to_variant(item, variant):
from frappe.model import no_value_fields from frappe.model import no_value_fields
for field in item.meta.fields: for field in item.meta.fields:

View File

@ -33,13 +33,13 @@ class ItemAttribute(Document):
if d.abbr in abbrs: if d.abbr in abbrs:
frappe.throw(_("{0} must appear only once").format(d.abbr)) frappe.throw(_("{0} must appear only once").format(d.abbr))
abbrs.append(d.abbr) abbrs.append(d.abbr)
def validate_attribute_values(self): def validate_attribute_values(self):
attribute_values = [] attribute_values = []
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"
}