def get_jenv(): import frappe from frappe.utils.safe_exec import get_safe_globals if not getattr(frappe.local, 'jenv', None): from jinja2 import DebugUndefined from jinja2.sandbox import SandboxedEnvironment # frappe will be loaded last, so app templates will get precedence jenv = SandboxedEnvironment( loader=get_jloader(), undefined=DebugUndefined ) set_filters(jenv) jenv.globals.update(get_safe_globals()) jenv.globals.update(get_jenv_customization('methods')) jenv.globals.update({ 'resolve_class': resolve_class, 'inspect': inspect, 'web_blocks': web_blocks, 'web_block': web_block, 'include_style': include_style }) frappe.local.jenv = jenv return frappe.local.jenv
def get_context(doc): return { "doc": doc, "nowdate": nowdate, "frappe": frappe._dict(utils=get_safe_globals().get("frappe").get("utils")) }
def execute(filters=None): doc = frappe.get_doc("Report", "Aptronics Profit and Loss Statement") data = [] month_start = parse_date("{} {}".format(filters.current_period, filters.fiscal_year)) month_start = month_start.replace(day=1) month_end = month_start.replace( day=(monthrange(month_start.year, month_start.month)[1]) ) fiscal_year_start = frappe.get_value("Fiscal Year", filters.fiscal_year, "year_start_date") for row in doc.report_rows: if not row.subtotal: current_period = get_total_for_account( filters.company, row.account, month_start, month_end ) year_to_date = get_total_for_account( filters.company, row.account, fiscal_year_start, month_end ) else: year_to_date = 0 current_period = 0 data.append(frappe._dict({ "account": "<strong> {} </strong>".format(row.row_label) if row.subtotal else row.account, "current_period": current_period, "year_to_date": year_to_date })) exec_globals = get_safe_globals() exec_globals.__builtins__['sum'] = __builtins__['sum'] exec_globals.__builtins__['format'] = __builtins__['format'] for row in doc.report_rows: if row.subtotal and row.formula: loc = {"filters": frappe._dict(filters), 'data': data} safe_exec(row.formula, None, loc) return get_columns(), data
def get_value_from_fieldname(field_map, fieldname_field, doc): field_name = get_source_value(field_map, fieldname_field) if field_name.startswith('eval:'): value = frappe.safe_eval(field_name[5:], get_safe_globals()) elif field_name[0] in ('"', "'"): value = field_name[1:-1] else: value = get_source_value(doc, field_name) return value
def get_autocompletion_items(self): """Generates a list of a autocompletion strings from the context dict that is used while executing a Server Script. Returns: list: Returns list of autocompletion items. For e.g., ["frappe.utils.cint", "frappe.db.get_all", ...] """ def get_keys(obj): out = [] for key in obj: if key.startswith('_'): continue value = obj[key] if isinstance(value, (NamespaceDict, dict)) and value: if key == 'form_dict': out.append(['form_dict', 7]) continue for subkey, score in get_keys(value): fullkey = f'{key}.{subkey}' out.append([fullkey, score]) else: if isinstance(value, type) and issubclass( value, Exception): score = 0 elif isinstance(value, ModuleType): score = 10 elif isinstance(value, (FunctionType, MethodType)): score = 9 elif isinstance(value, type): score = 8 elif isinstance(value, dict): score = 7 else: score = 6 out.append([key, score]) return out items = frappe.cache().get_value('server_script_autocompletion_items') if not items: items = get_keys(get_safe_globals()) items = [{'value': d[0], 'score': d[1]} for d in items] frappe.cache().set_value('server_script_autocompletion_items', items) return items
def get_jenv(): import frappe from frappe.utils.safe_exec import get_safe_globals if not getattr(frappe.local, 'jenv', None): from jinja2 import DebugUndefined from jinja2.sandbox import SandboxedEnvironment # frappe will be loaded last, so app templates will get precedence jenv = SandboxedEnvironment(loader = get_jloader(), undefined=DebugUndefined) set_filters(jenv) jenv.globals.update(get_safe_globals()) frappe.local.jenv = jenv return frappe.local.jenv
def get_context(doc): return {'doc': doc, 'utils': get_safe_globals().get('frappe').get('utils')}
def get_filters(self): if self.condition: return frappe.safe_eval(self.condition, get_safe_globals())
def test_safe_eval(self): self.assertEqual(frappe.safe_eval('1+1'), 2) self.assertRaises(AttributeError, frappe.safe_eval, 'frappe.utils.os.path', get_safe_globals())
def get_context(doc): return {"doc": doc, "utils": get_safe_globals().get("frappe").get("utils")}