mirror of
https://github.com/frappe/erpnext.git
synced 2026-08-14 15:11:52 +00:00
Repost Item Valuation and BOM Update Log cleared old logs with a raw delete on the parent table, orphaning timeline comments, versions, attachments and other reference records. Fixes #57237
85 lines
2.5 KiB
Python
85 lines
2.5 KiB
Python
## temp utility
|
|
|
|
from contextlib import contextmanager
|
|
|
|
import frappe
|
|
from frappe import _
|
|
from frappe.utils import create_batch, cstr
|
|
|
|
from erpnext.utilities.activation import get_level
|
|
|
|
LOG_REFERENCE_FIELDS = {
|
|
"Comment": ("reference_doctype", "reference_name"),
|
|
"Version": ("ref_doctype", "docname"),
|
|
"ToDo": ("reference_type", "reference_name"),
|
|
"DocShare": ("share_doctype", "share_name"),
|
|
"View Log": ("reference_doctype", "reference_name"),
|
|
"Document Follow": ("ref_doctype", "ref_docname"),
|
|
"Notification Log": ("document_type", "document_name"),
|
|
}
|
|
|
|
|
|
def clear_logs_with_references(doctype, filters):
|
|
names = frappe.get_all(doctype, filters=filters, pluck="name")
|
|
for batch in create_batch(names, 1000):
|
|
attached_files = frappe.get_all(
|
|
"File",
|
|
filters={"attached_to_doctype": doctype, "attached_to_name": ("in", batch)},
|
|
pluck="name",
|
|
)
|
|
if attached_files:
|
|
frappe.delete_doc("File", attached_files, ignore_permissions=True)
|
|
|
|
for reference_doctype, (doctype_field, name_field) in LOG_REFERENCE_FIELDS.items():
|
|
frappe.db.delete(reference_doctype, {doctype_field: doctype, name_field: ("in", batch)})
|
|
|
|
frappe.db.delete(doctype, {"name": ("in", batch)})
|
|
|
|
|
|
def update_doctypes():
|
|
df = frappe.qb.DocType("DocField")
|
|
dt_table = frappe.qb.DocType("DocType")
|
|
for d in (
|
|
frappe.qb.from_(df)
|
|
.inner_join(dt_table)
|
|
.on(df.parent == dt_table.name)
|
|
.select(df.parent, df.fieldname)
|
|
.where(df.fieldname.like("%description%") & (dt_table.istable == 1))
|
|
.run(as_dict=1)
|
|
):
|
|
dt = frappe.get_doc("DocType", d.parent)
|
|
|
|
for f in dt.fields:
|
|
if f.fieldname == d.fieldname and f.fieldtype in ("Text", "Small Text"):
|
|
f.fieldtype = "Text Editor"
|
|
dt.save()
|
|
break
|
|
|
|
|
|
def get_site_info(site_info):
|
|
# called via hook
|
|
company = frappe.db.get_single_value("Global Defaults", "default_company")
|
|
domain = None
|
|
|
|
if not company:
|
|
company = frappe.get_all("Company", order_by="creation asc", pluck="name")
|
|
company = company[0] if company else None
|
|
|
|
if company:
|
|
domain = frappe.get_cached_value("Company", cstr(company), "domain")
|
|
|
|
return {"company": company, "domain": domain, "activation": get_level(site_info)}
|
|
|
|
|
|
@contextmanager
|
|
def payment_app_import_guard():
|
|
marketplace_link = '<a href="https://frappecloud.com/marketplace/apps/payments">Marketplace</a>'
|
|
github_link = '<a href="https://github.com/frappe/payments/">GitHub</a>'
|
|
msg = _("payments app is not installed. Please install it from {0} or {1}").format(
|
|
marketplace_link, github_link
|
|
)
|
|
try:
|
|
yield
|
|
except ImportError:
|
|
frappe.throw(msg, title=_("Missing Payments App"))
|