seitime-frappe/frappe/utils/jinja_globals.py
Ankush Menat 4754ab71d1
perf(minor): remove unnecessary comprehensions
- remove several unnecessary comprehensions from functions that accept a generator.
- Using `[x for x in iter]` causes a list to be built first then passed to the outer function.
- `any` and `all` can take generator instead. This makes memory usage O(1) and actually makes these functions short-circuiting. E.g. if the first condition fails then `all` will immediately return false instead of evaluating all the entries.
- `sum`, `min`, `max` => memory usage become O(1)
- `list`, `set`, `.join()` => roughly halves memory usage, as list is not required to be built.
- lastly, it's two fewer characters to read/think about.
2021-05-12 20:38:18 +05:30

71 lines
1.6 KiB
Python

# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt
from __future__ import unicode_literals
from frappe.utils.jinja import get_jenv
import frappe
def resolve_class(classes):
if classes is None:
return ""
if isinstance(classes, str):
return classes
if isinstance(classes, (list, tuple)):
return " ".join(resolve_class(c) for c in classes).strip()
if isinstance(classes, dict):
return " ".join(classname for classname in classes if classes[classname]).strip()
return classes
def inspect(var, render=True):
context = {"var": var}
if render:
html = "<pre>{{ var | pprint | e }}</pre>"
else:
return ""
return get_jenv().from_string(html).render(context)
def web_block(template, values=None, **kwargs):
options = {"template": template, "values": values}
options.update(kwargs)
return web_blocks([options])
def web_blocks(blocks):
from frappe import throw, _dict
from frappe.website.doctype.web_page.web_page import get_web_blocks_html
web_blocks = []
for block in blocks:
if not block.get("template"):
throw("Web Template is not specified")
doc = _dict(
{
"doctype": "Web Page Block",
"web_template": block["template"],
"web_template_values": block.get("values", {}),
"add_top_padding": 1,
"add_bottom_padding": 1,
"add_container": 1,
"hide_block": 0,
"css_class": "",
}
)
doc.update(block)
web_blocks.append(doc)
out = get_web_blocks_html(web_blocks)
html = out.html
for script in out.scripts:
html += "<script>{}</script>".format(script)
return html