Skip to content

Commit

Permalink
fix: uniqueness on common_code does not hold true
Browse files Browse the repository at this point in the history
  • Loading branch information
blaggacao committed Oct 1, 2024
1 parent 8f28447 commit 3040aef
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 29 deletions.
4 changes: 3 additions & 1 deletion erpnext/edi/doctype/common_code/common_code.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"actions": [],
"autoname": "hash",
"creation": "2024-09-29 07:01:18.133067",
"doctype": "DocType",
"engine": "InnoDB",
Expand Down Expand Up @@ -59,10 +60,11 @@
}
],
"links": [],
"modified": "2024-09-30 17:18:44.818486",
"modified": "2024-10-01 13:00:35.702299",
"modified_by": "Administrator",
"module": "EDI",
"name": "Common Code",
"naming_rule": "Random",
"owner": "Administrator",
"permissions": [
{
Expand Down
31 changes: 3 additions & 28 deletions erpnext/edi/doctype/common_code/common_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,16 @@ class CommonCode(Document):
def simple_hash(input_string, length=6):
return hashlib.blake2b(input_string.encode(), digest_size=length // 2).hexdigest()

def autoname(self):
self.name = self.simple_hash(self.code_list) + "|" + self.simple_hash(self.common_code, 10)

def validate(self):
self.validate_unique_code()
self.validate_distinct_references()

def validate_unique_code(self):
"""Ensure the same Common Code does not appear twice in the same Code List."""
if frappe.db.exists(
"Common Code",
{"code_list": self.code_list, "common_code": self.common_code, "name": ("!=", self.name)},
):
frappe.throw(
_("Common Code {0} already exists in Code List {1}").format(self.common_code, self.code_list)
)

def validate_distinct_references(self):
"""Ensure the same reference is not used on a different Common Code of the same Code List."""
"""Ensure no two Common Codes of the same Code List are linked to the same document."""
for link in self.applies_to:
existing_links = frappe.get_all(
"Common Code",
filters=[
["common_code", "!=", self.common_code],
["name", "!=", self.name],
["code_list", "=", self.code_list],
["Dynamic Link", "link_doctype", "=", link.link_doctype],
["Dynamic Link", "link_name", "=", link.link_name],
Expand All @@ -80,7 +66,6 @@ def import_genericode(file_path, list_name, code_column, title_column=None, filt
root = tree.getroot()

codes = []
seen_common_codes = set()
list_hash = CommonCode.simple_hash(list_name)

# Construct the XPath expression
Expand All @@ -95,13 +80,6 @@ def import_genericode(file_path, list_name, code_column, title_column=None, filt
content = etree.tostring(code, encoding="unicode", pretty_print=True)

code_value = code.find(f"./Value[@ColumnRef='{code_column}']/SimpleValue").text
if code_value in seen_common_codes:
frappe.throw(
_("Duplicate value found for '{}'").format(code_column)
+ ":\n\n"
+ f"<pre><code class='language-xml'>{escape_html(content)}</code></pre>"
)
seen_common_codes.add(code_value)
code_hash = CommonCode.simple_hash(code_value, 10)

title = None
Expand All @@ -110,7 +88,7 @@ def import_genericode(file_path, list_name, code_column, title_column=None, filt

codes.append(
{
"name": f"{list_hash}|{code_hash}|{idx}", # according to autoname + row index
"name": f"{list_hash}|{idx}|{code_hash}",
"code_list": list_name,
"common_code": code_value,
"title": title,
Expand All @@ -122,7 +100,4 @@ def import_genericode(file_path, list_name, code_column, title_column=None, filt


def on_doctype_update():
frappe.db.add_unique(
"Common Code", ["code_list", "common_code"], constraint_name="unique_code_list_common_code"
)
frappe.db.add_index("Common Code", ["code_list", "common_code"])

0 comments on commit 3040aef

Please sign in to comment.