From f7c0dd66fd711250015abca13979faf927ccdca7 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Mon, 11 Mar 2024 18:00:05 +0530 Subject: [PATCH] refactor: migrate virtual doctypes to new API --- .../permission_inspector/permission_inspector.py | 6 +++--- frappe/core/doctype/recorder/recorder.py | 15 ++++++--------- .../core/doctype/recorder_query/recorder_query.py | 6 +++--- frappe/model/virtual_doctype.py | 6 +++--- frappe/modules/utils.py | 6 +++--- frappe/tests/test_db_query.py | 2 ++ frappe/tests/test_virtual_doctype.py | 13 ++++++------- 7 files changed, 26 insertions(+), 28 deletions(-) diff --git a/frappe/core/doctype/permission_inspector/permission_inspector.py b/frappe/core/doctype/permission_inspector/permission_inspector.py index 4197f3f4c9..c85df1648b 100644 --- a/frappe/core/doctype/permission_inspector/permission_inspector.py +++ b/frappe/core/doctype/permission_inspector/permission_inspector.py @@ -60,15 +60,15 @@ class PermissionInspector(Document): ... @staticmethod - def get_list(args): + def get_list(): ... @staticmethod - def get_count(args): + def get_count(): ... @staticmethod - def get_stats(args): + def get_stats(): ... def delete(self): diff --git a/frappe/core/doctype/recorder/recorder.py b/frappe/core/doctype/recorder/recorder.py index 26ccfcf378..ddbf208ad2 100644 --- a/frappe/core/doctype/recorder/recorder.py +++ b/frappe/core/doctype/recorder/recorder.py @@ -39,12 +39,10 @@ class Recorder(Document): super(Document, self).__init__(request) @staticmethod - def get_list(args): - start = cint(args.get("start")) - page_length = cint(args.get("page_length")) or 20 - requests = Recorder.get_filtered_requests(args)[start : start + page_length] + def get_list(filters=None, start=0, page_length=20, order_by="duration desc"): + requests = Recorder.get_filtered_requests(filters)[start : start + page_length] - if order_by_statment := args.get("order_by"): + if order_by_statment := order_by: if "." in order_by_statment: order_by_statment = order_by_statment.split(".")[1] @@ -60,12 +58,11 @@ class Recorder(Document): return sorted(requests, key=lambda r: r.duration, reverse=1) @staticmethod - def get_count(args): - return len(Recorder.get_filtered_requests(args)) + def get_count(filters=None): + return len(Recorder.get_filtered_requests(filters)) @staticmethod - def get_filtered_requests(args): - filters = args.get("filters") + def get_filtered_requests(filters): requests = [serialize_request(request) for request in get_recorder_data()] return [req for req in requests if evaluate_filters(req, filters)] diff --git a/frappe/core/doctype/recorder_query/recorder_query.py b/frappe/core/doctype/recorder_query/recorder_query.py index c797769dda..b3ebc66b98 100644 --- a/frappe/core/doctype/recorder_query/recorder_query.py +++ b/frappe/core/doctype/recorder_query/recorder_query.py @@ -39,15 +39,15 @@ class RecorderQuery(Document): pass @staticmethod - def get_list(args): + def get_list(): pass @staticmethod - def get_count(args): + def get_count(): pass @staticmethod - def get_stats(args): + def get_stats(): pass def delete(self): diff --git a/frappe/model/virtual_doctype.py b/frappe/model/virtual_doctype.py index 6390e35cef..c5a948a7c4 100644 --- a/frappe/model/virtual_doctype.py +++ b/frappe/model/virtual_doctype.py @@ -21,17 +21,17 @@ class VirtualDoctype(Protocol): # ============ class/static methods ============ @staticmethod - def get_list(args) -> list[frappe._dict]: + def get_list(**kwargs) -> list[frappe._dict]: """Similar to reportview.get_list""" ... @staticmethod - def get_count(args) -> int: + def get_count(**kwargs) -> int: """Similar to reportview.get_count, return total count of documents on listview.""" ... @staticmethod - def get_stats(args): + def get_stats(**kwargs): """Similar to reportview.get_stats, return sidebar stats.""" ... diff --git a/frappe/modules/utils.py b/frappe/modules/utils.py index a3a5bfc719..dda1b83732 100644 --- a/frappe/modules/utils.py +++ b/frappe/modules/utils.py @@ -307,15 +307,15 @@ def make_boilerplate( raise NotImplementedError @staticmethod - def get_list(args): + def get_list(filters=None, page_length=20, **kwargs): pass @staticmethod - def get_count(args): + def get_count(filters=None, **kwargs): pass @staticmethod - def get_stats(args): + def get_stats(**kwargs): pass """ ), diff --git a/frappe/tests/test_db_query.py b/frappe/tests/test_db_query.py index 4c0776fe7e..078bb55c42 100644 --- a/frappe/tests/test_db_query.py +++ b/frappe/tests/test_db_query.py @@ -1076,7 +1076,9 @@ class TestDBQuery(FrappeTestCase): class VirtualDocType: @staticmethod def get_list(args=None, limit_page_length=0, doctype=None): + # Backward compatibility self.assertEqual(args["filters"], [["Virtual DocType", "name", "=", "test"]]) + self.assertEqual(limit_page_length, 1) self.assertEqual(doctype, "Virtual DocType") diff --git a/frappe/tests/test_virtual_doctype.py b/frappe/tests/test_virtual_doctype.py index f21cd140cd..940aa24f87 100644 --- a/frappe/tests/test_virtual_doctype.py +++ b/frappe/tests/test_virtual_doctype.py @@ -68,17 +68,17 @@ class VirtualDoctypeTest(Document): self.update_data(data) @staticmethod - def get_list(args): + def get_list(): data = VirtualDoctypeTest.get_current_data() return [frappe._dict(doc) for name, doc in data.items()] @staticmethod - def get_count(args): + def get_count(): data = VirtualDoctypeTest.get_current_data() return len(data) @staticmethod - def get_stats(args): + def get_stats(): return {} @@ -157,19 +157,18 @@ class TestVirtualDoctypes(FrappeTestCase): updated_docs = {doc1.name, doc2.name} self.assertEqual(docs, updated_docs) - listed_docs = {d.name for d in VirtualDoctypeTest.get_list({})} + listed_docs = {d.name for d in VirtualDoctypeTest.get_list()} self.assertEqual(docs, listed_docs) def test_get_count(self): - args = {"doctype": TEST_DOCTYPE_NAME, "filters": [], "fields": []} - self.assertIsInstance(VirtualDoctypeTest.get_count(args), int) + self.assertIsInstance(VirtualDoctypeTest.get_count(), int) def test_delete_doc(self): doc = frappe.get_doc(doctype=TEST_DOCTYPE_NAME).insert() frappe.delete_doc(doc.doctype, doc.name) - listed_docs = {d.name for d in VirtualDoctypeTest.get_list({})} + listed_docs = {d.name for d in VirtualDoctypeTest.get_list()} self.assertNotIn(doc.name, listed_docs) def test_controller_validity(self):