def get_items(doctype, txt, limit_start=0): meta = frappe.get_meta(doctype) filters, or_filters = [], [] out = frappe._dict() module = load_doctype_module(doctype) if txt: if meta.search_fields: for f in meta.get_search_fields(): or_filters.append([doctype, f.strip(), "like", "%" + txt + "%"]) else: filters.append([doctype, "name", "like", "%" + txt + "%"]) out.raw_items = frappe.get_list(doctype, fields = ["*"], filters=filters, or_filters = or_filters, limit_start=limit_start, limit_page_length = 20) if hasattr(module, "get_list_item"): out["items"] = [] for i in out.raw_items: i.doc = i out["items"].append(module.get_list_item(i)) else: template = Template("""<div><a href="/{{ doctype }}/{{ doc.name }}" no-pjax> {{ doc[title_field] }}</a></div>""") out.items = [template.render(doc=i, doctype=doctype, title_field = meta.title_field or "name") for i in out.raw_items] out.meta = meta return out
def get_communication_doctype(doctype, txt, searchfield, start, page_len, filters): user_perms = frappe.utils.user.UserPermissions(frappe.session.user) user_perms.build_permissions() can_read = user_perms.can_read from frappe.modules import load_doctype_module com_doctypes = [] if len(txt) < 2: for name in frappe.get_hooks("communication_doctypes"): try: module = load_doctype_module(name, suffix='_dashboard') if hasattr(module, 'get_data'): for i in module.get_data()['transactions']: com_doctypes += i["items"] except ImportError: pass else: com_doctypes = [ d[0] for d in frappe.db.get_values("DocType", { "issingle": 0, "istable": 0, "hide_toolbar": 0 }) ] out = [] for dt in com_doctypes: if txt.lower().replace("%", "") in dt.lower() and dt in can_read: out.append([dt]) return out
def _get_linked_doctypes(doctype): ret = {} # find fields where this doctype is linked ret.update(get_linked_fields(doctype)) ret.update(get_dynamic_linked_fields(doctype)) # find links of parents links = frappe.db.sql("""select dt from `tabCustom Field` where (fieldtype="Table" and options=%s)""", (doctype)) links += frappe.db.sql("""select parent from tabDocField where (fieldtype="Table" and options=%s)""", (doctype)) for dt, in links: if not dt in ret: ret[dt] = {"get_parent": True} for dt in list(ret): try: doctype_module = load_doctype_module(dt) except ImportError: # in case of Custom DocType continue if getattr(doctype_module, "exclude_from_linked_with", False): del ret[dt] return ret
def get_list_context(context, doctype): from frappe.modules import load_doctype_module module = load_doctype_module(doctype) if hasattr(module, "get_list_context"): return frappe._dict(module.get_list_context(context) or {}) return frappe._dict()
def get_list_context(context, doctype, web_form_name=None): from frappe.modules import load_doctype_module list_context = context or frappe._dict() meta = frappe.get_meta(doctype) def update_context_from_module(module, list_context): # call the user defined method `get_list_context` # from the python module if hasattr(module, "get_list_context"): out = frappe._dict(module.get_list_context(list_context) or {}) if out: list_context = out return list_context # get context from the doctype module if not meta.custom: # custom doctypes don't have modules module = load_doctype_module(doctype) list_context = update_context_from_module(module, list_context) # get context from web form module if web_form_name: web_form = frappe.get_doc('Web Form', web_form_name) list_context = update_context_from_module( web_form.get_web_form_module(), list_context) # get path from '/templates/' folder of the doctype if not list_context.row_template: list_context.row_template = meta.get_row_template() return list_context
def get_list_context(context, doctype): from frappe.modules import load_doctype_module from frappe.website.doctype.web_form.web_form import get_web_form_list list_context = context or frappe._dict() meta = frappe.get_meta(doctype) if not meta.custom: # custom doctypes don't have modules module = load_doctype_module(doctype) if hasattr(module, "get_list_context"): out = frappe._dict(module.get_list_context(list_context) or {}) if out: list_context = out # get path from '/templates/' folder of the doctype if not list_context.row_template: list_context.row_template = meta.get_row_template() # is web form, show the default web form filters # which is only the owner if frappe.form_dict.web_form_name: list_context.web_form_name = frappe.form_dict.web_form_name if not list_context.get("get_list"): list_context.get_list = get_web_form_list if not frappe.flags.web_form: # update list context from web_form frappe.flags.web_form = frappe.get_doc('Web Form', frappe.form_dict.web_form_name) if frappe.flags.web_form.is_standard: frappe.flags.web_form.update_list_context(list_context) return list_context
def get_controller(doctype): """Returns the **class** object of the given DocType. For `custom` type, returns `frappe.model.document.Document`. :param doctype: DocType name as string.""" from frappe.model.document import Document from frappe.utils.nestedset import NestedSet global _classes if not doctype in _classes: module_name, custom = frappe.db.get_value("DocType", doctype, ("module", "custom"), cache=True) \ or ["Core", False] if custom: if frappe.db.field_exists(doctype, "is_tree"): is_tree = frappe.db.get_value("DocType", doctype, ("is_tree"), cache=True) else: is_tree = False _class = NestedSet if is_tree else Document else: module = load_doctype_module(doctype, module_name) classname = doctype.replace(" ", "").replace("-", "") if hasattr(module, classname): _class = getattr(module, classname) if issubclass(_class, BaseDocument): _class = getattr(module, classname) else: raise ImportError(doctype) else: raise ImportError(doctype) _classes[doctype] = _class return _classes[doctype]
def get_controller(doctype): """Returns the **class** object of the given DocType. For `custom` type, returns `frappe.model.document.Document`. :param doctype: DocType name as string.""" from frappe.model.document import Document if not doctype in _classes: module_name, custom = frappe.db.get_value("DocType", doctype, ["module", "custom"]) \ or ["Core", False] if custom: _class = Document else: module = load_doctype_module(doctype, module_name) classname = doctype.replace(" ", "").replace("-", "") if hasattr(module, classname): _class = getattr(module, classname) if issubclass(_class, BaseDocument): _class = getattr(module, classname) else: raise ImportError, doctype else: raise ImportError, doctype _classes[doctype] = _class return _classes[doctype]
def get_controller(doctype): """Returns the **class** object of the given DocType. For `custom` type, returns `frappe.model.document.Document`. :param doctype: DocType name as string.""" from frappe.model.document import Document global _classes if not doctype in _classes: module_name, custom = frappe.db.get_value("DocType", doctype, ["module", "custom"]) \ or ["Core", False] if custom: _class = Document else: module = load_doctype_module(doctype, module_name) classname = doctype.replace(" ", "").replace("-", "") if hasattr(module, classname): _class = getattr(module, classname) if issubclass(_class, BaseDocument): _class = getattr(module, classname) else: raise ImportError(doctype) else: raise ImportError(doctype) _classes[doctype] = _class return _classes[doctype]
def get_list_context(context, doctype): from frappe.modules import load_doctype_module from frappe.website.doctype.web_form.web_form import get_web_form_list list_context = frappe._dict() module = load_doctype_module(doctype) if hasattr(module, "get_list_context"): list_context = frappe._dict(module.get_list_context(context) or {}) # is web form, show the default web form filters # which is only the owner if frappe.form_dict.web_form_name: list_context.web_form_name = frappe.form_dict.web_form_name if not list_context.get("get_list"): list_context.get_list = get_web_form_list if not frappe.flags.web_form: # update list context from web_form frappe.flags.web_form = frappe.get_doc( 'Web Form', frappe.form_dict.web_form_name) if frappe.flags.web_form.is_standard: frappe.flags.web_form.update_list_context(list_context) return list_context
def _get_linked_doctypes(doctype, without_ignore_user_permissions_enabled=False): ret = {} # find fields where this doctype is linked ret.update(get_linked_fields(doctype, without_ignore_user_permissions_enabled)) ret.update(get_dynamic_linked_fields(doctype, without_ignore_user_permissions_enabled)) filters=[['fieldtype', 'in', frappe.model.table_fields], ['options', '=', doctype]] if without_ignore_user_permissions_enabled: filters.append(['ignore_user_permissions', '!=', 1]) # find links of parents links = frappe.get_all("DocField", fields=["parent as dt"], filters=filters) links+= frappe.get_all("Custom Field", fields=["dt"], filters=filters) for dt, in links: if dt in ret: continue ret[dt] = {"get_parent": True} for dt in list(ret): try: doctype_module = load_doctype_module(dt) except (ImportError, KeyError): # in case of Custom DocType # or in case of module rename eg. (Schools -> Education) continue if getattr(doctype_module, "exclude_from_linked_with", False): del ret[dt] return ret
def _get_linked_doctypes(doctype): ret = {} # find fields where this doctype is linked ret.update(get_linked_fields(doctype)) ret.update(get_dynamic_linked_fields(doctype)) # find links of parents links = frappe.db.sql("""select dt from `tabCustom Field` where (fieldtype="Table" and options=%s)""", (doctype)) links += frappe.db.sql("""select parent from tabDocField where (fieldtype="Table" and options=%s)""", (doctype)) for dt, in links: if not dt in ret: ret[dt] = {"get_parent": True} for dt in ret.keys(): try: doctype_module = load_doctype_module(dt) except ImportError: # in case of Custom DocType continue if getattr(doctype_module, "exclude_from_linked_with", False): del ret[dt] return ret
def _get_linked_doctypes(doctype, without_ignore_user_permissions_enabled=False): ret = {} # find fields where this doctype is linked ret.update(get_linked_fields(doctype, without_ignore_user_permissions_enabled)) ret.update(get_dynamic_linked_fields(doctype, without_ignore_user_permissions_enabled)) filters=[['fieldtype','=','Table'], ['options', '=', doctype]] if without_ignore_user_permissions_enabled: filters.append(['ignore_user_permissions', '!=', 1]) # find links of parents links = frappe.get_all("DocField", fields=["parent as dt"], filters=filters) links+= frappe.get_all("Custom Field", fields=["dt"], filters=filters) for dt, in links: if dt in ret: continue ret[dt] = {"get_parent": True} for dt in list(ret): try: doctype_module = load_doctype_module(dt) except (ImportError, KeyError): # in case of Custom DocType # or in case of module rename eg. (Schools -> Education) continue if getattr(doctype_module, "exclude_from_linked_with", False): del ret[dt] return ret
def get_dashboard_data(self): '''Returns dashboard setup related to this doctype. This method will return the `data` property in the `[doctype]_dashboard.py` file in the doctype folder''' data = frappe._dict() try: module = load_doctype_module(self.name, suffix='_dashboard') dashboard_method = frappe.get_hooks('override_dashboard_data', {}).get(self.name) if dashboard_method: import importlib method_splitted = dashboard_method[0].split('.') last = method_splitted.pop() path = '.'.join(method_splitted) m = importlib.import_module(path) data = frappe._dict(getattr(m, last)()) elif hasattr(module, 'get_data'): data = frappe._dict(module.get_data()) except ImportError: pass return data
def get_list_context(context, doctype): from frappe.modules import load_doctype_module from frappe.website.doctype.web_form.web_form import get_web_form_list list_context = context or frappe._dict() meta = frappe.get_meta(doctype) if not meta.custom: # custom doctypes don't have modules module = load_doctype_module(doctype) if hasattr(module, "get_list_context"): out = frappe._dict(module.get_list_context(list_context) or {}) if out: list_context = out # get path from '/templates/' folder of the doctype if not list_context.row_template: list_context.row_template = meta.get_row_template() # is web form, show the default web form filters # which is only the owner if frappe.form_dict.web_form_name: list_context.web_form_name = frappe.form_dict.web_form_name if not list_context.get("get_list"): list_context.get_list = get_web_form_list if not frappe.flags.web_form: # update list context from web_form frappe.flags.web_form = frappe.get_doc( 'Web Form', frappe.form_dict.web_form_name) if frappe.flags.web_form.is_standard: frappe.flags.web_form.update_list_context(list_context) return list_context
def get_communication_doctype(doctype, txt, searchfield, start, page_len, filters): from frappe.modules import load_doctype_module com_doctypes = [] if len(txt) < 2: for name in ["Customer", "Supplier"]: try: module = load_doctype_module(name, suffix='_dashboard') if hasattr(module, 'get_data'): for i in module.get_data()['transactions']: com_doctypes += i["items"] except ImportError: pass else: com_doctypes = [ d[0] for d in frappe.db.get_values("DocType", { "issingle": 0, "istable": 0, "hide_toolbar": 0 }) ] filtered_doctypes = tuple([ v for v in com_doctypes if re.search(txt + ".*", _(v), re.IGNORECASE) ]) allowed_doctypes = frappe.permissions.get_doctypes_with_read() valid_doctypes = sorted( set(filtered_doctypes).intersection(set(allowed_doctypes))) valid_doctypes = [[doctype] for doctype in valid_doctypes] return valid_doctypes
def is_condition_field_enabled(self): self.controller_module = load_doctype_module(self.doctype) if hasattr(self.controller_module, "condition_field"): return self.get( self.controller_module.condition_field) and True or False else: return True
def load_templates(self): module = load_doctype_module(self.name) app = module.__name__.split(".")[0] templates = {} if hasattr(module, "form_grid_templates"): for key, path in module.form_grid_templates.iteritems(): templates[key] = get_html_format(frappe.get_app_path(app, path)) self.set("__form_grid_templates", templates)
def get_modules(doctype): module = frappe.db.get_value("DocType", doctype, "module") try: test_module = load_doctype_module(doctype, module, "test_") if test_module: reload(test_module) except ImportError: test_module = None return module, test_module
def create_database_views(): from frappe.modules import load_doctype_module module = load_doctype_module("Loan Charges", "loans") frappe.flags.in_install = False if hasattr(module, "on_doctype_update"): getattr(module, "on_doctype_update")() frappe.flags.in_install = "fimax"
def load_templates(self): if not self.custom: module = load_doctype_module(self.name) app = module.__name__.split(".")[0] templates = {} if hasattr(module, "form_grid_templates"): for key, path in module.form_grid_templates.iteritems(): templates[key] = get_html_format(frappe.get_app_path(app, path)) self.set("__form_grid_templates", templates)
def get_links_setup(self): '''Returns setup for documents related to this doctype. This method will return the `links_setup` property in the `[doctype]_links.py` file in the doctype folder''' try: module = load_doctype_module(self.name, suffix='_links') return frappe._dict(module.links) except ImportError: return frappe._dict()
def load_form_grid_templates(self): module = load_doctype_module(self.name) app = module.__name__.split(".")[0] templates = {} if hasattr(module, "form_grid_templates"): for key, path in module.form_grid_templates.iteritems(): with open(frappe.get_app_path(app, path), "r") as f: templates[key] = f.read() self.set("__form_grid_templates", templates)
def get_dashboard_data(self): '''Returns dashboard setup related to this doctype. This method will return the `data` property in the `[doctype]_dashboard.py` file in the doctype folder''' try: module = load_doctype_module(self.name, suffix='_dashboard') data = frappe._dict(module.data) except ImportError: data = frappe._dict() return data
def get_dashboard_data(self): """Returns dashboard setup related to this doctype. This method will return the `data` property in the `[doctype]_dashboard.py` file in the doctype folder""" try: module = load_doctype_module(self.name, suffix="_dashboard") data = frappe._dict(module.data) except ImportError: data = frappe._dict() return data
def get_versions_data(doctype): try: data = [] module = load_doctype_module(doctype, suffix='_version') if hasattr(module, 'get_data'): data = module.get_data() except ImportError: return [] return data
def get_dashboard_data(self): '''Returns dashboard setup related to this doctype. This method will return the `data` property in the `[doctype]_dashboard.py` file in the doctype folder''' data = frappe._dict() try: module = load_doctype_module(self.name, suffix='_dashboard') if hasattr(module, 'get_data'): data = frappe._dict(module.get_data()) except ImportError: pass return data
def get_controller(doctype): if not doctype in _classes: module = load_doctype_module(doctype) classname = doctype.replace(" ", "").replace("-", "") if hasattr(module, classname): _class = getattr(module, classname) if issubclass(_class, Document): _class = getattr(module, classname) else: raise ImportError, doctype else: raise ImportError, doctype _classes[doctype] = _class return _classes[doctype]
def get_sync_generators(app): generators = [] for doctype in frappe.get_hooks("website_generators", app_name=app): condition, order_by = "", "name asc" module = load_doctype_module(doctype) if hasattr(module, "condition_field"): condition = " where ifnull({0}, 0)=1 ".format( module.condition_field) if hasattr(module, "order_by"): order_by = module.order_by for name in frappe.db.sql_list( "select name from `tab{0}` {1} order by {2}".format( doctype, condition, order_by)): generators.append((doctype, name)) return generators
def get_list_context(context, doctype): from frappe.modules import load_doctype_module from frappe.website.doctype.web_form.web_form import get_web_form_list list_context = frappe._dict() module = load_doctype_module(doctype) if hasattr(module, "get_list_context"): list_context = frappe._dict(module.get_list_context(context) or {}) # is web form if cint(frappe.local.form_dict.is_web_form): list_context.is_web_form = 1 if not list_context.get("get_list"): list_context.get_list = get_web_form_list return list_context
def get_seal_doc_and_version(doc): try: data = [] version = None module = load_doctype_module(doc.doctype, suffix='_version') if hasattr(module, 'get_data'): data = module.get_data() version = module.DOCTYPE_VERSION except (ImportError, AttributeError): """ If the versionning has not been configured, no seal will be recorded. """ return None return get_sealed_doc(doc, data, version)
def get_controller(doctype): """Returns the **class** object of the given DocType. For `custom` type, returns `frappe.model.document.Document`. :param doctype: DocType name as string.""" from frappe.model.document import Document from frappe.utils.nestedset import NestedSet global _classes if not doctype in _classes: module_name, custom = frappe.db.get_value("DocType", doctype, ("module", "custom"), cache=True) \ or ["Core", False] if custom: if frappe.db.field_exists("DocType", "is_tree"): is_tree = frappe.db.get_value("DocType", doctype, "is_tree", cache=True) else: is_tree = False _class = NestedSet if is_tree else Document else: #PJOB: replacement === class_overrides = frappe.get_hooks('override_doctype_class') if class_overrides and class_overrides.get(doctype): import_path = class_overrides[doctype][-1] module_path, classname = import_path.rsplit('.', 1) module = frappe.get_module(module_path) if not hasattr(module, classname): raise ImportError( '{0}: {1} does not exist in module {2}'.format( doctype, classname, module_path)) else: module = load_doctype_module(doctype, module_name) classname = doctype.replace(" ", "").replace("-", "") if hasattr(module, classname): _class = getattr(module, classname) if issubclass(_class, BaseDocument): _class = getattr(module, classname) else: raise ImportError(doctype) else: raise ImportError(doctype) return _class
def on_update(self): from frappe.model.db_schema import updatedb updatedb(self.name) self.change_modified_of_parent() make_module_and_roles(self) from frappe import conf if (not frappe.flags.in_import) and conf.get('developer_mode') or 0: self.export_doc() self.make_controller_template() # update index if not getattr(self, "custom", False): from frappe.modules import load_doctype_module module = load_doctype_module(self.name, self.module) if hasattr(module, "on_doctype_update"): module.on_doctype_update() frappe.clear_cache(doctype=self.name)
def get_dashboard_data(self): '''Returns dashboard setup related to this doctype. This method will return the `data` property in the `[doctype]_dashboard.py` file in the doctype's folder, along with any overrides or extensions implemented in other Frappe applications via hooks. ''' data = frappe._dict() try: module = load_doctype_module(self.name, suffix='_dashboard') if hasattr(module, 'get_data'): data = frappe._dict(module.get_data()) except ImportError: pass for hook in frappe.get_hooks("override_doctype_dashboards", {}).get(self.name, []): data = frappe.get_attr(hook)(data=data) return data
def get_items(doctype, txt, limit_start=0): meta = frappe.get_meta(doctype) filters, or_filters = [], [] out = frappe._dict() module = load_doctype_module(doctype) if txt: if meta.search_fields: for f in meta.get_search_fields(): or_filters.append( [doctype, f.strip(), "like", "%" + txt + "%"]) else: filters.append([doctype, "name", "like", "%" + txt + "%"]) out.raw_items = frappe.get_list(doctype, fields=["*"], filters=filters, or_filters=or_filters, limit_start=limit_start, limit_page_length=20) if hasattr(module, "get_list_item"): out["items"] = [] for i in out.raw_items: i.doc = i out["items"].append(module.get_list_item(i)) else: template = Template( """<div><a href="/{{ doctype }}/{{ doc.name }}" no-pjax> {{ doc[title_field] }}</a></div>""") out.items = [ template.render(doc=i, doctype=doctype, title_field=meta.title_field or "name") for i in out.raw_items ] out.meta = meta return out
def _get_controller(): from frappe.model.document import Document from frappe.utils.nestedset import NestedSet module_name, custom = frappe.db.get_value( "DocType", doctype, ("module", "custom"), cache=True) or ["Core", False] if custom: if frappe.db.field_exists("DocType", "is_tree"): is_tree = frappe.db.get_value("DocType", doctype, "is_tree", cache=True) else: is_tree = False _class = NestedSet if is_tree else Document else: class_overrides = frappe.get_hooks('override_doctype_class') if class_overrides and class_overrides.get(doctype): import_path = class_overrides[doctype][-1] module_path, classname = import_path.rsplit('.', 1) module = frappe.get_module(module_path) if not hasattr(module, classname): raise ImportError( '{0}: {1} does not exist in module {2}'.format( doctype, classname, module_path)) else: module = load_doctype_module(doctype, module_name) classname = doctype.replace(" ", "").replace("-", "") if hasattr(module, classname): _class = getattr(module, classname) if issubclass(_class, BaseDocument): _class = getattr(module, classname) else: raise ImportError(doctype) else: raise ImportError(doctype) return _class
def on_update(self): """Update database schema, make controller templates if `custom` is not set and clear cache.""" from frappe.model.db_schema import updatedb updatedb(self.name) self.change_modified_of_parent() make_module_and_roles(self) from frappe import conf if not (frappe.flags.in_import or frappe.flags.in_test) and conf.get('developer_mode') or 0: self.export_doc() self.make_controller_template() # update index if not getattr(self, "custom", False): from frappe.modules import load_doctype_module module = load_doctype_module(self.name, self.module) if hasattr(module, "on_doctype_update"): module.on_doctype_update() delete_notification_count_for(doctype=self.name) frappe.clear_cache(doctype=self.name)
def get_communication_doctype(doctype, txt, searchfield, start, page_len, filters): user_perms = frappe.utils.user.UserPermissions(frappe.session.user) user_perms.build_permissions() can_read = user_perms.can_read from frappe.modules import load_doctype_module com_doctypes = [] if len(txt)<3: for name in ["Customer", "Supplier"]: try: module = load_doctype_module(name, suffix='_dashboard') if hasattr(module, 'get_data'): for i in module.get_data()['transactions']: com_doctypes += i["items"] except ImportError: pass else: com_doctypes = [d[0] for d in frappe.db.get_values("DocType", {"issingle": 0, "istable": 0, "hide_toolbar": 0})] out = [] for dt in com_doctypes: if txt.lower().replace("%", "") in dt.lower() and dt in can_read: out.append([dt]) return out
def get_list_context(context, doctype): from frappe.modules import load_doctype_module from frappe.website.doctype.web_form.web_form import get_web_form_list list_context = frappe._dict() module = load_doctype_module(doctype) if hasattr(module, "get_list_context"): list_context = frappe._dict(module.get_list_context(context) or {}) # is web form, show the default web form filters # which is only the owner if frappe.form_dict.web_form_name: list_context.web_form_name = frappe.form_dict.web_form_name if not list_context.get("get_list"): list_context.get_list = get_web_form_list if not frappe.flags.web_form: # update list context from web_form frappe.flags.web_form = frappe.get_doc('Web Form', frappe.form_dict.web_form_name) if frappe.flags.web_form.is_standard: frappe.flags.web_form.update_list_context(list_context) return list_context
def run_module_method(self, method): from frappe.modules import load_doctype_module module = load_doctype_module(self.name, self.module) if hasattr(module, method): getattr(module, method)()
def get_context(context): context.doctype = frappe.local.form_dict.doctype context.txt = frappe.local.form_dict.txt module = load_doctype_module(context.doctype) context.update(get_items(context.doctype, context.txt)) return context