def config_exists(app, module): try: frappe.get_module("{app}.config.{module}".format(app=app, module=module)) return True except ImportError: return False
def validate(self): if not (self.python_module or self.connector_type): frappe.throw(_('Enter python module or select connector type')) if self.python_module: try: frappe.get_module(self.python_module) except: frappe.throw(frappe._('Invalid module path'))
def get_version(): "Show the versions of all the installed apps" from frappe.utils.change_log import get_app_branch frappe.init('') for m in sorted(frappe.get_all_apps()): branch_name = get_app_branch(m) module = frappe.get_module(m) app_hooks = frappe.get_module(m + ".hooks") if hasattr(app_hooks, '{0}_version'.format(branch_name)): print("{0} {1}".format(m, getattr(app_hooks, '{0}_version'.format(branch_name)))) elif hasattr(module, "__version__"): print("{0} {1}".format(m, module.__version__))
def update_controller_context(context, controller): module = frappe.get_module(controller) if module: # get config fields for prop in ("base_template_path", "template", "no_cache", "no_sitemap", "condition_field"): if hasattr(module, prop): context[prop] = getattr(module, prop) if hasattr(module, "get_context"): try: ret = module.get_context(context) if ret: context.update(ret) except frappe.Redirect: raise except (frappe.PermissionError, frappe.DoesNotExistError): raise except: if not frappe.flags.in_migrate: frappe.errprint(frappe.utils.get_traceback()) if hasattr(module, "get_children"): context.children = module.get_children(context)
def build_context(sitemap_options): """get_context method of doc or module is supposed to render content templates and push it into context""" context = frappe._dict(sitemap_options) context.update(get_website_settings()) # provide doc if context.doc: context.update(context.doc.as_dict()) if hasattr(context.doc, "get_context"): ret = context.doc.get_context(context) if ret: context.update(ret) for prop in ("no_cache", "no_sitemap"): if not prop in context: context[prop] = getattr(context.doc, prop, False) elif context.controller: module = frappe.get_module(context.controller) if module: if hasattr(module, "get_context"): ret = module.get_context(context) if ret: context.update(ret) if hasattr(module, "get_children"): context.children = module.get_children(context) add_metatags(context) if context.get("base_template_path") != context.get( "template") and not context.get("rendered"): context.data = render_blocks(context) return context
def make_asset_dirs(make_copy=False): assets_path = os.path.join(frappe.local.sites_path, "assets") for dir_path in [ os.path.join(assets_path, 'js'), os.path.join(assets_path, 'css')]: if not os.path.exists(dir_path): os.makedirs(dir_path) # symlink app/public > assets/app for app_name in frappe.get_all_apps(True): pymodule = frappe.get_module(app_name) app_base_path = os.path.abspath(os.path.dirname(pymodule.__file__)) symlinks = [] symlinks.append([os.path.join(app_base_path, 'public'), os.path.join(assets_path, app_name)]) symlinks.append([os.path.join(app_base_path, 'docs'), os.path.join(assets_path, app_name + '_docs')]) for source, target in symlinks: source = os.path.abspath(source) if not os.path.exists(target) and os.path.exists(source): if make_copy: shutil.copytree(source, target) else: os.symlink(source, target)
def update_controller_context(context, controller): module = frappe.get_module(controller) if module: # get config fields for prop in ("base_template_path", "template", "no_cache", "sitemap", "condition_field"): if hasattr(module, prop): context[prop] = getattr(module, prop) if hasattr(module, "get_context"): import inspect try: if inspect.getfullargspec(module.get_context).args: ret = module.get_context(context) else: ret = module.get_context() if ret: context.update(ret) except (frappe.PermissionError, frappe.PageDoesNotExistError, frappe.Redirect): raise except: if not any([ frappe.flags.in_migrate, frappe.flags.in_website_search_build ]): frappe.errprint(frappe.utils.get_traceback()) if hasattr(module, "get_children"): context.children = module.get_children(context)
def get_build_maps(): """get all build.jsons with absolute paths""" # framework js and css files pymodules = [frappe.get_module(app) for app in frappe.get_all_apps(True)] app_paths = [os.path.dirname(pymodule.__file__) for pymodule in pymodules] build_maps = {} for app_path in app_paths: path = os.path.join(app_path, 'public', 'build.json') if os.path.exists(path): with open(path) as f: try: for target, sources in json.loads(f.read()).iteritems(): # update app path source_paths = [] for source in sources: if isinstance(source, list): s = frappe.get_pymodule_path(source[0], *source[1].split("/")) else: s = os.path.join(app_path, source) source_paths.append(s) build_maps[target] = source_paths except Exception, e: print path raise
def get_hook_module(hook_name, report_name=None): """ check if there is a scriptlet hook for this report in frappe-bench/sites/site_name/jasper_hooks folder. The folder have the following structure where jasper_hooks is the root(package): jasper_hooks/report name/hook name.py Example: jasper_hooks/Table 1 Custom/jasper_scriptlet.py -> where Table 1 Custom is the name of report and jasper_scriptlet.py is the name of the hook. Note: All the folders must have __init__.py to make it a package This strucutre is to help development. There is no need to make a frappe app only to control reports. """ import os, sys make_jasper_hooks_path() jasper_absolute_path = os.path.realpath(frappe.local.site_path) if jasper_absolute_path not in sys.path: sys.path.insert(0, jasper_absolute_path) report_name = report_name + "." if report_name else "" module_path = "jasper_hooks_" + frappe.local.site.replace( ".", "_") + "." + report_name + hook_name try: hook_module = frappe.get_module(module_path) except: hook_module = None return hook_module
def get_hook_module(hook_name, report_name=None): """ check if there is a scriptlet hook for this report in frappe-bench/sites/site_name/jasper_hooks folder. The folder have the following structure where jasper_hooks is the root(package): jasper_hooks/report name/hook name.py Example: jasper_hooks/Table 1 Custom/jasper_scriptlet.py -> where Table 1 Custom is the name of report and jasper_scriptlet.py is the name of the hook. Note: All the folders must have __init__.py to make it a package This strucutre is to help development. There is no need to make a frappe app only to control reports. """ import os, sys make_jasper_hooks_path() jasper_absolute_path = os.path.realpath(frappe.local.site_path) if jasper_absolute_path not in sys.path: sys.path.insert(0, jasper_absolute_path) report_name = report_name + "." if report_name else "" module_path = "jasper_hooks_" + frappe.local.site.replace(".", "_") + "." + report_name + hook_name try: hook_module = frappe.get_module(module_path) except: hook_module = None return hook_module
def sync_for(app_name, force=0, sync_everything = False, verbose=False): files = [] if app_name == "frappe": # these need to go first at time of install for d in (("core", "docfield"), ("core", "docperm"), ("core", "doctype"), ("core", "user"), ("core", "role"), ("custom", "custom_field"), ("custom", "property_setter")): files.append(os.path.join(frappe.get_app_path("frappe"), d[0], "doctype", d[1], d[1] + ".json")) for module_name in frappe.local.app_modules.get(app_name) or []: folder = os.path.dirname(frappe.get_module(app_name + "." + module_name).__file__) get_doc_files(files, folder, force, sync_everything, verbose=verbose) l = len(files) if l: for i, doc_path in enumerate(files): import_file_by_path(doc_path, force=force) #print module_name + ' | ' + doctype + ' | ' + name frappe.db.commit() # show progress bar update_progress_bar("Updating {0}".format(app_name), i, l) print ""
def _add_test(app, path, filename, verbose, test_suite=None): import os, imp if os.path.sep.join(["doctype", "doctype", "boilerplate"]) in path: # in /doctype/doctype/boilerplate/ return app_path = frappe.get_pymodule_path(app) relative_path = os.path.relpath(path, app_path) if relative_path=='.': module_name = app else: module_name = '{app}.{relative_path}.{module_name}'.format(app=app, relative_path=relative_path.replace('/', '.'), module_name=filename[:-3]) module = frappe.get_module(module_name) if getattr(module, "selenium_tests", False) and not frappe.conf.run_selenium_tests: return if not test_suite: test_suite = unittest.TestSuite() if os.path.basename(os.path.dirname(path))=="doctype": txt_file = os.path.join(path, filename[5:].replace(".py", ".json")) with open(txt_file, 'r') as f: doc = json.loads(f.read()) doctype = doc["name"] make_test_records(doctype, verbose) test_suite.addTest(unittest.TestLoader().loadTestsFromModule(module))
def make_asset_dirs(make_copy=False): assets_path = os.path.join(frappe.local.sites_path, "assets") for dir_path in [ os.path.join(assets_path, 'js'), os.path.join(assets_path, 'css') ]: if not os.path.exists(dir_path): os.makedirs(dir_path) # symlink app/public > assets/app for app_name in frappe.get_all_apps(True): pymodule = frappe.get_module(app_name) app_base_path = os.path.abspath(os.path.dirname(pymodule.__file__)) symlinks = [] symlinks.append([ os.path.join(app_base_path, 'public'), os.path.join(assets_path, app_name) ]) symlinks.append([ os.path.join(app_base_path, 'docs'), os.path.join(assets_path, app_name + '_docs') ]) for source, target in symlinks: source = os.path.abspath(source) if not os.path.exists(target) and os.path.exists(source): if make_copy: shutil.copytree(source, target) else: os.symlink(source, target)
def build_context(sitemap_options): """get_context method of doc or module is supposed to render content templates and push it into context""" context = frappe._dict(sitemap_options) context.update(get_website_settings()) # provide doc if context.doc: context.update(context.doc.as_dict()) if hasattr(context.doc, "get_context"): context.update(context.doc.get_context(context) or {}) elif context.controller: module = frappe.get_module(context.controller) if module: if hasattr(module, "get_context"): context.update(module.get_context(context) or {}) if hasattr(module, "get_children"): context.get_children = module.get_children add_metatags(context) if context.get("base_template_path") != context.get( "template") and not context.get("rendered"): context.data = render_blocks(context) return context
def _add_test(app, path, filename, verbose, test_suite=None): import os if os.path.sep.join(["doctype", "doctype", "boilerplate"]) in path: # in /doctype/doctype/boilerplate/ return app_path = frappe.get_pymodule_path(app) relative_path = os.path.relpath(path, app_path) if relative_path == '.': module_name = app else: module_name = '{app}.{relative_path}.{module_name}'.format( app=app, relative_path=relative_path.replace('/', '.'), module_name=filename[:-3]) module = frappe.get_module(module_name) if getattr(module, "selenium_tests", False) and not frappe.conf.run_selenium_tests: return if not test_suite: test_suite = unittest.TestSuite() if os.path.basename(os.path.dirname(path)) == "doctype": txt_file = os.path.join(path, filename[5:].replace(".py", ".json")) with open(txt_file, 'r') as f: doc = json.loads(f.read()) doctype = doc["name"] make_test_records(doctype, verbose) test_suite.addTest(unittest.TestLoader().loadTestsFromModule(module))
def get_version(): "Show the versions of all the installed apps" frappe.init('') for m in sorted(frappe.get_all_apps()): module = frappe.get_module(m) if hasattr(module, "__version__"): print("{0} {1}".format(m, module.__version__))
def sync_for(app_name, force=0, sync_everything=False, verbose=False): files = [] if app_name == "frappe": # these need to go first at time of install for d in (("core", "docfield"), ("core", "docperm"), ("core", "doctype"), ("core", "user"), ("core", "role"), ("custom", "custom_field"), ("custom", "property_setter")): files.append( os.path.join(frappe.get_app_path("frappe"), d[0], "doctype", d[1], d[1] + ".json")) for module_name in frappe.local.app_modules.get(app_name) or []: folder = os.path.dirname( frappe.get_module(app_name + "." + module_name).__file__) get_doc_files(files, folder, force, sync_everything, verbose=verbose) l = len(files) if l: for i, doc_path in enumerate(files): import_file_by_path(doc_path, force=force) #print module_name + ' | ' + doctype + ' | ' + name frappe.db.commit() # show progress bar update_progress_bar("Updating {0}".format(app_name), i, l) print ""
def make_asset_dirs(make_copy=False, restore=False): # don't even think of making assets_path absolute - rm -rf ahead. assets_path = join_path(frappe.local.sites_path, "assets") for dir_path in [ join_path(assets_path, 'js'), join_path(assets_path, 'css')]: if not path_exists(dir_path): os.makedirs(dir_path) for app_name in frappe.get_all_apps(True): pymodule = frappe.get_module(app_name) app_base_path = abspath(os.path.dirname(pymodule.__file__)) symlinks = [] app_public_path = join_path(app_base_path, 'public') # app/public > assets/app symlinks.append([app_public_path, join_path(assets_path, app_name)]) # app/node_modules > assets/app/node_modules if path_exists(abspath(app_public_path)): symlinks.append([join_path(app_base_path, '..', 'node_modules'), join_path(assets_path, app_name, 'node_modules')]) app_doc_path = None if isdir(join_path(app_base_path, 'docs')): app_doc_path = join_path(app_base_path, 'docs') elif isdir(join_path(app_base_path, 'www', 'docs')): app_doc_path = join_path(app_base_path, 'www', 'docs') if app_doc_path: symlinks.append([app_doc_path, join_path(assets_path, app_name + '_docs')]) for source, target in symlinks: source = abspath(source) if path_exists(source): if restore: if path_exists(target): if os.path.islink(target): os.unlink(target) else: shutil.rmtree(target) shutil.copytree(source, target) elif make_copy: if path_exists(target): warnings.warn('Target {target} already exists.'.format(target = target)) else: shutil.copytree(source, target) else: if path_exists(target): if os.path.islink(target): os.unlink(target) else: shutil.rmtree(target) try: os.symlink(source, target) except OSError: print('Cannot link {} to {}'.format(source, target)) else: # warnings.warn('Source {source} does not exist.'.format(source = source)) pass
def setup_app_context(self): self.docs_config = frappe.get_module(self.app + ".config.docs") version = get_version(app=self.app) self.app_context = { "app": frappe._dict({ "name": self.app, "title": self.app_title, "description": self.hooks.get("app_description")[0], "version": version, "publisher": self.hooks.get("app_publisher")[0], "icon": self.hooks.get("app_icon")[0], "email": self.hooks.get("app_email")[0], "source_link": self.docs_config.source_link, "license": self.hooks.get("app_license")[0], "branch": getattr(self.docs_config, "branch", None) or "develop", }), "metatags": { "description": self.hooks.get("app_description")[0], }, "get_doctype_app": frappe.get_doctype_app }
def build_route(path): context = resolve_route(path) if not context: raise frappe.DoesNotExistError if context.controller: module = frappe.get_module(context.controller) # get sitemap config fields too for prop in ("base_template_path", "template", "no_cache", "no_sitemap", "condition_field"): if hasattr(module, prop): context[prop] = getattr(module, prop) context.doctype = context.ref_doctype context.title = context.page_title context.pathname = path # determine templates to be used if not context.base_template_path: app_base = frappe.get_hooks("base_template") context.base_template_path = app_base[ 0] if app_base else "templates/base.html" return context
def add_website_template(page_or_generator, app, path, fname, app_path): name = fname[:-5] if fname.endswith(".html") else fname wsc = frappe._dict( { "doctype": "Website Template", "page_or_generator": page_or_generator, "link_name": name, "template_path": os.path.relpath(os.path.join(path, fname), app_path), } ) wsc.controller = get_template_controller(app, path, fname) if wsc.controller: # verbose print wsc.controller module = frappe.get_module(wsc.controller) wsc.no_cache = getattr(module, "no_cache", 0) wsc.no_sitemap = wsc.no_cache or getattr(module, "no_sitemap", 0) wsc.no_sidebar = wsc.no_sidebar or getattr(module, "no_sidebar", 0) wsc.ref_doctype = getattr(module, "doctype", None) wsc.page_name_field = getattr(module, "page_name_field", "page_name") wsc.condition_field = getattr(module, "condition_field", None) wsc.sort_by = getattr(module, "sort_by", "name") wsc.sort_order = getattr(module, "sort_order", "asc") wsc.base_template_path = getattr(module, "base_template_path", None) wsc.page_title = getattr(module, "page_title", _(name.title())) if frappe.db.exists("Website Template", wsc.link_name): # found by earlier app, override frappe.db.sql("""delete from `tabWebsite Template` where name=%s""", (wsc.link_name,)) frappe.get_doc(wsc).insert() return name
def setup_app_context(self): self.docs_config = frappe.get_module(self.app + ".config.docs") version = self.hooks.get("app_version")[0] self.app_context = { "app": frappe._dict({ "name": self.app, "title": self.app_title, "description": self.hooks.get("app_description")[0], "version": version, "publisher": self.hooks.get("app_publisher")[0], "icon": self.hooks.get("app_icon")[0], "email": self.hooks.get("app_email")[0], "headline": self.docs_config.headline, "sub_heading": self.docs_config.sub_heading, "source_link": self.docs_config.source_link, "hide_install": getattr(self.docs_config, "hide_install", False), "docs_base_url": self.docs_config.docs_base_url, "long_description": markdown2.markdown(getattr(self.docs_config, "long_description", "")), "license": self.hooks.get("app_license")[0], "branch": getattr(self.docs_config, "branch", None) or "develop", "style": getattr(self.docs_config, "style", "") }), "metatags": { "description": self.hooks.get("app_description")[0], }, "get_doctype_app": frappe.get_doctype_app }
def __init__(self, doctype): test_module = frappe.get_module( "accounting.accounting." f"doctype.{doctype}." f"test_{doctype}" ) self.doctype = doctype self.doc_handler = test_module.DocHandler() self._test_records = test_module._test_records
def get_build_maps(): """get all build.jsons with absolute paths""" # framework js and css files pymodules = [frappe.get_module(app) for app in frappe.get_all_apps(True)] app_paths = [os.path.dirname(pymodule.__file__) for pymodule in pymodules] build_maps = {} for app_path in app_paths: path = os.path.join(app_path, 'public', 'build.json') if os.path.exists(path): with open(path) as f: try: for target, sources in json.loads(f.read()).iteritems(): # update app path source_paths = [] for source in sources: if isinstance(source, list): s = frappe.get_pymodule_path( source[0], *source[1].split("/")) else: s = os.path.join(app_path, source) source_paths.append(s) build_maps[target] = source_paths except Exception, e: print path raise
def add_website_template(page_or_generator, app, path, fname, app_path): name = fname[:-5] if fname.endswith(".html") else fname wsc = frappe._dict({ "doctype": "Website Template", "page_or_generator": page_or_generator, "link_name": name, "template_path": os.path.relpath(os.path.join(path, fname), app_path), }) wsc.controller = get_template_controller(app, path, fname) if wsc.controller: # verbose print wsc.controller module = frappe.get_module(wsc.controller) wsc.no_cache = getattr(module, "no_cache", 0) wsc.no_sitemap = wsc.no_cache or getattr(module, "no_sitemap", 0) wsc.no_sidebar = wsc.no_sidebar or getattr(module, "no_sidebar", 0) wsc.ref_doctype = getattr(module, "doctype", None) wsc.page_name_field = getattr(module, "page_name_field", "page_name") wsc.condition_field = getattr(module, "condition_field", None) wsc.sort_by = getattr(module, "sort_by", "name") wsc.sort_order = getattr(module, "sort_order", "asc") wsc.base_template_path = getattr(module, "base_template_path", None) wsc.page_title = getattr(module, "page_title", _(name.title())) if frappe.db.exists("Website Template", wsc.link_name): # found by earlier app, override frappe.db.sql("""delete from `tabWebsite Template` where name=%s""", (wsc.link_name,)) frappe.bean(wsc).insert() return name
def get_route_children(pathname, home_page=None): if not home_page: home_page = get_home_page() if pathname == home_page or not pathname: children = frappe.db.sql("""select url as name, label as page_title, 1 as public_read from `tabTop Bar Item` where parentfield='sidebar_items' order by idx""", as_dict=True) else: children = frappe.db.sql("""select * from `tabWebsite Route` where ifnull(parent_website_route,'')=%s and public_read=1 order by idx, page_title asc""", pathname, as_dict=True) if children: # if children are from generator and sort order is specified, then get that condition module = frappe.get_module(children[0].controller) if hasattr(module, "order_by"): children = frappe.db.sql("""select t1.* from `tabWebsite Route` t1, `tab{ref_doctype}` t2 where ifnull(t1.parent_website_route,'')=%s and t1.public_read=1 and t1.docname = t2.name order by {order_by}""".format(ref_doctype=children[0].ref_doctype, order_by=module.order_by), pathname, as_dict=True) children = [frappe.get_doc("Website Route", pathname)] + children return children
def get_config(app, module): """Load module info from `[app].config.[module]`.""" config = frappe.get_module("{app}.config.{module}".format(app=app, module=module)) config = config.get_data() sections = [s for s in config if s.get("condition", True)] disabled_reports = get_disabled_reports() has_report_permission = frappe.permissions.has_permission( 'Report', 'read', user=frappe.session.user, raise_exception=False) for section in sections: items = [] for item in section["items"]: if item["type"] == "report": if item["name"] in disabled_reports or not has_report_permission: continue # some module links might not have name if not item.get("name"): item["name"] = item.get("label") if not item.get("label"): item["label"] = _(item.get("name")) items.append(item) section['items'] = items return sections
def sync_pages(self): self.db.sql('truncate help') doc_contents = '<ol>' apps = os.listdir( '../apps' ) if self.global_help_setup else frappe.get_installed_apps() for app in apps: # Expect handling of cloning docs apps in bench docs_app = frappe.get_hooks('docs_app', app, app)[0] web_folder = 'www/' if docs_app != app else '' docs_folder = '../apps/{docs_app}/{docs_app}/{web_folder}docs/user'.format( docs_app=docs_app, web_folder=web_folder) self.out_base_path = '../apps/{docs_app}/{docs_app}/{web_folder}docs'.format( docs_app=docs_app, web_folder=web_folder) if os.path.exists(docs_folder): app_name = getattr(frappe.get_module(app), '__title__', None) or app.title() doc_contents += '<li><a data-path="/{app}/index">{app_name}</a></li>'.format( app=app, app_name=app_name) for basepath, folders, files in os.walk(docs_folder): files = self.reorder_files(files) for fname in files: if fname.rsplit('.', 1)[-1] in ('md', 'html'): fpath = os.path.join(basepath, fname) with io.open(fpath, 'r', encoding='utf-8') as f: try: content = frappe.render_template( f.read(), { 'docs_base_url': '/assets/{app}_docs'.format( app=app) }, safe_render=False) relpath = self.get_out_path(fpath) relpath = relpath.replace("user", app) content = frappe.utils.md_to_html(content) title = self.make_title( basepath, fname, content) intro = self.make_intro(content) content = self.make_content( content, fpath, relpath, app, docs_app) self.db.sql( '''insert into help(path, content, title, intro, full_path) values (%s, %s, %s, %s, %s)''', (relpath, content, title, intro, fpath)) except jinja2.exceptions.TemplateSyntaxError: print( "Invalid Jinja Template for {0}. Skipping" .format(fpath)) doc_contents += "</ol>" self.db.sql( '''insert into help(path, content, title, intro, full_path) values (%s, %s, %s, %s, %s)''', ('/documentation/index', doc_contents, 'Documentation', '', ''))
def run_unittest(doctype, verbose=False): module = frappe.db.get_value("DocType", doctype, "module") test_module = get_module_name(doctype, module, "test_") make_test_records(doctype, verbose=verbose) test_suite = unittest.TestSuite() module = frappe.get_module(test_module) test_suite.addTest(unittest.TestLoader().loadTestsFromModule(module)) return unittest.TextTestRunner(verbosity=1+(verbose and 1 or 0)).run(test_suite)
def get_doc_module(module, doctype, name): """Get custom module for given document""" module_name = "{app}.{module}.{doctype}.{name}.{name}".format( app=frappe.local.module_app[scrub(module)], doctype=scrub(doctype), module=scrub(module), name=scrub(name)) return frappe.get_module(module_name)
def setup(): global app_paths pymodules = [] for app in frappe.get_all_apps(True): try: pymodules.append(frappe.get_module(app)) except ImportError: pass app_paths = [os.path.dirname(pymodule.__file__) for pymodule in pymodules]
def build_context(context): """get_context method of doc or module is supposed to render content templates and push it into context""" context = frappe._dict(context) if not "url_prefix" in context: context.url_prefix = "" if context.url_prefix and context.url_prefix[-1] != '/': context.url_prefix += '/' context.update(get_website_settings()) context.update(frappe.local.conf.get("website_context") or {}) # provide doc if context.doc: context.update(context.doc.as_dict()) context.update(context.doc.website) if hasattr(context.doc, "get_context"): ret = context.doc.get_context(context) if ret: context.update(ret) for prop in ("no_cache", "no_sitemap"): if not prop in context: context[prop] = getattr(context.doc, prop, False) elif context.controller: module = frappe.get_module(context.controller) if module: # get config fields for prop in ("base_template_path", "template", "no_cache", "no_sitemap", "condition_field"): if hasattr(module, prop): context[prop] = getattr(module, prop) if hasattr(module, "get_context"): ret = module.get_context(context) if ret: context.update(ret) if hasattr(module, "get_children"): context.children = module.get_children(context) add_metatags(context) if context.show_sidebar: add_sidebar_data(context) # determine templates to be used if not context.base_template_path: app_base = frappe.get_hooks("base_template") context.base_template_path = app_base[ 0] if app_base else "templates/base.html" return context
def get_config(app, module): config = frappe.get_module("{app}.config.{module}".format(app=app, module=module)) config = deepcopy(config.get_data() if hasattr(config, "get_data") else config.data) for section in config: for item in section["items"]: if not "label" in item: item["label"] = _(item["name"]) return config
def get_installed_apps_info(): out = [] for app in frappe.get_installed_apps(): out.append({ 'app_name': app, 'version': getattr(frappe.get_module(app), '__version__', 'Unknown') }) return out
def build_context(context): """get_context method of doc or module is supposed to render content templates and push it into context""" context = frappe._dict(context) if not "url_prefix" in context: context.url_prefix = "" if context.url_prefix and context.url_prefix[-1]!='/': context.url_prefix += '/' context.update(get_website_settings()) context.update(frappe.local.conf.get("website_context") or {}) # provide doc if context.doc: context.update(context.doc.as_dict()) context.update(context.doc.website) if hasattr(context.doc, "get_context"): ret = context.doc.get_context(context) if ret: context.update(ret) for prop in ("no_cache", "no_sitemap"): if not prop in context: context[prop] = getattr(context.doc, prop, False) elif context.controller: module = frappe.get_module(context.controller) if module: # get config fields for prop in ("base_template_path", "template", "no_cache", "no_sitemap", "condition_field"): if hasattr(module, prop): context[prop] = getattr(module, prop) if hasattr(module, "get_context"): ret = module.get_context(context) if ret: context.update(ret) if hasattr(module, "get_children"): context.children = module.get_children(context) add_metatags(context) if context.show_sidebar: add_sidebar_data(context) # determine templates to be used if not context.base_template_path: app_base = frappe.get_hooks("base_template") context.base_template_path = app_base[0] if app_base else "templates/base.html" return context
def run_tests_for_doctype(doctype, verbose=False, tests=(), force=False): module = frappe.db.get_value("DocType", doctype, "module") test_module = get_module_name(doctype, module, "test_") if force: for name in frappe.db.sql_list("select name from `tab%s`" % doctype): frappe.delete_doc(doctype, name, force=True) make_test_records(doctype, verbose=verbose, force=force) module = frappe.get_module(test_module) return _run_unittest(module, verbose=verbose, tests=tests)
def get_config(app, module): config = frappe.get_module("{app}.config.{module}".format(app=app, module=module)) config = config.get_data() for section in config: for item in section["items"]: if not "label" in item: item["label"] = _(item["name"]) return config
def get_app_paths(): '''Creates a list of paths to all Python modules, based on known apps''' pymodules = [] for app in frappe.get_all_apps(True): try: pymodules.append(frappe.get_module(app)) except ImportError: pass return [os.path.dirname(pymodule.__file__) for pymodule in pymodules]
def get_doc_module(module, doctype, name): """Get custom module for given document""" module_name = "{app}.{module}.{doctype}.{name}.{name}".format( app = frappe.local.module_app[scrub(module)], doctype = scrub(doctype), module = scrub(module), name = scrub(name) ) return frappe.get_module(module_name)
def get_mapping_module(self, mapping_name): try: module_def = frappe.get_doc("Module Def", self.module) module = frappe.get_module('{app}.{module}.data_migration_mapping.{mapping_name}'.format( app= module_def.app_name, module=frappe.scrub(self.module), mapping_name=frappe.scrub(mapping_name) )) return module except ImportError: return None
def load_doctype_module(doctype, module=None, prefix=""): if not module: module = get_doctype_module(doctype) app = get_module_app(module) key = (app, doctype, prefix) if key not in doctype_python_modules: doctype_python_modules[key] = frappe.get_module(get_module_name(doctype, module, prefix)) return doctype_python_modules[key]
def make_asset_dirs(make_copy=False, restore=False): # don't even think of making assets_path absolute - rm -rf ahead. assets_path = os.path.join(frappe.local.sites_path, "assets") for dir_path in [ os.path.join(assets_path, 'js'), os.path.join(assets_path, 'css')]: if not os.path.exists(dir_path): os.makedirs(dir_path) # symlink app/public > assets/app for app_name in frappe.get_all_apps(True): pymodule = frappe.get_module(app_name) app_base_path = os.path.abspath(os.path.dirname(pymodule.__file__)) symlinks = [] symlinks.append([os.path.join(app_base_path, 'public'), os.path.join(assets_path, app_name)]) app_doc_path = None if os.path.isdir(os.path.join(app_base_path, 'docs')): app_doc_path = os.path.join(app_base_path, 'docs') elif os.path.isdir(os.path.join(app_base_path, 'www', 'docs')): app_doc_path = os.path.join(app_base_path, 'www', 'docs') if app_doc_path: symlinks.append([app_doc_path, os.path.join(assets_path, app_name + '_docs')]) for source, target in symlinks: source = os.path.abspath(source) if os.path.exists(source): if restore: if os.path.exists(target): if os.path.islink(target): os.unlink(target) else: shutil.rmtree(target) shutil.copytree(source, target) elif make_copy: if os.path.exists(target): warnings.warn('Target {target} already exists.'.format(target = target)) else: shutil.copytree(source, target) else: if os.path.exists(target): if os.path.islink(target): os.unlink(target) else: shutil.rmtree(target) os.symlink(source, target) else: # warnings.warn('Source {source} does not exist.'.format(source = source)) pass
def load_doctype_module(doctype, module=None, prefix="", suffix=""): """Returns the module object for given doctype.""" if not module: module = get_doctype_module(doctype) app = get_module_app(module) key = (app, doctype, prefix, suffix) if key not in doctype_python_modules: doctype_python_modules[key] = frappe.get_module(get_module_name(doctype, module, prefix, suffix)) return doctype_python_modules[key]
def run_tests_for_doctype(doctype, verbose=False, tests=(), force=False, profile=False): module = frappe.db.get_value("DocType", doctype, "module") if not module: print 'Invalid doctype {0}'.format(doctype) sys.exit(1) test_module = get_module_name(doctype, module, "test_") if force: for name in frappe.db.sql_list("select name from `tab%s`" % doctype): frappe.delete_doc(doctype, name, force=True) make_test_records(doctype, verbose=verbose, force=force) module = frappe.get_module(test_module) return _run_unittest(module, verbose=verbose, tests=tests, profile=profile)
def get_config(app, module): """Load module info from `[app].config.[module]`.""" config = frappe.get_module("{app}.config.{module}".format(app=app, module=module)) config = config.get_data() for section in config: for item in section["items"]: if item["type"]=="report" and frappe.db.get_value("Report", item["name"], "disabled")==1: section["items"].remove(item) continue if not "label" in item: item["label"] = _(item["name"]) return config
def get_improve_page_html(app_name, target): docs_config = frappe.get_module(app_name + ".config.docs") source_link = docs_config.source_link branch = getattr(docs_config, "branch", "develop") html = '''<div class="page-container"> <div class="page-content"> <div class="edit-container text-center"> <i class="fa fa-smile text-muted"></i> <a class="edit text-muted" href="{source_link}/blob/{branch}/{target}"> Improve this page </a> </div> </div> </div>'''.format(source_link=source_link, app_name=app_name, target=target, branch=branch) return html
def get_connection_class(python_module): filename = python_module.rsplit('.', 1)[-1] classname = frappe.unscrub(filename).replace(' ', '') module = frappe.get_module(python_module) raise_error = False if hasattr(module, classname): _class = getattr(module, classname) if not issubclass(_class, BaseConnection): raise_error = True else: raise_error = True if raise_error: raise ImportError(filename) return _class
def update_controller_context(context, controller): module = frappe.get_module(controller) if module: # get config fields for prop in ("base_template_path", "template", "no_cache", "no_sitemap", "condition_field"): if hasattr(module, prop): context[prop] = getattr(module, prop) if hasattr(module, "get_context"): ret = module.get_context(context) if ret: context.update(ret) if hasattr(module, "get_children"): context.children = module.get_children(context)
def sync_for(app_name, force=0, sync_everything = False, verbose=False): files = [] for module_name in frappe.local.app_modules.get(app_name) or []: folder = os.path.dirname(frappe.get_module(app_name + "." + module_name).__file__) files += get_doc_files(folder, force, sync_everything, verbose=verbose) l = len(files) if l: for i, doc_path in enumerate(files): import_file_by_path(doc_path, force=force) #print module_name + ' | ' + doctype + ' | ' + name frappe.db.commit() # show progress bar update_progress_bar("Updating {0}".format(app_name), i, l) print ""
def load_doctype_module(doctype, module=None, prefix="", suffix=""): """Returns the module object for given doctype.""" if not module: module = get_doctype_module(doctype) app = get_module_app(module) key = (app, doctype, prefix, suffix) module_name = get_module_name(doctype, module, prefix, suffix) try: if key not in doctype_python_modules: doctype_python_modules[key] = frappe.get_module(module_name) except ImportError as e: raise ImportError('Module import failed for {0} ({1})'.format(doctype, module_name + ' Error: ' + str(e))) return doctype_python_modules[key]
def sync_for(app_name, force=0, sync_everything = False, verbose=False, reset_permissions=False): files = [] if app_name == "frappe": # these need to go first at time of install for d in (("core", "docfield"), ("core", "docperm"), ("core", "has_role"), ("core", "doctype"), ("core", "user"), ("core", "role"), ("custom", "custom_field"), ("custom", "property_setter"), ("website", "web_form"), ("website", "web_form_field"), ("website", "portal_menu_item"), ("data_migration", "data_migration_mapping_detail"), ("data_migration", "data_migration_mapping"), ("data_migration", "data_migration_plan_mapping"), ("data_migration", "data_migration_plan")): files.append(os.path.join(frappe.get_app_path("frappe"), d[0], "doctype", d[1], d[1] + ".json")) for module_name in frappe.local.app_modules.get(app_name) or []: folder = os.path.dirname(frappe.get_module(app_name + "." + module_name).__file__) get_doc_files(files, folder, force, sync_everything, verbose=verbose) l = len(files) if l: for i, doc_path in enumerate(files): import_file_by_path(doc_path, force=force, ignore_version=True, reset_permissions=reset_permissions, for_sync=True) #print module_name + ' | ' + doctype + ' | ' + name frappe.db.commit() # show progress bar update_progress_bar("Updating DocTypes for {0}".format(app_name), i, l) # print each progress bar on new line print()