From 266e1f95ed80811a816cad4816f8239eed08ca7e Mon Sep 17 00:00:00 2001 From: phot0n Date: Mon, 7 Feb 2022 15:33:19 +0530 Subject: [PATCH] chore: adding back idx column This is needed for ordering in display of links as it keeps track of the clicks on a particular link item --- frappe/contacts/doctype/address/address.py | 2 +- frappe/contacts/doctype/contact/contact.py | 2 +- frappe/core/doctype/doctype/doctype.py | 1 - frappe/core/doctype/user_type/user_type.py | 2 +- frappe/database/database.py | 4 ++-- frappe/database/mariadb/framework_mariadb.sql | 1 + frappe/database/mariadb/schema.py | 4 ++-- frappe/database/postgres/framework_postgres.sql | 1 + frappe/database/postgres/schema.py | 4 ++-- frappe/database/schema.py | 2 +- frappe/desk/search.py | 2 +- frappe/model/__init__.py | 6 +++--- frappe/model/base_document.py | 10 +++++----- frappe/model/delete_doc.py | 1 - frappe/model/mapper.py | 2 +- frappe/public/js/frappe/model/model.js | 3 ++- frappe/tests/test_db_update.py | 4 ++-- frappe/website/website_generator.py | 1 + 18 files changed, 27 insertions(+), 25 deletions(-) diff --git a/frappe/contacts/doctype/address/address.py b/frappe/contacts/doctype/address/address.py index 921990f2a3..5d0ed18d5f 100644 --- a/frappe/contacts/doctype/address/address.py +++ b/frappe/contacts/doctype/address/address.py @@ -241,7 +241,7 @@ def address_query(doctype, txt, searchfield, start, page_len, filters): {mcond} {condition} order by if(locate(%(_txt)s, `tabAddress`.name), locate(%(_txt)s, `tabAddress`.name), 99999), - `tabAddress`.name + `tabAddress`.idx desc, `tabAddress`.name limit %(start)s, %(page_len)s """.format( mcond=get_match_cond(doctype), key=searchfield, diff --git a/frappe/contacts/doctype/contact/contact.py b/frappe/contacts/doctype/contact/contact.py index d3d0fba62c..9152655b85 100644 --- a/frappe/contacts/doctype/contact/contact.py +++ b/frappe/contacts/doctype/contact/contact.py @@ -215,7 +215,7 @@ def contact_query(doctype, txt, searchfield, start, page_len, filters): {mcond} order by if(locate(%(_txt)s, `tabContact`.name), locate(%(_txt)s, `tabContact`.name), 99999), - `tabContact`.name + `tabContact`.idx desc, `tabContact`.name limit %(start)s, %(page_len)s """.format(mcond=get_match_cond(doctype), key=searchfield), { 'txt': '%' + txt + '%', '_txt': txt.replace("%", ""), diff --git a/frappe/core/doctype/doctype/doctype.py b/frappe/core/doctype/doctype/doctype.py index 2df7516d93..d259367a16 100644 --- a/frappe/core/doctype/doctype/doctype.py +++ b/frappe/core/doctype/doctype/doctype.py @@ -707,7 +707,6 @@ class DocType(Document): add_column(self.name, "parent", "Data") add_column(self.name, "parenttype", "Data") add_column(self.name, "parentfield", "Data") - add_column(self.name, "idx", "Int", length=8, not_null=True, default="0") def get_max_idx(self): """Returns the highest `idx`""" diff --git a/frappe/core/doctype/user_type/user_type.py b/frappe/core/doctype/user_type/user_type.py index a64ad1a130..c0dfd2e597 100644 --- a/frappe/core/doctype/user_type/user_type.py +++ b/frappe/core/doctype/user_type/user_type.py @@ -193,7 +193,7 @@ def get_user_linked_doctypes(doctype, txt, searchfield, start, page_len, filters ['DocType', 'read_only', '=', 0], ['DocType', 'name', 'like', '%{0}%'.format(txt)]] doctypes = frappe.get_all('DocType', fields = ['`tabDocType`.`name`'], filters=filters, - limit_start=start, limit_page_length=page_len, as_list=1) + order_by='`tabDocType`.`idx` desc', limit_start=start, limit_page_length=page_len, as_list=1) custom_dt_filters = [['Custom Field', 'dt', 'like', '%{0}%'.format(txt)], ['Custom Field', 'options', '=', 'User'], ['Custom Field', 'fieldtype', '=', 'Link']] diff --git a/frappe/database/database.py b/frappe/database/database.py index 3cebe4510d..9fa1ff161c 100644 --- a/frappe/database/database.py +++ b/frappe/database/database.py @@ -38,8 +38,8 @@ class Database(object): OPTIONAL_COLUMNS = ["_user_tags", "_comments", "_assign", "_liked_by"] DEFAULT_SHORTCUTS = ['_Login', '__user', '_Full Name', 'Today', '__today', "now", "Now"] STANDARD_VARCHAR_COLUMNS = ('name', 'owner', 'modified_by') - DEFAULT_COLUMNS = ['name', 'creation', 'modified', 'modified_by', 'owner', 'docstatus'] - CHILD_TABLE_COLUMNS = ('parent', 'parenttype', 'parentfield', 'idx') + DEFAULT_COLUMNS = ['name', 'creation', 'modified', 'modified_by', 'owner', 'docstatus', 'idx'] + CHILD_TABLE_COLUMNS = ('parent', 'parenttype', 'parentfield') MAX_WRITES_PER_TRANSACTION = 200_000 class InvalidColumnName(frappe.ValidationError): pass diff --git a/frappe/database/mariadb/framework_mariadb.sql b/frappe/database/mariadb/framework_mariadb.sql index b59beefa0b..7c9309ee9f 100644 --- a/frappe/database/mariadb/framework_mariadb.sql +++ b/frappe/database/mariadb/framework_mariadb.sql @@ -171,6 +171,7 @@ CREATE TABLE `tabDocType` ( `modified_by` varchar(255) DEFAULT NULL, `owner` varchar(255) DEFAULT NULL, `docstatus` int(1) NOT NULL DEFAULT 0, + `idx` int(8) NOT NULL DEFAULT 0, `search_fields` varchar(255) DEFAULT NULL, `issingle` int(1) NOT NULL DEFAULT 0, `is_tree` int(1) NOT NULL DEFAULT 0, diff --git a/frappe/database/mariadb/schema.py b/frappe/database/mariadb/schema.py index 2f375bc751..0dc5863aa4 100644 --- a/frappe/database/mariadb/schema.py +++ b/frappe/database/mariadb/schema.py @@ -24,8 +24,7 @@ class MariaDBTable(DBTable): ( f"parent varchar({varchar_len})", f"parentfield varchar({varchar_len})", - f"parenttype varchar({varchar_len})", - "idx int(8) not null default '0'" + f"parenttype varchar({varchar_len})" ) ) + ',\n' @@ -37,6 +36,7 @@ class MariaDBTable(DBTable): modified_by varchar({varchar_len}), owner varchar({varchar_len}), docstatus int(1) not null default '0', + idx int(8) not null default '0', {additional_definitions} index modified(modified)) ENGINE={engine} diff --git a/frappe/database/postgres/framework_postgres.sql b/frappe/database/postgres/framework_postgres.sql index 19381607d4..1662b7b93e 100644 --- a/frappe/database/postgres/framework_postgres.sql +++ b/frappe/database/postgres/framework_postgres.sql @@ -176,6 +176,7 @@ CREATE TABLE "tabDocType" ( "modified_by" varchar(255) DEFAULT NULL, "owner" varchar(255) DEFAULT NULL, "docstatus" smallint NOT NULL DEFAULT 0, + "idx" bigint NOT NULL DEFAULT 0, "search_fields" varchar(255) DEFAULT NULL, "issingle" smallint NOT NULL DEFAULT 0, "is_tree" smallint NOT NULL DEFAULT 0, diff --git a/frappe/database/postgres/schema.py b/frappe/database/postgres/schema.py index 9dbb597adf..9487bc2fa7 100644 --- a/frappe/database/postgres/schema.py +++ b/frappe/database/postgres/schema.py @@ -21,8 +21,7 @@ class PostgresTable(DBTable): ( "parent varchar({varchar_len})", "parentfield varchar({varchar_len})", - "parenttype varchar({varchar_len})", - "idx bigint not null default '0'" + "parenttype varchar({varchar_len})" ) ) @@ -35,6 +34,7 @@ class PostgresTable(DBTable): modified_by varchar({varchar_len}), owner varchar({varchar_len}), docstatus smallint not null default '0', + idx bigint not null default '0', %s)""" % (self.table_name, add_text)).format(varchar_len=frappe.db.VARCHAR_LEN)) self.create_indexes() diff --git a/frappe/database/schema.py b/frappe/database/schema.py index 7315d3630a..dd54385c83 100644 --- a/frappe/database/schema.py +++ b/frappe/database/schema.py @@ -108,7 +108,7 @@ class DBTable: frappe.db.STANDARD_VARCHAR_COLUMNS] if self.meta.get("istable"): columns += [frappe._dict({"fieldname": f, "fieldtype": "Data"}) for f in - frappe.db.CHILD_TABLE_COLUMNS if f != "idx"] + frappe.db.CHILD_TABLE_COLUMNS] columns += self.columns.values() for col in columns: diff --git a/frappe/desk/search.py b/frappe/desk/search.py index e89ef2be24..95397070ae 100644 --- a/frappe/desk/search.py +++ b/frappe/desk/search.py @@ -147,7 +147,7 @@ def search_widget(doctype, txt, query=None, searchfield=None, start=0, from frappe.model.db_query import get_order_by order_by_based_on_meta = get_order_by(doctype, meta) # 2 is the index of _relevance column - order_by = "_relevance, {0}".format(order_by_based_on_meta) + order_by = "_relevance, {0}, `tab{1}`.idx desc".format(order_by_based_on_meta, doctype) ptype = 'select' if frappe.only_has_select_perm(doctype) else 'read' ignore_permissions = True if doctype == "DocType" else (cint(ignore_user_permissions) and has_permission(doctype, ptype=ptype)) diff --git a/frappe/model/__init__.py b/frappe/model/__init__.py index d635b31900..be9496c85b 100644 --- a/frappe/model/__init__.py +++ b/frappe/model/__init__.py @@ -90,14 +90,14 @@ default_fields = ( 'creation', 'modified', 'modified_by', - 'docstatus' + 'docstatus', + 'idx' ) child_table_fields = ( 'parent', 'parentfield', - 'parenttype', - 'idx' + 'parenttype' ) optional_fields = ( diff --git a/frappe/model/base_document.py b/frappe/model/base_document.py index bd3875cbea..307d95e84b 100644 --- a/frappe/model/base_document.py +++ b/frappe/model/base_document.py @@ -292,16 +292,16 @@ class BaseDocument(object): if key not in self.__dict__: self.__dict__[key] = None - if key == "docstatus" and self.__dict__[key] is None: - self.__dict__[key] = DocStatus.draft() + if self.__dict__[key] is None: + if key == "docstatus": + self.docstatus = DocStatus.draft() + elif key == "idx": + self.__dict__[key] = 0 for key in self.get_valid_columns(): if key not in self.__dict__: self.__dict__[key] = None - if key == "idx" and self.__dict__[key] is None: - self.__dict__[key] = 0 - def get_valid_columns(self): if self.doctype not in frappe.local.valid_columns: if self.doctype in DOCTYPES_FOR_DOCTYPE: diff --git a/frappe/model/delete_doc.py b/frappe/model/delete_doc.py index a888e3f7f7..ef73a349cc 100644 --- a/frappe/model/delete_doc.py +++ b/frappe/model/delete_doc.py @@ -232,7 +232,6 @@ def check_if_doc_is_linked(doc, method="Delete"): if frappe.get_meta(link_dt).istable: fields.extend(["parent", "parenttype"]) - # NOTE: scenario: parent doc <-(linked to) child table doc <-(linked to) doc for item in frappe.db.get_values(link_dt, {link_field:doc.name}, fields , as_dict=True): # available only in child table cases item_parent = getattr(item, "parent", None) diff --git a/frappe/model/mapper.py b/frappe/model/mapper.py index 1ebff84a08..f40a43bb73 100644 --- a/frappe/model/mapper.py +++ b/frappe/model/mapper.py @@ -184,7 +184,7 @@ def map_fields(source_doc, target_doc, table_map, source_parent): target_doc.set(fmap[1], val) # map idx - if source_doc.get("idx"): + if source_doc.idx: target_doc.idx = source_doc.idx # add fetch diff --git a/frappe/public/js/frappe/model/model.js b/frappe/public/js/frappe/model/model.js index ef161fca3d..89e029ffb1 100644 --- a/frappe/public/js/frappe/model/model.js +++ b/frappe/public/js/frappe/model/model.js @@ -10,7 +10,7 @@ $.extend(frappe.model, { layout_fields: ['Section Break', 'Column Break', 'Tab Break', 'Fold'], std_fields_list: ['name', 'owner', 'creation', 'modified', 'modified_by', - '_user_tags', '_comments', '_assign', '_liked_by', 'docstatus'], + '_user_tags', '_comments', '_assign', '_liked_by', 'docstatus', 'idx'], core_doctypes_list: ['DocType', 'DocField', 'DocPerm', 'User', 'Role', 'Has Role', 'Page', 'Module Def', 'Print Format', 'Report', 'Customize Form', @@ -19,6 +19,7 @@ $.extend(frappe.model, { std_fields: [ {fieldname:'name', fieldtype:'Link', label:__('ID')}, {fieldname:'owner', fieldtype:'Link', label:__('Created By'), options: 'User'}, + {fieldname:'idx', fieldtype:'Int', label:__('Index')}, {fieldname:'creation', fieldtype:'Date', label:__('Created On')}, {fieldname:'modified', fieldtype:'Date', label:__('Last Updated On')}, {fieldname:'modified_by', fieldtype:'Data', label:__('Last Updated By')}, diff --git a/frappe/tests/test_db_update.py b/frappe/tests/test_db_update.py index 4c0cc4b2bc..66eb05391a 100644 --- a/frappe/tests/test_db_update.py +++ b/frappe/tests/test_db_update.py @@ -106,6 +106,7 @@ def get_other_fields_meta(meta): 'modified_by': ('Data', 0), 'creation': ('Datetime', 0), 'modified': ('Datetime', 0), + 'idx': ('Int', 8), 'docstatus': ('Check', 0) } @@ -115,8 +116,7 @@ def get_other_fields_meta(meta): child_table_fields_map = {} if meta.istable: - child_table_fields_map.update({field: ('Data', 0) for field in frappe.db.CHILD_TABLE_COLUMNS if field != 'idx'}) - child_table_fields_map.update({'idx': ('Int', 8)}) + child_table_fields_map.update({field: ('Data', 0) for field in frappe.db.CHILD_TABLE_COLUMNS}) optional_fields_map = {field: ('Text', 0) for field in optional_fields} fields = dict(default_fields_map, **optional_fields_map, **child_table_fields_map) diff --git a/frappe/website/website_generator.py b/frappe/website/website_generator.py index 67b8e77c4f..e66496aa89 100644 --- a/frappe/website/website_generator.py +++ b/frappe/website/website_generator.py @@ -118,6 +118,7 @@ class WebsiteGenerator(Document): "doc": self, "page_or_generator": "Generator", "ref_doctype":self.doctype, + "idx": self.idx, "docname": self.name, "controller": get_module_name(self.doctype, self.meta.module), })