feat(Dashboard Chart Source): delete files on trash (#25173)

Also refactored writing and reading of these files.

Co-authored-by: barredterra <14891507+barredterra@users.noreply.github.com>
This commit is contained in:
Vishnu VS 2024-03-11 00:15:56 +05:30 committed by GitHub
parent e96c5755d0
commit f1e8db80d5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,23 +1,22 @@
# Copyright (c) 2019, Frappe Technologies and contributors
# License: MIT. See LICENSE
import os
import shutil
from pathlib import Path
import frappe
from frappe import _
from frappe.model.document import Document
from frappe.modules import get_module_path, scrub
from frappe.modules.export_file import export_to_files
FOLDER_NAME = "dashboard_chart_source"
@frappe.whitelist()
def get_config(name):
doc = frappe.get_doc("Dashboard Chart Source", name)
with open(
os.path.join(
get_module_path(doc.module), "dashboard_chart_source", scrub(doc.name), scrub(doc.name) + ".js"
),
) as f:
return f.read()
def get_config(name: str) -> str:
doc: "DashboardChartSource" = frappe.get_doc("Dashboard Chart Source", name)
return doc.read_config()
class DashboardChartSource(Document):
@ -35,4 +34,28 @@ class DashboardChartSource(Document):
# end: auto-generated types
def on_update(self):
if not frappe.conf.developer_mode and not frappe.flags.in_migrate:
frappe.throw(_("Creation of this document is only permitted in developer mode."))
export_to_files(record_list=[[self.doctype, self.name]], record_module=self.module, create_init=True)
def on_trash(self):
if not frappe.conf.developer_mode and not frappe.flags.in_migrate:
frappe.throw(_("Deletion of this document is only permitted in developer mode."))
frappe.db.after_commit.add(self.delete_folder)
def read_config(self) -> str:
"""Return the config JS file for this dashboard chart source."""
config_path = self.get_folder_path() / f"{scrub(self.name)}.js"
return config_path.read_text() if config_path.exists() else ""
def delete_folder(self):
"""Delete the folder for this dashboard chart source."""
path = self.get_folder_path()
if path.exists():
shutil.rmtree(path, ignore_errors=True)
def get_folder_path(self) -> Path:
"""Return the path of the folder for this dashboard chart source."""
return Path(get_module_path(self.module)) / FOLDER_NAME / frappe.scrub(self.name)