-
Notifications
You must be signed in to change notification settings - Fork 23
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
feat: Prevent Attachment Deletion #69
base: version-15-hotfix
Are you sure you want to change the base?
Changes from all commits
333c46e
175182b
58929d6
f084f9b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import frappe | ||
|
||
|
||
def boot_session(bootinfo): | ||
bootinfo.sysdefaults.prevent_attachment_deletion = frappe.db.get_single_value( | ||
"ERPNext Germany Settings", "prevent_attachment_deletion" | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from typing import TYPE_CHECKING | ||
|
||
import frappe | ||
from frappe import _ | ||
|
||
if TYPE_CHECKING: | ||
from frappe.core.doctype.file import File | ||
|
||
APPLICABLE_DOCTYPES = ( | ||
"Quotation", | ||
"Sales Order", | ||
"Delivery Note", | ||
"Sales Invoice", | ||
"Request for Quotation", | ||
"Supplier Quotation", | ||
"Purchase Order", | ||
"Purchase Receipt", | ||
"Purchase Invoice", | ||
"Journal Entry", | ||
"Payment Entry", | ||
"Asset", | ||
) | ||
|
||
|
||
def on_trash(doc: "File", method: str): | ||
if doc.attached_to_doctype not in APPLICABLE_DOCTYPES: | ||
# Not applicable | ||
return | ||
|
||
docstatus = frappe.db.get_value( | ||
doc.attached_to_doctype, doc.attached_to_name, "docstatus" | ||
) | ||
if docstatus != 1: | ||
# Not submitted | ||
return | ||
Comment on lines
+33
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would extend this to docstatus 1 & 2 ( if docstatus == 0: ). If a document is cancelled, it does usually not matter if a file is attached or not. It is quite clear, that it is cancelled. On the other hand, it prevents the document from being deleted, which is of course a drawback (but in terms of GoBD better). For the case, that something was attached by accident, I would opt for a grace period of maybe 10 minutes after file attachment. That also allows to quickly fix a wrong upload to a submitted document. Possible solution for check if file can be deleted in docstatus 2. If the user has the permission to delete the document, he can delete the file. |
||
|
||
prevent_attachment_deletion = frappe.db.get_single_value( | ||
"ERPNext Germany Settings", "prevent_attachment_deletion" | ||
) | ||
if prevent_attachment_deletion: | ||
msg = _( | ||
"Attachments to the submitted record {0} of type {1} cannot be deleted." | ||
).format(doc.attached_to_name, _(doc.attached_to_doctype)) | ||
|
||
if "System Manager" in frappe.get_roles(): | ||
msg += " " + _("You can allow deletion via <b>ERPNext Germany Settings</b>.") | ||
|
||
frappe.throw(msg, title=_("Attachment Deletion Restricted")) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
import "./erpnext_germany/business_letter.js"; | ||
import "./erpnext_germany/prevent_attachment_deletion.js"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
$(document).ready(function () { | ||
$(document).on("form-load", function (event, frm) { | ||
if (!frappe.boot.sysdefaults.prevent_attachment_deletion || frappe.user.has_role("System Manager")) { | ||
return; | ||
} | ||
|
||
if (![ | ||
"Quotation", | ||
"Sales Order", | ||
"Delivery Note", | ||
"Sales Invoice", | ||
"Request for Quotation", | ||
"Supplier Quotation", | ||
"Purchase Order", | ||
"Purchase Receipt", | ||
"Purchase Invoice", | ||
"Journal Entry", | ||
"Payment Entry", | ||
"Asset", | ||
].includes(frm.doctype)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extend list if file.py is extended |
||
return; | ||
} | ||
|
||
frappe.ui.form.on(frm.doctype, { | ||
refresh: () => { | ||
$(".attachment-row .remove-btn").hide(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When uploading a new file in the submitted state, the list of attachment reloads and the "hide" is temporarily gone. Server side check still prevents from delete. Same principle when uploading a file in draft. One the site is reloaded the icon to "delete" is hidden, so the file cannot be removed client side from the document. Server side does not prevent deletion as intended. |
||
}, | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked with my accounting department and payroll team and identfied DocTypes that should be added: