Skip to content

Commit

Permalink
feat(LANDA Member): bulk delete with password (LAN-848)
Browse files Browse the repository at this point in the history
  • Loading branch information
barredterra committed Oct 25, 2024
1 parent e8ac01d commit b7fc1ce
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
15 changes: 15 additions & 0 deletions landa/auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import frappe
from frappe.rate_limiter import rate_limit


@frappe.whitelist()
@rate_limit(limit=100, seconds=60 * 60)
def check_password(password: str) -> bool:
if not isinstance(password, str):
return False

try:
frappe.local.login_manager.check_password(frappe.session.user, password)
return True
except frappe.AuthenticationError:
return False
Original file line number Diff line number Diff line change
@@ -1,10 +1,87 @@
frappe.listview_settings["LANDA Member"] = {
onload(list_view) {
if (!frappe.user.has_role("System Manager")) {
// remove the default delete button
list_view.page.actions
.find(`[data-label='${encodeURIComponent(__("Delete"))}']`)
.closest("li")
.addClass("hidden");
.remove();

if (frappe.model.can_delete(doctype) && !frappe.model.has_workflow(doctype)) {
// add our own delete button with password prompt
list_view.page.add_actions_menu_item(
__("Delete", null, "Button in list view actions menu"),
() => {
const docnames = list_view.get_checked_items(true).map((docname) => docname.toString());

frappe.prompt(
[
{
fieldname: "password",
fieldtype: "Password",
label: __("Password"),
reqd: 1,
},
],
(values) => {
frappe
.xcall("landa.auth.check_password", { password: values.password })
.then((result) => {
if (!result) {
frappe.msgprint(__("Incorrect password"));
return;
}

list_view.disable_list_update = true;
bulk_delete(list_view.doctype, docnames, () => {
list_view.disable_list_update = false;
list_view.clear_checked_items();
list_view.refresh();
});
});
},
__("Delete {0} items permanently?", [docnames.length]),
__("Delete")
);
},
true
);
}
}
},
};

// copied from frappe/public/js/frappe/list/bulk_operations.js
function bulk_delete(doctype, docnames, done) {
frappe
.call({
method: "frappe.desk.reportview.delete_items",
freeze: true,
freeze_message:
docnames.length <= 10
? __("Deleting {0} records...", [docnames.length])
: null,
args: {
items: docnames,
doctype: doctype,
},
})
.then((r) => {
let failed = r.message;
if (!failed) {
failed = [];
}

if (failed.length && !r._server_messages) {
frappe.throw(
__("Cannot delete {0}", [failed.map((f) => f.bold()).join(", ")])
);
}
if (failed.length < docnames.length) {
frappe.utils.play_sound("delete");
if (done) {
done();
}
}
});
}

0 comments on commit b7fc1ce

Please sign in to comment.