refactor: rewrite raw sql queries with frappe.qb and database API

This commit is contained in:
Rucha Mahabal 2022-07-07 19:00:19 +05:30
parent 64075cbebc
commit 108cce2785
6 changed files with 26 additions and 34 deletions

View File

@ -1175,8 +1175,8 @@ def validate_inclusive_tax(tax, doc):
@frappe.whitelist()
# nosemgrep
def get_outstanding_reference_documents(args):
if isinstance(args, str):
args = json.loads(args)

View File

@ -420,7 +420,7 @@ def add_cc(args=None):
return cc.name
def reconcile_against_document(args):
def reconcile_against_document(args): # nosemgrep
"""
Cancel PE or JV, Update against document, split if required and resubmit
"""

View File

@ -87,6 +87,7 @@ website_context = {
"splash_image": "/assets/erpnext/images/erpnext-logo.svg",
}
# nosemgrep
website_route_rules = [
{"from_route": "/orders", "to_route": "Sales Order"},
{

View File

@ -52,33 +52,18 @@ def get_abbreviated_name(name, company):
@frappe.whitelist()
def get_children(doctype, parent=None, company=None, is_root=False):
condition = ""
var_dict = {
"name": get_root_of("Department"),
"parent": parent,
"company": company,
}
if company == parent:
condition = "name=%(name)s"
elif company:
condition = "parent_department=%(parent)s and company=%(company)s"
else:
condition = "parent_department = %(parent)s"
fields = ["name as value", "is_group as expandable"]
filters = {}
return frappe.db.sql(
"""
select
name as value,
is_group as expandable
from `tab{doctype}`
where
{condition}
order by name""".format(
doctype=doctype, condition=condition
),
var_dict,
as_dict=1,
)
if company == parent:
filters["name"] = get_root_of("Department")
elif company:
filters["parent_department"] = parent
filters["company"] = company
else:
filters["parent_department"] = parent
return frappe.get_all(doctype, fields=fields, filters=filters, order_by="name")
@frappe.whitelist()

View File

@ -217,14 +217,19 @@ class Employee(NestedSet):
frappe.throw(_("User {0} is disabled").format(self.user_id), EmployeeUserDisabledError)
def validate_duplicate_user_id(self):
employee = frappe.db.sql_list(
"""select name from `tabEmployee` where
user_id=%s and status='Active' and name!=%s""",
(self.user_id, self.name),
)
Employee = frappe.qb.DocType("Employee")
employee = (
frappe.qb.from_(Employee)
.select(Employee.name)
.where(
(Employee.user_id == self.user_id)
& (Employee.status == "Active")
& (Employee.name != self.name)
)
).run()
if employee:
throw(
_("User {0} is already assigned to Employee {1}").format(self.user_id, employee[0]),
_("User {0} is already assigned to Employee {1}").format(self.user_id, employee[0][0]),
frappe.DuplicateEntryError,
)

View File

@ -6,6 +6,7 @@ from frappe import _
from frappe.utils import cstr, getdate
# nosemgrep
def set_default_settings(args):
# enable default currency
frappe.db.set_value("Currency", args.get("currency"), "enabled", 1)