def get_script(report_name): report = get_report_doc(report_name) module = report.module or frappe.db.get_value("DocType", report.ref_doctype, "module") module_path = get_module_path(module) report_folder = os.path.join(module_path, "report", scrub(report.name)) script_path = os.path.join(report_folder, scrub(report.name) + ".js") print_path = os.path.join(report_folder, scrub(report.name) + ".html") script = None if os.path.exists(script_path): with open(script_path, "r") as f: script = f.read() html_format = get_html_format(print_path) if not script and report.javascript: script = report.javascript if not script: script = "frappe.query_reports['%s']={}" % report_name # load translations if frappe.lang != "en": send_translations(frappe.get_lang_dict("report", report_name)) return { "script": script, "html_format": html_format }
def get_script(report_name): report = get_report_doc(report_name) module = frappe.db.get_value("DocType", report.ref_doctype, "module") module_path = get_module_path(module) report_folder = os.path.join(module_path, "report", scrub(report.name)) script_path = os.path.join(report_folder, scrub(report.name) + ".js") script = None if os.path.exists(script_path): with open(script_path, "r") as script: script = script.read() if not script and report.javascript: script = report.javascript if not script: script = "frappe.query_reports['%s']={}" % report_name # load translations if frappe.lang != "en": frappe.response["__messages"] = frappe.get_lang_dict( "report", report_name) return script
def get_js(items): '''Load JS code files. Will also append translations and extend `frappe._messages` :param items: JSON list of paths of the js files to be loaded.''' items = json.loads(items) out = [] for src in items: src = src.strip("/").split("/") if ".." in src or src[0] != "assets": frappe.throw(_("Invalid file path: {0}").format("/".join(src))) contentpath = os.path.join(frappe.local.sites_path, *src) with open(contentpath, "r") as srcfile: code = frappe.utils.cstr(srcfile.read()) if frappe.local.lang != "en": messages = frappe.get_lang_dict("jsfile", contentpath) messages = json.dumps(messages) code += "\n\n$.extend(frappe._messages, {})".format(messages) out.append(code) return out
def get_roles_and_doctypes(): frappe.only_for("System Manager") send_translations(frappe.get_lang_dict("doctype", "DocPerm")) active_domains = frappe.get_active_domains() doctypes = frappe.get_all("DocType", filters={ "istable": 0, "name": ("not in", ",".join(not_allowed_in_permission_manager)), }, or_filters={ "ifnull(restrict_to_domain, '')": "", "restrict_to_domain": ("in", active_domains) }, fields=["name"]) roles = frappe.get_all("Role", filters={ "name": ("not in", "Administrator"), "disabled": 0, }, or_filters={ "ifnull(restrict_to_domain, '')": "", "restrict_to_domain": ("in", active_domains) }, fields=["name"]) doctypes_list = [ {"label":_(d.get("name")), "value":d.get("name")} for d in doctypes] roles_list = [ {"label":_(d.get("name")), "value":d.get("name")} for d in roles] return { "doctypes": sorted(doctypes_list, key=lambda d: d['label']), "roles": sorted(roles_list, key=lambda d: d['label']) }
def get_roles_and_doctypes(): frappe.only_for("System Manager") send_translations(frappe.get_lang_dict("doctype", "DocPerm")) active_domains = frappe.get_active_domains() doctypes = frappe.get_all("DocType", filters={ "istable": 0, "name": ("not in", "DocType"), }, or_filters={ "ifnull(restrict_to_domain, '')": "", "restrict_to_domain": ("in", active_domains) }, fields=["name"]) roles = frappe.get_all("Role", filters={ "name": ("not in", "Administrator"), "disabled": 0, }, or_filters={ "ifnull(restrict_to_domain, '')": "", "restrict_to_domain": ("in", active_domains) }, fields=["name"]) return { "doctypes": [d.get("name") for d in doctypes], "roles": [d.get("name") for d in roles] }
def get_roles_and_doctypes(): frappe.only_for("System Manager") send_translations(frappe.get_lang_dict("doctype", "DocPerm")) return { "doctypes": [d[0] for d in frappe.db.sql("""select name from `tabDocType` dt where istable=0 and name not in ('DocType')""")], "roles": [d[0] for d in frappe.db.sql("""select name from tabRole where name != 'Administrator' and disabled=0""")] }
def get_js(src): contentpath = os.path.join(frappe.local.sites_path, src) with open(contentpath, "r") as srcfile: code = srcfile.read() if frappe.local.lang != "en": code += "\n\n$.extend(frappe._messages, {})".format(json.dumps(\ frappe.get_lang_dict("jsfile", contentpath))) return code
def set_translations(self, lang): self.set("__messages", frappe.get_lang_dict("doctype", self.name)) # set translations for grid templates if self.get("__form_grid_templates"): for content in self.get("__form_grid_templates").values(): messages = extract_messages_from_code(content) messages = make_dict_from_messages(messages) self.get("__messages").update(messages, as_value=True)
def get_meta(doctype, cached=True): if cached: meta = frappe.cache().get_value("form_meta:" + doctype, lambda: FormMeta(doctype)) else: meta = FormMeta(doctype) if frappe.local.lang != 'en': meta.set("__messages", frappe.get_lang_dict("doctype", doctype)) return meta
def get_js(src): contentpath = os.path.join(frappe.local.sites_path, src) with open(contentpath, "r") as srcfile: code = frappe.utils.cstr(srcfile.read()) if frappe.local.lang != "en": messages = frappe.get_lang_dict("jsfile", contentpath) messages = json.dumps(messages) code += "\n\n$.extend(frappe._messages, {})".format(messages) return code
def load_translations(bootinfo): if frappe.local.lang != "en": messages = frappe.get_lang_dict("boot") bootinfo["lang"] = frappe.lang # load translated report names for name in bootinfo.user.all_reports: messages[name] = frappe._(name) bootinfo["__messages"] = messages
def get_roles_and_doctypes(): frappe.only_for("System Manager") send_translations(frappe.get_lang_dict("doctype", "DocPerm")) return { "doctypes": [d[0] for d in frappe.db.sql("""select name from `tabDocType` dt where ifnull(istable,0)=0 and name not in ('DocType') and exists(select * from `tabDocField` where parent=dt.name)""")], "roles": [d[0] for d in frappe.db.sql("""select name from tabRole where name not in ('Guest', 'Administrator')""")] }
def load_translations(bootinfo): if frappe.local.lang != 'en': messages = frappe.get_lang_dict("boot") bootinfo["lang"] = frappe.lang # load translated report names for name in bootinfo.user.all_reports: messages[name] = frappe._(name) bootinfo["__messages"] = messages
def getpage(): """ Load the page from `frappe.form` and send it via `frappe.response` """ page = frappe.form_dict.get('name') doc = get(page) # load translations if frappe.lang != "en": send_translations(frappe.get_lang_dict("page", page)) frappe.response.docs.append(doc)
def getpage(): """ Load the page from `frappe.form` and send it via `frappe.response` """ page = frappe.form_dict.get('name') doc = get(page) # load translations if frappe.lang != "en": frappe.response["__messages"] = frappe.get_lang_dict("page", page) frappe.response.docs.append(doc)
def getpage(): """ Load the page from `frappe.form` and send it via `frappe.response` """ page = frappe.form_dict.get("name") doc = get(page) # load translations if frappe.lang != "en": send_translations(frappe.get_lang_dict("page", page)) frappe.response.docs.append(doc)
def get_js(src): if src[0]=="/": src = src[1:] contentpath = os.path.join(frappe.local.sites_path, src) with open(contentpath, "r") as srcfile: code = frappe.utils.cstr(srcfile.read()) if frappe.local.lang != "en": messages = frappe.get_lang_dict("jsfile", contentpath) messages = json.dumps(messages) code += "\n\n$.extend(frappe._messages, {})".format(messages) return code
def get_roles_and_doctypes(): frappe.only_for("System Manager") send_translations(frappe.get_lang_dict("doctype", "DocPerm")) active_domains = frappe.get_active_domains() doctypes = frappe.get_all( "DocType", filters={ "istable": 0, "name": ("not in", ",".join(not_allowed_in_permission_manager)), }, or_filters={ "ifnull(restrict_to_domain, '')": "", "restrict_to_domain": ("in", active_domains) }, fields=["name"]) restricted_roles = ['Administrator'] if frappe.session.user != 'Administrator': custom_user_type_roles = frappe.get_all('User Type', filters={'is_standard': 0}, fields=['role']) for row in custom_user_type_roles: restricted_roles.append(row.role) restricted_roles.append('All') roles = frappe.get_all("Role", filters={ "name": ("not in", restricted_roles), "disabled": 0, }, or_filters={ "ifnull(restrict_to_domain, '')": "", "restrict_to_domain": ("in", active_domains) }, fields=["name"]) doctypes_list = [{ "label": _(d.get("name")), "value": d.get("name") } for d in doctypes] roles_list = [{ "label": _(d.get("name")), "value": d.get("name") } for d in roles] return { "doctypes": sorted(doctypes_list, key=lambda d: d['label']), "roles": sorted(roles_list, key=lambda d: d['label']) }
def get_roles_and_doctypes(): if frappe.local.site_path.split('/')[1] == 'demo.tailorpad.com' and frappe.session.user!='Administrator': return None frappe.only_for("System Manager") send_translations(frappe.get_lang_dict("doctype", "DocPerm")) return { "doctypes": [d[0] for d in frappe.db.sql("""select name from `tabDocType` dt where ifnull(istable,0)=0 and name not in ('DocType') and exists(select * from `tabDocField` where parent=dt.name)""")], "roles": [d[0] for d in frappe.db.sql("""select name from tabRole where name not in ('Guest', 'Administrator')""")] }
def load_translations(bootinfo): messages = frappe.get_lang_dict("boot") bootinfo["lang"] = frappe.lang # load translated report names for name in bootinfo.user.all_reports: messages[name] = frappe._(name) # only untranslated messages = {k:v for k, v in messages.iteritems() if k!=v} bootinfo["__messages"] = messages
def load_translations(bootinfo): messages = frappe.get_lang_dict("boot") bootinfo["lang"] = frappe.lang # load translated report names for name in bootinfo.user.all_reports: messages[name] = frappe._(name) # only untranslated messages = {k: v for k, v in iteritems(messages) if k != v} bootinfo["__messages"] = messages
def get_js(src): src = src.strip("/").split("/") if ".." in src: frappe.throw(_("Invalid file path: {0}").format("/".join(src))) contentpath = os.path.join(frappe.local.sites_path, *src) with open(contentpath, "r") as srcfile: code = frappe.utils.cstr(srcfile.read()) if frappe.local.lang != "en": messages = frappe.get_lang_dict("jsfile", contentpath) messages = json.dumps(messages) code += "\n\n$.extend(frappe._messages, {})".format(messages) return code
def get_roles_and_doctypes(): frappe.only_for("System Manager") send_translations(frappe.get_lang_dict("doctype", "DocPerm")) return { "doctypes": [ d[0] for d in frappe.db.sql("""select name from `tabDocType` dt where ifnull(istable,0)=0 and name not in ('DocType') and exists(select * from `tabDocField` where parent=dt.name)""") ], "roles": [ d[0] for d in frappe.db.sql( """select name from tabRole where name not in ('Guest', 'Administrator')""") ] }
def getpage(): """ Load the page from `frappe.form` and send it via `frappe.response` """ page = frappe.form_dict.get('name') doclist = get(page) if has_permission(doclist): # load translations if frappe.lang != "en": frappe.response["__messages"] = frappe.get_lang_dict("page", page) frappe.response['docs'] = doclist else: frappe.response['403'] = 1 raise frappe.PermissionError, 'No read permission for Page %s' % \ (doclist[0].title or page, )
def get_lang_dict(lang=None): if lang: # frappe.get_lang_dict uses this variable internally frappe.local.lang = lang messages = frappe.get_lang_dict("boot") # load translated report names for name in frappe.boot.get_allowed_reports(): messages[name] = frappe._(name) # only untranslated messages = {k: v for k, v in messages.items() if k != v} for hook in frappe.get_hooks("extend_translation"): frappe.get_attr(hook)(messages=messages) return messages
def get_roles_and_doctypes(): frappe.only_for("System Manager") send_translations(frappe.get_lang_dict("doctype", "DocPerm")) return { "doctypes": [ d[0] for d in frappe.db.sql("""select name from `tabDocType` dt where istable=0 and name not in ('DocType') and exists(select * from `tabDocField` where parent=dt.name) and exists(select * from `tabDocPerm` dp,`tabRole` role where dp.role = role.name and dp.parent=dt.name and not role.disabled)""" ) ], "roles": [ d[0] for d in frappe.db.sql("""select name from tabRole where name != 'Administrator' and disabled=0""") ] }
def get_script(report_name): report = get_report_doc(report_name) module = report.module or frappe.db.get_value("DocType", report.ref_doctype, "module") is_custom_module = frappe.get_cached_value("Module Def", module, "custom") # custom modules are virtual modules those exists in DB but not in disk. module_path = '' if is_custom_module else get_module_path(module) report_folder = module_path and os.path.join(module_path, "report", scrub(report.name)) script_path = report_folder and os.path.join(report_folder, scrub(report.name) + ".js") print_path = report_folder and os.path.join(report_folder, scrub(report.name) + ".html") script = None if os.path.exists(script_path): with open(script_path, "r") as f: script = f.read() script += f"\n\n//# sourceURL={scrub(report.name)}.js" html_format = get_html_format(print_path) if not script and report.javascript: script = report.javascript script += f"\n\n//# sourceURL={scrub(report.name)}__custom" if not script: script = "frappe.query_reports['%s']={}" % report_name # load translations if frappe.lang != "en": send_translations(frappe.get_lang_dict("report", report_name)) return { "script": render_include(script), "html_format": html_format, "execution_time": frappe.cache().hget("report_execution_time", report_name) or 0, }
def get_script(report_name): report = get_report_doc(report_name) module = frappe.db.get_value("DocType", report.ref_doctype, "module") module_path = get_module_path(module) report_folder = os.path.join(module_path, "report", scrub(report.name)) script_path = os.path.join(report_folder, scrub(report.name) + ".js") script = None if os.path.exists(script_path): with open(script_path, "r") as script: script = script.read() if not script and report.javascript: script = report.javascript if not script: script = "frappe.query_reports['%s']={}" % report_name # load translations if frappe.lang != "en": frappe.response["__messages"] = frappe.get_lang_dict("report", report_name) return script
def search(text, start=0, limit=20, doctype=""): """ Search for given text in __global_search :param text: phrase to be searched :param start: start results at, default 0 :param limit: number of results to return, default 20 :return: Array of result objects """ from frappe.desk.doctype.global_search_settings.global_search_settings import get_doctypes_for_global_search results = [] sorted_results = [] allowed_doctypes = get_doctypes_for_global_search() for text in set(text.split('&')): text = text.strip() if not text: continue conditions = '1=1' offset = '' mariadb_text = frappe.db.escape('+' + text + '*') mariadb_fields = '`doctype`, `name`, `content`, MATCH (`content`) AGAINST ({} IN BOOLEAN MODE) AS rank'.format( mariadb_text) postgres_fields = '`doctype`, `name`, `content`, TO_TSVECTOR("content") @@ PLAINTO_TSQUERY({}) AS rank'.format( frappe.db.escape(text)) values = {} if doctype: conditions = '`doctype` = %(doctype)s' values['doctype'] = doctype elif allowed_doctypes: conditions = '`doctype` IN %(allowed_doctypes)s' values['allowed_doctypes'] = tuple(allowed_doctypes) if int(start) > 0: offset = 'OFFSET {}'.format(start) common_query = """ SELECT {fields} FROM `__global_search` WHERE {conditions} ORDER BY rank DESC LIMIT {limit} {offset} """ result = frappe.db.multisql( { 'mariadb': common_query.format(fields=mariadb_fields, conditions=conditions, limit=limit, offset=offset), 'postgres': common_query.format(fields=postgres_fields, conditions=conditions, limit=limit, offset=offset) }, values=values, as_dict=True) results.extend(result) if frappe.flags.translated_doctypes is None: frappe.flags.translated_doctypes = [] # Faster for smaller lists # sort results based on allowed_doctype's priority for doctype in allowed_doctypes: for index, r in enumerate(results): if r.doctype == doctype and r.rank > 0.0: try: meta = frappe.get_meta(r.doctype) if meta.image_field: r.image = frappe.db.get_value(r.doctype, r.name, meta.image_field) if doctype not in frappe.flags.translated_doctypes: frappe.flags.translated_doctypes.append(doctype) send_translations( frappe.get_lang_dict("doctype", doctype)) except Exception: frappe.clear_messages() sorted_results.extend([r]) return sorted_results
def update_language(doclist): """update language""" if frappe.local.lang != 'en': doclist[0].fields["__messages"] = frappe.get_lang_dict( "doctype", doclist[0].name)
def load_translations(bootinfo): if frappe.local.lang != 'en': bootinfo["__messages"] = frappe.get_lang_dict("boot") bootinfo["lang"] = frappe.lang
def load_translations(bootinfo): frappe.set_user_lang(frappe.session.user) if frappe.lang != 'en': bootinfo["__messages"] = frappe.get_lang_dict("include") bootinfo["lang"] = frappe.lang