Skip to content

Commit

Permalink
refactor: Use db.set_single_value (frappe#35668)
Browse files Browse the repository at this point in the history
I just applied semgrep autofix. Untested completed, review before merging.

```yaml
- id: frappe-set-value-semantics
  patterns:
    - pattern-either:
      - pattern: frappe.db.set_value($DOCTYPE, None, $...AFTER)
      - pattern: frappe.db.set_value($DOCTYPE, $DOCTYPE, $...AFTER)
  fix: frappe.db.set_single_value($DOCTYPE, $...AFTER)
  message: |
    If $DOCTYPE is a single doctype then using `frappe.db.set_value` is discouraged for setting values in DB. Use db.set_single_value for single doctype instead.
  languages: [python]
  severity: ERROR
```
  • Loading branch information
ankush authored Jun 13, 2023
1 parent b43e068 commit a3ea985
Show file tree
Hide file tree
Showing 43 changed files with 124 additions and 142 deletions.
Empty file added .semgrepignore
Empty file.
2 changes: 1 addition & 1 deletion erpnext/accounts/doctype/fiscal_year/fiscal_year.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class FiscalYear(Document):
@frappe.whitelist()
def set_as_default(self):
frappe.db.set_value("Global Defaults", None, "current_fiscal_year", self.name)
frappe.db.set_single_value("Global Defaults", "current_fiscal_year", self.name)
global_defaults = frappe.get_doc("Global Defaults")
global_defaults.check_permission("write")
global_defaults.on_update()
Expand Down
4 changes: 2 additions & 2 deletions erpnext/accounts/doctype/journal_entry/test_journal_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ def cancel_against_voucher_testcase(self, test_voucher):

elif test_voucher.doctype in ["Sales Order", "Purchase Order"]:
# if test_voucher is a Sales Order/Purchase Order, test error on cancellation of test_voucher
frappe.db.set_value(
"Accounts Settings", "Accounts Settings", "unlink_advance_payment_on_cancelation_of_order", 0
frappe.db.set_single_value(
"Accounts Settings", "unlink_advance_payment_on_cancelation_of_order", 0
)
submitted_voucher = frappe.get_doc(test_voucher.doctype, test_voucher.name)
self.assertRaises(frappe.LinkExistsError, submitted_voucher.cancel)
Expand Down
4 changes: 2 additions & 2 deletions erpnext/accounts/doctype/pos_invoice/test_pos_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def tearDown(self):
frappe.set_user("Administrator")

if frappe.db.get_single_value("Selling Settings", "validate_selling_price"):
frappe.db.set_value("Selling Settings", None, "validate_selling_price", 0)
frappe.db.set_single_value("Selling Settings", "validate_selling_price", 0)

def test_timestamp_change(self):
w = create_pos_invoice(do_not_save=1)
Expand Down Expand Up @@ -722,7 +722,7 @@ def test_merging_with_validate_selling_price(self):
)

if not frappe.db.get_single_value("Selling Settings", "validate_selling_price"):
frappe.db.set_value("Selling Settings", "Selling Settings", "validate_selling_price", 1)
frappe.db.set_single_value("Selling Settings", "validate_selling_price", 1)

item = "Test Selling Price Validation"
make_item(item, {"is_stock_item": 1})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class TestPurchaseInvoice(unittest.TestCase, StockTestMixin):
@classmethod
def setUpClass(self):
unlink_payment_on_cancel_of_invoice()
frappe.db.set_value("Buying Settings", None, "allow_multiple_items", 1)
frappe.db.set_single_value("Buying Settings", "allow_multiple_items", 1)

@classmethod
def tearDownClass(self):
Expand Down Expand Up @@ -1232,9 +1232,7 @@ def test_gain_loss_with_advance_entry(self):
"Accounts Settings", "Accounts Settings", "unlink_payment_on_cancel_of_invoice"
)

frappe.db.set_value(
"Accounts Settings", "Accounts Settings", "unlink_payment_on_cancel_of_invoice", 1
)
frappe.db.set_single_value("Accounts Settings", "unlink_payment_on_cancel_of_invoice", 1)

