|
| 1 | +# Copyright (c) 2024, Frappe Technologies Pvt. Ltd. and contributors |
| 2 | +# For license information, please see license.txt |
| 3 | + |
| 4 | +from typing import TYPE_CHECKING |
| 5 | + |
| 6 | +import frappe |
| 7 | +from frappe.model.document import Document |
| 8 | + |
| 9 | +if TYPE_CHECKING: |
| 10 | + from lxml.etree import Element |
| 11 | + |
| 12 | + |
| 13 | +class CodeList(Document): |
| 14 | + # begin: auto-generated types |
| 15 | + # This code is auto-generated. Do not modify anything in this block. |
| 16 | + |
| 17 | + from typing import TYPE_CHECKING |
| 18 | + |
| 19 | + if TYPE_CHECKING: |
| 20 | + from frappe.types import DF |
| 21 | + |
| 22 | + canonical_uri: DF.Data | None |
| 23 | + default_common_code: DF.Link | None |
| 24 | + description: DF.SmallText | None |
| 25 | + publisher: DF.Data | None |
| 26 | + publisher_id: DF.Data | None |
| 27 | + title: DF.Data | None |
| 28 | + url: DF.Data | None |
| 29 | + version: DF.Data | None |
| 30 | + # end: auto-generated types |
| 31 | + |
| 32 | + def on_trash(self): |
| 33 | + if not frappe.flags.in_bulk_delete: |
| 34 | + self.__delete_linked_docs() |
| 35 | + |
| 36 | + def __delete_linked_docs(self): |
| 37 | + self.db_set("default_common_code", None) |
| 38 | + |
| 39 | + linked_docs = frappe.get_all( |
| 40 | + "Common Code", |
| 41 | + filters={"code_list": self.name}, |
| 42 | + fields=["name"], |
| 43 | + ) |
| 44 | + |
| 45 | + for doc in linked_docs: |
| 46 | + frappe.delete_doc("Common Code", doc.name) |
| 47 | + |
| 48 | + def get_codes_for(self, doctype: str, name: str) -> tuple[str]: |
| 49 | + """Get the applicable codes for a doctype and name""" |
| 50 | + return get_codes_for(self.name, doctype, name) |
| 51 | + |
| 52 | + def get_docnames_for(self, doctype: str, code: str) -> tuple[str]: |
| 53 | + """Get the mapped docnames for a doctype and code""" |
| 54 | + return get_docnames_for(self.name, doctype, code) |
| 55 | + |
| 56 | + def get_default_code(self) -> str | None: |
| 57 | + """Get the default common code for this code list""" |
| 58 | + return ( |
| 59 | + frappe.db.get_value("Common Code", self.default_common_code, "common_code") |
| 60 | + if self.default_common_code |
| 61 | + else None |
| 62 | + ) |
| 63 | + |
| 64 | + def from_genericode(self, root: "Element"): |
| 65 | + """Extract Code List details from genericode XML""" |
| 66 | + self.title = root.find(".//Identification/ShortName").text |
| 67 | + self.version = root.find(".//Identification/Version").text |
| 68 | + self.canonical_uri = root.find(".//CanonicalUri").text |
| 69 | + # optionals |
| 70 | + self.description = getattr(root.find(".//Identification/LongName"), "text", None) |
| 71 | + self.publisher = getattr(root.find(".//Identification/Agency/ShortName"), "text", None) |
| 72 | + if not self.publisher: |
| 73 | + self.publisher = getattr(root.find(".//Identification/Agency/LongName"), "text", None) |
| 74 | + self.publisher_id = getattr(root.find(".//Identification/Agency/Identifier"), "text", None) |
| 75 | + self.url = getattr(root.find(".//Identification/LocationUri"), "text", None) |
| 76 | + |
| 77 | + |
| 78 | +def get_codes_for(code_list: str, doctype: str, name: str) -> tuple[str]: |
| 79 | + """Return the common code for a given record""" |
| 80 | + CommonCode = frappe.qb.DocType("Common Code") |
| 81 | + DynamicLink = frappe.qb.DocType("Dynamic Link") |
| 82 | + |
| 83 | + codes = ( |
| 84 | + frappe.qb.from_(CommonCode) |
| 85 | + .join(DynamicLink) |
| 86 | + .on((CommonCode.name == DynamicLink.parent) & (DynamicLink.parenttype == "Common Code")) |
| 87 | + .select(CommonCode.common_code) |
| 88 | + .where( |
| 89 | + (DynamicLink.link_doctype == doctype) |
| 90 | + & (DynamicLink.link_name == name) |
| 91 | + & (CommonCode.code_list == code_list) |
| 92 | + ) |
| 93 | + .distinct() |
| 94 | + .orderby(CommonCode.common_code) |
| 95 | + ).run() |
| 96 | + |
| 97 | + return tuple(c[0] for c in codes) if codes else () |
| 98 | + |
| 99 | + |
| 100 | +def get_docnames_for(code_list: str, doctype: str, code: str) -> tuple[str]: |
| 101 | + """Return the record name for a given common code""" |
| 102 | + CommonCode = frappe.qb.DocType("Common Code") |
| 103 | + DynamicLink = frappe.qb.DocType("Dynamic Link") |
| 104 | + |
| 105 | + docnames = ( |
| 106 | + frappe.qb.from_(CommonCode) |
| 107 | + .join(DynamicLink) |
| 108 | + .on((CommonCode.name == DynamicLink.parent) & (DynamicLink.parenttype == "Common Code")) |
| 109 | + .select(DynamicLink.link_name) |
| 110 | + .where( |
| 111 | + (DynamicLink.link_doctype == doctype) |
| 112 | + & (CommonCode.common_code == code) |
| 113 | + & (CommonCode.code_list == code_list) |
| 114 | + ) |
| 115 | + .distinct() |
| 116 | + .orderby(DynamicLink.idx) |
| 117 | + ).run() |
| 118 | + |
| 119 | + return tuple(d[0] for d in docnames) if docnames else () |
| 120 | + |
| 121 | + |
| 122 | +def get_default_code(code_list: str) -> str | None: |
| 123 | + """Return the default common code for a given code list""" |
| 124 | + code_id = frappe.db.get_value("Code List", code_list, "default_common_code") |
| 125 | + return frappe.db.get_value("Common Code", code_id, "common_code") if code_id else None |
0 commit comments