fix(server_scripts): warn about restricted context (#16900)

Server script UX is bad when you don't know what's whitelisted and what isn't... this often requires executing-rewrite-execute cycle for trivial things.

So instead, on saving just compile server script and immediately give warnings for whatever that can be caught at compile time.
This commit is contained in:
Ankush Menat 2022-05-16 13:34:25 +05:30 committed by GitHub
parent 4b13d6d7e4
commit a9d6147999
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,7 +2,6 @@
# Copyright (c) 2019, Frappe Technologies and contributors
# License: MIT. See LICENSE
import ast
from types import FunctionType, MethodType, ModuleType
from typing import Dict, List
@ -17,6 +16,7 @@ class ServerScript(Document):
frappe.only_for("Script Manager", True)
self.sync_scheduled_jobs()
self.clear_scheduled_events()
self.check_if_compilable_in_restricted_context()
def on_update(self):
frappe.cache().delete_value("server_script_map")
@ -60,6 +60,15 @@ class ServerScript(Document):
for scheduled_job in self.scheduled_jobs:
frappe.delete_doc("Scheduled Job Type", scheduled_job.name)
def check_if_compilable_in_restricted_context(self):
"""Check compilation errors and send them back as warnings."""
from RestrictedPython import compile_restricted
try:
compile_restricted(self.script)
except Exception as e:
frappe.msgprint(str(e), title=_("Compilation warning"))
def execute_method(self) -> Dict:
"""Specific to API endpoint Server Scripts