Prevent user from deleting attachments?

I have a custom app I’ve built that will allow users to send PDFs to request signatures.

I have an attach field called signature_request_file for attaching the document that the signer will sign.

Obviously, it’s important that that attached file doesn’t get deleted (or else there’s no way for the signer to sign it).

I need a way to prevent users from deleting certain files.

Ideally I’d use a hook to check to make sure that the file the user is trying to delete isn’t attached to any Signature Request documents.

I DO NOT want to have to restrict users from deleting files altogether, that would be asinine, because there are only certain files I need to be able to prevent the deletion of.

I’d assume that Frappe has a way to handle this, and I just haven’t found it yet, but I’m afraid there is not. If that’s the case, that is very… stupid… Preventing users from unwittingly deleting important files seems like a pretty basic feature…

you can use on_trash method as explained in this :arrow_up: and add your conditions in it

Create a hook to encounter this issue using before_delete method. You can prevent the deletion of specific files attached to Signature Request documents.

def before_delete(doc, method):
    if doc.doctype == "File" and doc.file_url:
        if frappe.db.exists("Signature Request", {"signature_request_file": doc.file_url}):
            frappe.throw(_("Cannot delete file attached to a Signature Request document."))

hook.py:

doc_events = {
    "File": {
        "before_delete": "your_custom_app.module_name.file_hooks.before_delete"
    }
}