refactor(treewide): formatting and ruff fixes, + manually enabled F401

Signed-off-by: Akhil Narang <me@akhilnarang.dev>
This commit is contained in:
Akhil Narang
2024-03-27 11:37:26 +05:30
parent 8afb7790de
commit 3effaf21ef
574 changed files with 4135 additions and 6276 deletions

View File

@@ -143,7 +143,9 @@ def add_bank_accounts(response, bank, company):
result.append(new_account.name)
except frappe.UniqueValidationError:
frappe.msgprint(
_("Bank account {0} already exists and could not be created again").format(account["name"])
_("Bank account {0} already exists and could not be created again").format(
account["name"]
)
)
except Exception:
frappe.log_error("Plaid Link Error")
@@ -220,9 +222,7 @@ def sync_transactions(bank, bank_account):
f"Plaid added {len(result)} new Bank Transactions from '{bank_account}' between {start_date} and {end_date}"
)
frappe.db.set_value(
"Bank Account", bank_account, "last_integration_date", last_transaction_date
)
frappe.db.set_value("Bank Account", bank_account, "last_integration_date", last_transaction_date)
except Exception:
frappe.log_error(frappe.get_traceback(), _("Plaid transactions sync error"))
@@ -244,9 +244,7 @@ def get_transactions(bank, bank_account=None, start_date=None, end_date=None):
transactions = []
try:
transactions = plaid.get_transactions(
start_date=start_date, end_date=end_date, account_id=account_id
)
transactions = plaid.get_transactions(start_date=start_date, end_date=end_date, account_id=account_id)
except ItemError as e:
if e.code == "ITEM_LOGIN_REQUIRED":
msg = _("There was an error syncing transactions.") + " "

View File

@@ -65,10 +65,8 @@ class QuickBooksMigrator(Document):
# end: auto-generated types
def __init__(self, *args, **kwargs):
super(QuickBooksMigrator, self).__init__(*args, **kwargs)
self.oauth = OAuth2Session(
client_id=self.client_id, redirect_uri=self.redirect_url, scope=self.scope
)
super().__init__(*args, **kwargs)
self.oauth = OAuth2Session(client_id=self.client_id, redirect_uri=self.redirect_url, scope=self.scope)
if not self.authorization_url and self.authorization_endpoint:
self.authorization_url = self.oauth.authorization_url(self.authorization_endpoint)[0]
@@ -76,9 +74,7 @@ class QuickBooksMigrator(Document):
if self.company:
# We need a Cost Center corresponding to the selected erpnext Company
self.default_cost_center = frappe.db.get_value("Company", self.company, "cost_center")
company_warehouses = frappe.get_all(
"Warehouse", filters={"company": self.company, "is_group": 0}
)
company_warehouses = frappe.get_all("Warehouse", filters={"company": self.company, "is_group": 0})
if company_warehouses:
self.default_warehouse = company_warehouses[0].name
if self.authorization_endpoint:
@@ -226,14 +222,14 @@ class QuickBooksMigrator(Document):
if not frappe.db.exists(
{
"doctype": "Account",
"name": encode_company_abbr("{} - QB".format(root), self.company),
"name": encode_company_abbr(f"{root} - QB", self.company),
"company": self.company,
}
):
frappe.get_doc(
{
"doctype": "Account",
"account_name": "{} - QB".format(root),
"account_name": f"{root} - QB",
"root_type": root,
"is_group": "1",
"company": self.company,
@@ -245,13 +241,10 @@ class QuickBooksMigrator(Document):
def _migrate_entries(self, entity):
try:
query_uri = "{}/company/{}/query".format(
self.api_endpoint,
self.quickbooks_company_id,
)
query_uri = f"{self.api_endpoint}/company/{self.quickbooks_company_id}/query"
max_result_count = 1000
# Count number of entries
response = self._get(query_uri, params={"query": """SELECT COUNT(*) FROM {}""".format(entity)})
response = self._get(query_uri, params={"query": f"""SELECT COUNT(*) FROM {entity}"""})
entry_count = response.json()["QueryResponse"]["totalCount"]
# fetch pages and accumulate
@@ -273,9 +266,7 @@ class QuickBooksMigrator(Document):
def _fetch_general_ledger(self):
try:
query_uri = "{}/company/{}/reports/GeneralLedger".format(
self.api_endpoint, self.quickbooks_company_id
)
query_uri = f"{self.api_endpoint}/company/{self.quickbooks_company_id}/reports/GeneralLedger"
response = self._get(
query_uri,
params={
@@ -604,7 +595,9 @@ class QuickBooksMigrator(Document):
"is_stock_item": 0,
"item_group": "All Item Groups",
"company": self.company,
"item_defaults": [{"company": self.company, "default_warehouse": self.default_warehouse}],
"item_defaults": [
{"company": self.company, "default_warehouse": self.default_warehouse}
],
}
if "ExpenseAccountRef" in item:
expense_account = self._get_account_name_by_id(item["ExpenseAccountRef"]["value"])
@@ -644,7 +637,9 @@ class QuickBooksMigrator(Document):
try:
if preference["SalesFormsPrefs"]["AllowShipping"]:
default_shipping_account_id = preference["SalesFormsPrefs"]["DefaultShippingAccount"]
self.default_shipping_account = self._get_account_name_by_id(self, default_shipping_account_id)
self.default_shipping_account = self._get_account_name_by_id(
self, default_shipping_account_id
)
self.save()
except Exception as e:
self._log_error(e, preference)
@@ -752,7 +747,9 @@ class QuickBooksMigrator(Document):
"item_code": item["name"],
"conversion_factor": 1,
"uom": item["stock_uom"],
"description": line.get("Description", line["SalesItemLineDetail"]["ItemRef"]["name"]),
"description": line.get(
"Description", line["SalesItemLineDetail"]["ItemRef"]["name"]
),
"qty": line["SalesItemLineDetail"]["Qty"],
"price_list_rate": line["SalesItemLineDetail"]["UnitPrice"],
"cost_center": self.default_cost_center,
@@ -1024,7 +1021,11 @@ class QuickBooksMigrator(Document):
si_quickbooks_id = "Invoice - {}".format(linked_transaction["TxnId"])
# Invoice could have been saved as a Sales Invoice or a Journal Entry
if frappe.db.exists(
{"doctype": "Sales Invoice", "quickbooks_id": si_quickbooks_id, "company": self.company}
{
"doctype": "Sales Invoice",
"quickbooks_id": si_quickbooks_id,
"company": self.company,
}
):
sales_invoice = frappe.get_all(
"Sales Invoice",
@@ -1040,7 +1041,11 @@ class QuickBooksMigrator(Document):
party_account = sales_invoice["debit_to"]
if frappe.db.exists(
{"doctype": "Journal Entry", "quickbooks_id": si_quickbooks_id, "company": self.company}
{
"doctype": "Journal Entry",
"quickbooks_id": si_quickbooks_id,
"company": self.company,
}
):
journal_entry = frappe.get_doc(
"Journal Entry",
@@ -1050,9 +1055,9 @@ class QuickBooksMigrator(Document):
},
)
# Invoice saved as a Journal Entry must have party and party_type set on line containing Receivable Account
customer_account_line = list(
customer_account_line = next(
filter(lambda acc: acc.party_type == "Customer", journal_entry.accounts)
)[0]
)
reference_type = "Journal Entry"
reference_name = journal_entry.name
@@ -1094,7 +1099,11 @@ class QuickBooksMigrator(Document):
if linked_transaction["TxnType"] == "Bill":
pi_quickbooks_id = "Bill - {}".format(linked_transaction["TxnId"])
if frappe.db.exists(
{"doctype": "Purchase Invoice", "quickbooks_id": pi_quickbooks_id, "company": self.company}
{
"doctype": "Purchase Invoice",
"quickbooks_id": pi_quickbooks_id,
"company": self.company,
}
):
purchase_invoice = frappe.get_all(
"Purchase Invoice",
@@ -1229,7 +1238,9 @@ class QuickBooksMigrator(Document):
else:
accounts.append(
{
"account": self._get_account_name_by_id(line["DepositLineDetail"]["AccountRef"]["value"]),
"account": self._get_account_name_by_id(
line["DepositLineDetail"]["AccountRef"]["value"]
),
"credit_in_account_currency": line["Amount"],
"cost_center": self.default_cost_center,
}
@@ -1284,7 +1295,7 @@ class QuickBooksMigrator(Document):
return taxes
for line in entry["TxnTaxDetail"]["TaxLine"]:
tax_rate = line["TaxLineDetail"]["TaxRateRef"]["value"]
account_head = self._get_account_name_by_id("TaxRate - {}".format(tax_rate))
account_head = self._get_account_name_by_id(f"TaxRate - {tax_rate}")
tax_type_applicable = self._get_tax_type(tax_rate)
if tax_type_applicable == "TaxOnAmount":
taxes.append(
@@ -1333,7 +1344,7 @@ class QuickBooksMigrator(Document):
return tax_rate_detail["TaxRateRef"]["value"]
def _get_parent_row_id(self, tax_rate, taxes):
tax_account = self._get_account_name_by_id("TaxRate - {}".format(tax_rate))
tax_account = self._get_account_name_by_id(f"TaxRate - {tax_rate}")
for index, tax in enumerate(taxes):
if tax["account_head"] == tax_account:
return index + 1
@@ -1358,7 +1369,7 @@ class QuickBooksMigrator(Document):
def _get(self, *args, **kwargs):
kwargs["headers"] = {
"Accept": "application/json",
"Authorization": "Bearer {}".format(self.access_token),
"Authorization": f"Bearer {self.access_token}",
}
response = requests.get(*args, **kwargs)
# HTTP Status code 401 here means that the access_token is expired
@@ -1370,18 +1381,18 @@ class QuickBooksMigrator(Document):
return response
def _get_account_name_by_id(self, quickbooks_id):
return frappe.get_all(
"Account", filters={"quickbooks_id": quickbooks_id, "company": self.company}
)[0]["name"]
return frappe.get_all("Account", filters={"quickbooks_id": quickbooks_id, "company": self.company})[
0
]["name"]
def _publish(self, *args, **kwargs):
frappe.publish_realtime("quickbooks_progress_update", *args, **kwargs, user=self.modified_by)
def _get_unique_account_name(self, quickbooks_name, number=0):
if number:
quickbooks_account_name = "{} - {} - QB".format(quickbooks_name, number)
quickbooks_account_name = f"{quickbooks_name} - {number} - QB"
else:
quickbooks_account_name = "{} - QB".format(quickbooks_name)
quickbooks_account_name = f"{quickbooks_name} - QB"
company_encoded_account_name = encode_company_abbr(quickbooks_account_name, self.company)
if frappe.db.exists(
{"doctype": "Account", "name": company_encoded_account_name, "company": self.company}

View File

@@ -190,7 +190,7 @@ class TallyMigration(Document):
def get_children_and_parent_dict(accounts):
children, parents = {}, {}
for parent, account, is_group in accounts:
for parent, account, _is_group in accounts:
children.setdefault(parent, set()).add(account)
parents.setdefault(account, set()).add(parent)
parents[account].update(parents.get(parent, []))
@@ -235,7 +235,9 @@ class TallyMigration(Document):
{
"doctype": party_type,
"customer_name": account.NAME.string.strip(),
"tax_id": account.INCOMETAXNUMBER.string.strip() if account.INCOMETAXNUMBER else None,
"tax_id": account.INCOMETAXNUMBER.string.strip()
if account.INCOMETAXNUMBER
else None,
"customer_group": "All Customer Groups",
"territory": "All Territories",
"customer_type": "Individual",
@@ -249,7 +251,9 @@ class TallyMigration(Document):
{
"doctype": party_type,
"supplier_name": account.NAME.string.strip(),
"pan": account.INCOMETAXNUMBER.string.strip() if account.INCOMETAXNUMBER else None,
"pan": account.INCOMETAXNUMBER.string.strip()
if account.INCOMETAXNUMBER
else None,
"supplier_group": "All Supplier Groups",
"supplier_type": "Individual",
}
@@ -265,7 +269,9 @@ class TallyMigration(Document):
"address_line2": address[140:].strip(),
"country": account.COUNTRYNAME.string.strip() if account.COUNTRYNAME else None,
"state": account.LEDSTATENAME.string.strip() if account.LEDSTATENAME else None,
"gst_state": account.LEDSTATENAME.string.strip() if account.LEDSTATENAME else None,
"gst_state": account.LEDSTATENAME.string.strip()
if account.LEDSTATENAME
else None,
"pin_code": account.PINCODE.string.strip() if account.PINCODE else None,
"mobile": account.LEDGERPHONE.string.strip() if account.LEDGERPHONE else None,
"phone": account.LEDGERPHONE.string.strip() if account.LEDGERPHONE else None,
@@ -607,7 +613,7 @@ class TallyMigration(Document):
if new_year.year_start_date.year == new_year.year_end_date.year:
new_year.year = new_year.year_start_date.year
else:
new_year.year = "{}-{}".format(new_year.year_start_date.year, new_year.year_end_date.year)
new_year.year = f"{new_year.year_start_date.year}-{new_year.year_end_date.year}"
new_year.save()
oldest_year = new_year

View File

@@ -13,7 +13,9 @@ def validate_webhooks_request(doctype, hmac_key, secret_key="secret"):
if frappe.request and settings and settings.get(secret_key) and not frappe.flags.in_test:
sig = base64.b64encode(
hmac.new(settings.get(secret_key).encode("utf8"), frappe.request.data, hashlib.sha256).digest()
hmac.new(
settings.get(secret_key).encode("utf8"), frappe.request.data, hashlib.sha256
).digest()
)
if frappe.request.data and sig != bytes(frappe.get_request_header(hmac_key).encode()):
@@ -26,7 +28,7 @@ def validate_webhooks_request(doctype, hmac_key, secret_key="secret"):
def get_webhook_address(connector_name, method, exclude_uri=False, force_https=False):
endpoint = "erpnext.erpnext_integrations.connectors.{0}.{1}".format(connector_name, method)
endpoint = f"erpnext.erpnext_integrations.connectors.{connector_name}.{method}"
if exclude_uri:
return endpoint