def rename_files_and_folders(self, old, new): # move files new_path = get_doc_path(self.module, 'doctype', new) subprocess.check_output(['mv', get_doc_path(self.module, 'doctype', old), new_path]) # rename files for fname in os.listdir(new_path): if frappe.scrub(old) in fname: subprocess.check_output(['mv', os.path.join(new_path, fname), os.path.join(new_path, fname.replace(frappe.scrub(old), frappe.scrub(new)))]) self.rename_inside_controller(new, old, new_path) frappe.msgprint('Renamed files and replaced code in controllers, please check!')
def rename_files_and_folders(self, old, new): # move files new_path = get_doc_path(self.module, 'doctype', new) old_path = get_doc_path(self.module, 'doctype', old) shutil.move(old_path, new_path) # rename files for fname in os.listdir(new_path): if frappe.scrub(old) in fname: old_file_name = os.path.join(new_path, fname) new_file_name = os.path.join(new_path, fname.replace(frappe.scrub(old), frappe.scrub(new))) shutil.move(old_file_name, new_file_name) self.rename_inside_controller(new, old, new_path) frappe.msgprint(_('Renamed files and replaced code in controllers, please check!'))
def get_print_format(doctype, format_name): if format_name == standard_format: return format_name opts = frappe.db.get_value("Print Format", format_name, "disabled", as_dict=True) if not opts: frappe.throw( _("Print Format {0} does not exist").format(format_name), frappe.DoesNotExistError) elif opts.disabled: frappe.throw( _("Print Format {0} is disabled").format(format_name), frappe.DoesNotExistError) # server, find template path = os.path.join( get_doc_path(frappe.db.get_value("DocType", doctype, "module"), "Print Format", format_name), frappe.scrub(format_name) + ".html") if os.path.exists(path): with open(path, "r") as pffile: return pffile.read() else: html = frappe.db.get_value("Print Format", format_name, "html") if html: return html else: frappe.throw( _("No template found at path: {0}").format(path), frappe.TemplateNotFoundError)
def get_items(type, txt, limit_start=0): meta = frappe.get_meta(type) filters, or_filters = [], [] out = frappe._dict() if txt: if meta.search_fields: for f in meta.get_search_fields(): or_filters.append([type, f.strip(), "like", "%" + txt + "%"]) else: filters.append([type, "name", "like", "%" + txt + "%"]) out.raw_items = frappe.get_list(type, fields = ["*"], filters=filters, or_filters = or_filters, limit_start=limit_start, limit_page_length = 20) template_path = os.path.join(get_doc_path(meta.module, "DocType", meta.name), "list_item.html") if os.path.exists(template_path): env = Environment(loader = FileSystemLoader("/")) #template_path = os.path.relpath(template_path) template = env.get_template(template_path) else: template = Template("""<div><a href="/{{ doctype }}/{{ doc.name }}"> {{ doc[title_field] }}</a></div>""") out.items = [template.render(doc=i, doctype=type, title_field = meta.title_field or "name") for i in out.raw_items] out.meta = meta return out
def make_controller_template(self): from frappe.modules import get_doc_path, get_module_path, scrub target_path = get_doc_path(self.module, self.doctype, self.name) app = frappe.local.module_app[scrub(self.module)] if not app: frappe.throw(_("App not found")) app_publisher = frappe.get_hooks(hook="app_publisher", app_name=app)[0] def _make_boilerplate(template): template_name = template.replace("controller", scrub(self.name)) target_file_path = os.path.join(target_path, template_name) if not os.path.exists(target_file_path): with open(target_file_path, 'w') as target: with open( os.path.join(get_module_path("core"), "doctype", "doctype", "boilerplate", template), 'r') as source: target.write(source.read().format( app_publisher=app_publisher, classname=self.name.replace(" ", ""), doctype=self.name)) _make_boilerplate("controller.py") if not (self.istable or self.issingle): _make_boilerplate("test_controller.py") _make_boilerplate("test_records.json")
def get_items(type, txt, limit_start=0): meta = frappe.get_meta(type) filters, or_filters = [], [] out = frappe._dict() if txt: if meta.search_fields: for f in meta.get_search_fields(): or_filters.append([type, f.strip(), "like", "%" + txt + "%"]) else: filters.append([type, "name", "like", "%" + txt + "%"]) out.raw_items = frappe.get_list(type, fields = ["*"], filters=filters, or_filters = or_filters, limit_start=limit_start, limit_page_length = 20) template_path = os.path.join(get_doc_path(meta.module, "DocType", meta.name), "list_item.html") if os.path.exists(template_path): env = Environment(loader = FileSystemLoader("/")) #template_path = os.path.relpath(template_path) template = env.get_template(template_path) else: template = Template("""<div><a href="/{{ doctype }}/{{ doc.name }}" no-pjax> {{ doc[title_field] }}</a></div>""") out.items = [template.render(doc=i, doctype=type, title_field = meta.title_field or "name") for i in out.raw_items] out.meta = meta return out
def get_print_style(style=None): if not style: style = frappe.db.get_default("print_style") or "Standard" path = os.path.join(get_doc_path("Core", "DocType", "Print Format"), "styles", style.lower() + ".css") if not os.path.exists(path): if style!="Standard": return get_print_style("Standard") else: return "/* Standard Style Missing ?? */" else: with open(path, 'r') as sfile: return sfile.read()
def make_controller_template(self): from frappe.modules import get_doc_path, get_module_path, scrub pypath = os.path.join( get_doc_path(self.doc.module, self.doc.doctype, self.doc.name), scrub(self.doc.name) + '.py') if not os.path.exists(pypath): with open(pypath, 'w') as pyfile: with open( os.path.join(get_module_path("core"), "doctype", "doctype", "doctype_template.py"), 'r') as srcfile: pyfile.write(srcfile.read())
def get_print_format_name(doctype, format_name): if format_name==standard_format: return format_name # server, find template path = os.path.join(get_doc_path(frappe.db.get_value("DocType", doctype, "module"), "Print Format", format_name), format_name + ".html") if os.path.exists(path): with open(path, "r") as pffile: return pffile.read() else: html = frappe.db.get_value("Print Format", format_name, "html") if html: return html else: return "No template found.\npath: " + path
def make_controller_template(self): from frappe.modules import get_doc_path, get_module_path, scrub pypath = os.path.join(get_doc_path(self.module, self.doctype, self.name), scrub(self.name) + '.py') if not os.path.exists(pypath): # get app publisher for copyright app = frappe.local.module_app[frappe.scrub(self.module)] if not app: frappe.throw(_("App not found")) app_publisher = frappe.get_hooks(hook="app_publisher", app_name=app)[0] with open(pypath, 'w') as pyfile: with open(os.path.join(get_module_path("core"), "doctype", "doctype", "doctype_template.py"), 'r') as srcfile: pyfile.write(srcfile.read().format(app_publisher=app_publisher, classname=self.name.replace(" ", "")))
def get_print_format(doctype, print_format): if print_format.disabled: frappe.throw(_("Print Format {0} is disabled").format(print_format.name), frappe.DoesNotExistError) # server, find template path = os.path.join(get_doc_path(frappe.db.get_value("DocType", doctype, "module"), "Print Format", print_format.name), frappe.scrub(print_format.name) + ".html") if os.path.exists(path): with open(path, "r") as pffile: return pffile.read() else: if print_format.html: return print_format.html else: frappe.throw(_("No template found at path: {0}").format(path), frappe.TemplateNotFoundError)
def get_print_format(doctype, format_name): if format_name==standard_format: return format_name opts = frappe.db.get_value("Print Format", format_name, "disabled", as_dict=True) if not opts: frappe.throw(_("Print Format {0} does not exist").format(format_name), frappe.DoesNotExistError) elif opts.disabled: frappe.throw(_("Print Format {0} is disabled").format(format_name), frappe.DoesNotExistError) # server, find template path = os.path.join(get_doc_path(frappe.db.get_value("DocType", doctype, "module"), "Print Format", format_name), frappe.scrub(format_name) + ".html") if os.path.exists(path): with open(path, "r") as pffile: return pffile.read() else: html = frappe.db.get_value("Print Format", format_name, "html") if html: return html else: frappe.throw(_("No template found at path: {0}").format(path), frappe.TemplateNotFoundError)