refactor: migrate virtual doctypes to new API

This commit is contained in:
Ankush Menat 2024-03-11 18:00:05 +05:30
parent 1f35886995
commit f7c0dd66fd
7 changed files with 26 additions and 28 deletions

View file

@ -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):

View file

@ -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)]

View file

@ -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):

View file

@ -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."""
...

View file

@ -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
"""
),

View file

@ -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")

View file

@ -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):