8000 feat: hook `ignore_links_on_delete` to skip doctypes on delete by DaizyModi · Pull Request #19347 · frappe/frappe · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: hook ignore_links_on_delete to skip doctypes on delete #19347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions frappe/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,21 @@
"frappe.core.doctype.file.file.move_file": "frappe.core.api.file.move_file",
"frappe.core.doctype.file.file.zip_files": "frappe.core.api.file.zip_files",
}

ignore_links_on_delete = [
"Communication",
"ToDo",
"DocShare",
"Email Unsubscribe",
"Activity Log",
"File",
"Version",
"Document Follow",
"Comment",
"View Log",
"Tag Link",
"Notification Log",
"Email Queue",
"Document Share Key",
"Integration Request",
]
24 changes: 4 additions & 20 deletions frappe/model/delete_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,6 @@
from frappe.utils.global_search import delete_for_document
from frappe.utils.password import delete_all_passwords_for

doctypes_to_skip = (
"Communication",
"ToDo",
"DocShare",
"Email Unsubscribe",
"Activity Log",
"File",
"Version",
"Document Follow",
"Comment",
"View Log",
"Tag Link",
"Notification Log",
"Email Queue",
"Document Share Key",
"Integration Request",
)


def delete_doc(
doctype=None,
Expand Down Expand Up @@ -277,7 +259,7 @@ def check_if_doc_is_linked(doc, method="Delete"):
item_parent = getattr(item, "parent", None)
linked_doctype = item.parenttype if item_parent else link_dt

if linked_doctype in doctypes_to_skip or (
if linked_doctype in frappe.get_hooks("ignore_links_on_delete") or (
linked_doctype in ignore_linked_doctypes and method == "Cancel"
):
# don't check for communication and todo!
Expand Down Expand Up @@ -306,7 +288,9 @@ def check_if_doc_is_dynamically_linked(doc, method="Delete"):

ignore_linked_doctypes = doc.get("ignore_linked_doctypes") or []

if df.parent in doctypes_to_skip or (df.parent in ignore_linked_doctypes and method == "Cancel"):
if df.parent in frappe.get_hooks("ignore_links_on_delete") or (
df.parent in ignore_linked_doctypes and method == "Cancel"
):
# don't check for communication and todo!
continue

Expand Down
36 changes: 36 additions & 0 deletions frappe/tests/test_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,42 @@ def test_has_permission(self):
address.flags.dont_touch_me = True
self.assertFalse(frappe.has_permission("Address", doc=address, user=username))

def test_ignore_links_on_delete(self):
email_unsubscribe = frappe.get_doc(
{"doctype": "Email Unsubscribe", "email": "test@example.com", "global_unsubscribe": 1}
).insert()

event = frappe.get_doc(
{
"doctype": "Event",
"subject": "Test Event",
"starts_on": "2022-12-21",
"event_type": "Public",
"event_participants": [
{
"reference_doctype": "Email Unsubscribe",
"reference_docname": email_unsubscribe.name,
}
],
}
).insert()
self.assertRaises(frappe.LinkExistsError, email_unsubscribe.delete)

event.event_participants = []
event.save()

todo = frappe.get_doc(
{
"doctype": "ToDo",
"description": "Test ToDo",
"reference_type": "Event",
"reference_name": event.name,
}
)
todo.insert()

event.delete()


def custom_has_permission(doc, ptype, user):
if doc.flags.dont_touch_me:
Expand Down
5 changes: 5 additions & 0 deletions frappe/utils/boilerplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,11 @@ def _create_github_workflow_files(dest, hooks):
#
# auto_cancel_exempted_doctypes = ["Auto Repeat"]

# Ignore links to specified DocTypes when deleting documents
# -----------------------------------------------------------

# ignore_links_on_delete = ["Communication", "ToDo"]


# User Data Protection
# --------------------
Expand Down
0