refactor: replacing queries with frappe ORM

This commit is contained in:
Aradhya-Tripathi 2021-10-14 01:16:46 +05:30
parent 0d21c6e272
commit e01d97b8df
3 changed files with 30 additions and 21 deletions

View file

@ -4,6 +4,7 @@
import frappe
from frappe.desk.notifications import clear_notifications
from frappe.cache_manager import clear_defaults_cache, common_default_keys
from frappe.query_builder import DocType
# Note: DefaultValue records are identified by parenttype
# __default, __global or 'User Permission'
@ -116,11 +117,11 @@ def set_default(key, value, parent, parenttype="__default"):
:param value: Default value.
:param parent: Usually, **User** to whom the default belongs.
:param parenttype: [optional] default is `__default`."""
table = frappe.qb.DocType("DefaultValue")
result = frappe.qb.from_(table).where(table.defkey == key) \
.where(table.parent == parent) \
.select(table.defkey).for_update().run()
if result:
table = DocType("DefaultValue")
key_exists = frappe.qb.from_(table).where(
(table.defkey == key) & (table.parent == parent)
).select(table.defkey).for_update().run()
if key_exists:
frappe.db.delete("DefaultValue", {
"defkey": key,
"parent": parent
@ -188,9 +189,12 @@ def get_defaults_for(parent="__default"):
if defaults==None:
# sort descending because first default must get precedence
table = frappe.qb.DocType("DefaultValue")
res = frappe.qb.from_(table).where(table.parent == parent) \
.select(table.defkey, table.defvalue).orderby("creation").run(as_dict=True)
table = DocType("DefaultValue")
res = frappe.qb.from_(table).where(
table.parent == parent
).select(
table.defkey, table.defvalue
).orderby("creation").run(as_dict=True)
defaults = frappe._dict({})
for d in res:

View file

@ -6,7 +6,7 @@ import frappe
import frappe.share
from frappe import _, msgprint
from frappe.utils import cint
from frappe.query_builder import DocType
rights = ("select", "read", "write", "create", "delete", "submit", "cancel", "amend",
"print", "email", "report", "import", "export", "set_user_permissions", "share")
@ -333,8 +333,7 @@ def get_all_perms(role):
'''Returns valid permissions for a given role'''
perms = frappe.get_all('DocPerm', fields='*', filters=dict(role=role))
custom_perms = frappe.get_all('Custom DocPerm', fields='*', filters=dict(role=role))
query = frappe.qb.from_("Custom DocPerm").select("parent").distinct()
doctypes_with_custom_perms = frappe.db.sql_list(query)
doctypes_with_custom_perms = frappe.get_all("Custom DocPerm", pluck="parent", distinct=True)
for p in perms:
if p.parent not in doctypes_with_custom_perms:
@ -351,11 +350,12 @@ def get_roles(user=None, with_standard=True):
def get():
if user == 'Administrator':
return [r[0] for r in frappe.qb.from_("Role").select("name").run()] # return all available roles
return frappe.get_all("Role", pluck="name") # return all available roles
else:
table = frappe.qb.DocType("Has Role")
result = frappe.qb.from_(table).where(table.parent == user) \
.where(table.role.notin(["All", "Guest"])).select(table.role).run()
table = DocType("Has Role")
result = frappe.qb.from_(table).where(
(table.parent == user) & (table.role.notin(["All", "Guest"]))
).select(table.role).run()
return [r[0] for r in result] + ['All', 'Guest']
roles = frappe.cache().hget("roles", user, get)
@ -465,7 +465,7 @@ def update_permission_property(doctype, role, permlevel, ptype, value=None, vali
name = frappe.get_value('Custom DocPerm', dict(parent=doctype, role=role,
permlevel=permlevel))
table = frappe.qb.DocType("Custom DocPerm")
table = DocType("Custom DocPerm")
frappe.qb.update(table).set(ptype, value).where(table.name == name).run()
if validate:

View file

@ -20,6 +20,7 @@ from typing import List, Union, Tuple
import frappe
from frappe.model.utils import InvalidIncludePath, render_include
from frappe.utils import get_bench_path, is_html, strip, strip_html_tags
from frappe.query_builder import Field
def get_language(lang_list: List = None) -> str:
@ -156,7 +157,7 @@ def get_dict(fortype, name=None):
messages += frappe.qb.from_("DocType").select("DocType:", "name").run()
messages += frappe.qb.from_("Role").select("Role:", "name").run()
messages += frappe.qb.from_("Module Def").select("Module:", "name").run()
messages += frappe.qb.from_("Workspace Shortcut").where(frappe.qb.Field("format" != None)).select("").run()
messages += frappe.qb.from_("Workspace Shortcut").where(Field("format").isnotnull()).select("").run()
messages += frappe.qb.from_("Onboarding Step").select("", "title").run()
messages = deduplicate_messages(messages)
@ -324,13 +325,17 @@ def get_messages_for_app(app, deduplicate=True):
# doctypes
if modules:
for name in frappe.db.sql_list("""select name from tabDocType
where module in ({})""".format(modules)):
names = frappe.qb.from_("DocType").where(
Field("module").isin(modules)
).select("name").run()
for name in names:
messages.extend(get_messages_from_doctype(name))
# pages
for name, title in frappe.db.sql("""select name, title from tabPage
where module in ({})""".format(modules)):
result = frappe.qb.from_("Page").where(
Field("module").isin(modules)
).select("name", "title").run()
for name, title in result:
messages.append((None, title or name))
messages.extend(get_messages_from_page(name))