original_account = frappe.db.get_value("Company", "_Test Company", "exchange_gain_loss_account")
frappe.db.set_value(
Expand Down Expand Up @@ -1369,8 +1367,8 @@ def test_gain_loss_with_advance_entry(self):
pay.reload()
pay.cancel()

frappe.db.set_value(
"Accounts Settings", "Accounts Settings", "unlink_payment_on_cancel_of_invoice", unlink_enabled
frappe.db.set_single_value(
"Accounts Settings", "unlink_payment_on_cancel_of_invoice", unlink_enabled
)
frappe.db.set_value("Company", "_Test Company", "exchange_gain_loss_account", original_account)

Expand Down
32 changes: 15 additions & 17 deletions erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,7 @@ def test_auto_write_off_amount(self):
self.assertEqual(pos.write_off_amount, 10)

def test_pos_with_no_gl_entry_for_change_amount(self):
frappe.db.set_value("Accounts Settings", None, "post_change_gl_entries", 0)
frappe.db.set_single_value("Accounts Settings", "post_change_gl_entries", 0)

make_pos_profile(
company="_Test Company with perpetual inventory",
Expand Down Expand Up @@ -1113,7 +1113,7 @@ def test_pos_with_no_gl_entry_for_change_amount(self):

self.validate_pos_gl_entry(pos, pos, 60, validate_without_change_gle=True)

frappe.db.set_value("Accounts Settings", None, "post_change_gl_entries", 1)
frappe.db.set_single_value("Accounts Settings", "post_change_gl_entries", 1)

def validate_pos_gl_entry(self, si, pos, cash_amount, validate_without_change_gle=False):
if validate_without_change_gle:
Expand Down Expand Up @@ -2452,7 +2452,7 @@ def test_inter_company_transaction_without_default_warehouse(self):
"Check mapping (expense account) of inter company SI to PI in absence of default warehouse."
# setup
old_negative_stock = frappe.db.get_single_value("Stock Settings", "allow_negative_stock")
frappe.db.set_value("Stock Settings", None, "allow_negative_stock", 1)
frappe.db.set_single_value("Stock Settings", "allow_negative_stock", 1)

old_perpetual_inventory = erpnext.is_perpetual_inventory_enabled("_Test Company 1")
frappe.local.enable_perpetual_inventory["_Test Company 1"] = 1
Expand Down Expand Up @@ -2506,7 +2506,7 @@ def test_inter_company_transaction_without_default_warehouse(self):

# tear down
frappe.local.enable_perpetual_inventory["_Test Company 1"] = old_perpetual_inventory
frappe.db.set_value("Stock Settings", None, "allow_negative_stock", old_negative_stock)
frappe.db.set_single_value("Stock Settings", "allow_negative_stock", old_negative_stock)

def test_sle_for_target_warehouse(self):
se = make_stock_entry(
Expand Down Expand Up @@ -2898,7 +2898,7 @@ def test_sales_invoice_against_supplier(self):
party_link = create_party_link("Supplier", supplier, customer)

# enable common party accounting
frappe.db.set_value("Accounts Settings", None, "enable_common_party_accounting", 1)
frappe.db.set_single_value("Accounts Settings", "enable_common_party_accounting", 1)

# create a sales invoice
si = create_sales_invoice(customer=customer, parent_cost_center="_Test Cost Center - _TC")
Expand All @@ -2925,7 +2925,7 @@ def test_sales_invoice_against_supplier(self):
self.assertEqual(jv[0], si.grand_total)

party_link.delete()
frappe.db.set_value("Accounts Settings", None, "enable_common_party_accounting", 0)
frappe.db.set_single_value("Accounts Settings", "enable_common_party_accounting", 0)

def test_payment_statuses(self):
from erpnext.accounts.doctype.payment_entry.test_payment_entry import get_payment_entry
Expand Down Expand Up @@ -3045,7 +3045,7 @@ def test_sales_commission(self):
self.assertRaises(frappe.ValidationError, si.save)

def test_sales_invoice_submission_post_account_freezing_date(self):
frappe.db.set_value("Accounts Settings", None, "acc_frozen_upto", add_days(getdate(), 1))
frappe.db.set_single_value("Accounts Settings", "acc_frozen_upto", add_days(getdate(), 1))
si = create_sales_invoice(do_not_save=True)
si.posting_date = add_days(getdate(), 1)
si.save()
Expand All @@ -3054,7 +3054,7 @@ def test_sales_invoice_submission_post_account_freezing_date(self):
si.posting_date = getdate()
si.submit()

frappe.db.set_value("Accounts Settings", None, "acc_frozen_upto", None)
frappe.db.set_single_value("Accounts Settings", "acc_frozen_upto", None)

def test_over_billing_case_against_delivery_note(self):
"""
Expand All @@ -3066,7 +3066,7 @@ def test_over_billing_case_against_delivery_note(self):
over_billing_allowance = frappe.db.get_single_value(
"Accounts Settings", "over_billing_allowance"
)
frappe.db.set_value("Accounts Settings", None, "over_billing_allowance", 0)
frappe.db.set_single_value("Accounts Settings", "over_billing_allowance", 0)

dn = create_delivery_note()
dn.submit()
Expand All @@ -3082,7 +3082,7 @@ def test_over_billing_case_against_delivery_note(self):

self.assertTrue("cannot overbill" in str(err.exception).lower())

frappe.db.set_value("Accounts Settings", None, "over_billing_allowance", over_billing_allowance)
frappe.db.set_single_value("Accounts Settings", "over_billing_allowance", over_billing_allowance)

def test_multi_currency_deferred_revenue_via_journal_entry(self):
deferred_account = create_account(
Expand Down Expand Up @@ -3121,7 +3121,7 @@ def test_multi_currency_deferred_revenue_via_journal_entry(self):
si.save()
si.submit()

frappe.db.set_value("Accounts Settings", None, "acc_frozen_upto", getdate("2019-01-31"))
frappe.db.set_single_value("Accounts Settings", "acc_frozen_upto", getdate("2019-01-31"))

pda1 = frappe.get_doc(
dict(
Expand Down Expand Up @@ -3166,7 +3166,7 @@ def test_multi_currency_deferred_revenue_via_journal_entry(self):
acc_settings.submit_journal_entries = 0
acc_settings.save()

frappe.db.set_value("Accounts Settings", None, "acc_frozen_upto", None)
frappe.db.set_single_value("Accounts Settings", "acc_frozen_upto", None)

def test_standalone_serial_no_return(self):
si = create_sales_invoice(
Expand Down Expand Up @@ -3216,9 +3216,7 @@ def test_gain_loss_with_advance_entry(self):
"Accounts Settings", "Accounts Settings", "unlink_payment_on_cancel_of_invoice"
)

frappe.db.set_value(
"Accounts Settings", "Accounts Settings", "unlink_payment_on_cancel_of_invoice", 1
)
frappe.db.set_single_value("Accounts Settings", "unlink_payment_on_cancel_of_invoice", 1)

jv = make_journal_entry("_Test Receivable USD - _TC", "_Test Bank - _TC", -7000, save=False)

Expand Down Expand Up @@ -3261,8 +3259,8 @@ def test_gain_loss_with_advance_entry(self):

check_gl_entries(self, si.name, expected_gle, nowdate())

frappe.db.set_value(
"Accounts Settings", "Accounts Settings", "unlink_payment_on_cancel_of_invoice", unlink_enabled
frappe.db.set_single_value(
"Accounts Settings", "unlink_payment_on_cancel_of_invoice", unlink_enabled
)

def test_batch_expiry_for_sales_invoice_return(self):
Expand Down
2 changes: 1 addition & 1 deletion erpnext/accounts/doctype/tax_rule/test_tax_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
class TestTaxRule(unittest.TestCase):
@classmethod
def setUpClass(cls):
frappe.db.set_value("Shopping Cart Settings", None, "enabled", 0)
frappe.db.set_single_value("Shopping Cart Settings", "enabled", 0)

@classmethod
def tearDownClass(cls):
Expand Down
2 changes: 1 addition & 1 deletion erpnext/assets/doctype/asset/test_asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1804,7 +1804,7 @@ def set_depreciation_settings_in_company(company=None):
company.save()

# Enable booking asset depreciation entry automatically
frappe.db.set_value("Accounts Settings", None, "book_asset_depreciation_entry_automatically", 1)
frappe.db.set_single_value("Accounts Settings", "book_asset_depreciation_entry_automatically", 1)


def enable_cwip_accounting(asset_category, enable=1):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,4 @@ def set_depreciation_settings_in_company():
company.save()

# Enable booking asset depreciation entry automatically
frappe.db.set_value("Accounts Settings", None, "book_asset_depreciation_entry_automatically", 1)
frappe.db.set_single_value("Accounts Settings", "book_asset_depreciation_entry_automatically", 1)
8 changes: 4 additions & 4 deletions erpnext/buying/doctype/purchase_order/test_purchase_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def test_ordered_qty_against_pi_with_update_stock(self):

frappe.db.set_value("Item", "_Test Item", "over_delivery_receipt_allowance", 0)
frappe.db.set_value("Item", "_Test Item", "over_billing_allowance", 0)
frappe.db.set_value("Accounts Settings", None, "over_billing_allowance", 0)
frappe.db.set_single_value("Accounts Settings", "over_billing_allowance", 0)

def test_update_remove_child_linked_to_mr(self):
"""Test impact on linked PO and MR on deleting/updating row."""
Expand Down Expand Up @@ -581,7 +581,7 @@ def test_ordered_qty_for_closing_po(self):
)

def test_group_same_items(self):
frappe.db.set_value("Buying Settings", None, "allow_multiple_items", 1)
frappe.db.set_single_value("Buying Settings", "allow_multiple_items", 1)
frappe.get_doc(
{
"doctype": "Purchase Order",
Expand Down Expand Up @@ -836,8 +836,8 @@ def test_internal_transfer_flow(self):
)
from erpnext.stock.doctype.delivery_note.delivery_note import make_inter_company_purchase_receipt

frappe.db.set_value("Selling Settings", None, "maintain_same_sales_rate", 1)
frappe.db.set_value("Buying Settings", None, "maintain_same_rate", 1)
frappe.db.set_single_value("Selling Settings", "maintain_same_sales_rate", 1)
frappe.db.set_single_value("Buying Settings", "maintain_same_rate", 1)

prepare_data_for_internal_transfer()
supplier = "_Test Internal Supplier 2"
Expand Down
4 changes: 2 additions & 2 deletions erpnext/buying/doctype/supplier/test_supplier.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def test_party_details_tax_category(self):
def test_serach_fields_for_supplier(self):
from erpnext.controllers.queries import supplier_query

frappe.db.set_value("Buying Settings", None, "supp_master_name", "Naming Series")
frappe.db.set_single_value("Buying Settings", "supp_master_name", "Naming Series")

supplier_name = create_supplier(supplier_name="Test Supplier 1").name

Expand Down Expand Up @@ -189,7 +189,7 @@ def test_serach_fields_for_supplier(self):
self.assertEqual(data[0].supplier_type, "Company")
self.assertTrue("supplier_type" in data[0])

frappe.db.set_value("Buying Settings", None, "supp_master_name", "Supplier Name")
frappe.db.set_single_value("Buying Settings", "supp_master_name", "Supplier Name")


def create_supplier(**args):
Expand Down
4 changes: 2 additions & 2 deletions erpnext/controllers/tests/test_subcontracting_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -1074,8 +1074,8 @@ def make_bom_for_subcontracted_items():


def set_backflush_based_on(based_on):
frappe.db.set_value(
"Buying Settings", None, "backflush_raw_materials_of_subcontract_based_on", based_on
frappe.db.set_single_value(
"Buying Settings", "backflush_raw_materials_of_subcontract_based_on", based_on
)


Expand Down
4 changes: 1 addition & 3 deletions erpnext/crm/doctype/opportunity/test_opportunity.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ def test_opportunity_item(self):
self.assertEqual(opportunity_doc.total, 2200)

def test_carry_forward_of_email_and_comments(self):
frappe.db.set_value(
"CRM Settings", "CRM Settings", "carry_forward_communication_and_comments", 1
)
frappe.db.set_single_value("CRM Settings", "carry_forward_communication_and_comments", 1)
lead_doc = make_lead()
lead_doc.add_comment("Comment", text="Test Comment 1")
lead_doc.add_comment("Comment", text="Test Comment 2")
Expand Down
2 changes: 1 addition & 1 deletion erpnext/e_commerce/shopping_cart/test_shopping_cart.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def test_cart_without_checkout_and_draft_quotation(self):

self.assertEqual(quote_doctstatus, 0)

frappe.db.set_value("E Commerce Settings", None, "save_quotations_as_draft", 0)
frappe.db.set_single_value("E Commerce Settings", "save_quotations_as_draft", 0)
frappe.local.shopping_cart_settings = None
update_cart("_Test Item", 1)
quote_name = request_for_quotation() # Request for Quote
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def tearDown(self):
frappe.delete_doc(doctype, d.name, force=True)

def test_plaid_disabled(self):
frappe.db.set_value("Plaid Settings", None, "enabled", 0)
frappe.db.set_single_value("Plaid Settings", "enabled", 0)
self.assertTrue(get_plaid_configuration() == "disabled")

def test_add_account_type(self):
Expand Down
Loading

0 comments on commit a3ea985

Please sign in to comment.