def reset_doc(doctype): ''' doctype = name of the DocType that you want to reset ''' # fetch module name module = frappe.db.get_value('DocType', doctype, 'module') app = utils.get_module_app(module) # get path for doctype's json and its equivalent git url doc_path = os.path.join(get_module_path(module), 'doctype', scrub(doctype), scrub(doctype)+'.json') try: git_link = '/'.join(['https://raw.githubusercontent.com/frappe',\ app, branch, doc_path.split('apps/'+app)[1]]) original_file = urlopen(git_link).read() except: print('Did not find {0} in {1}'.format(doctype, app)) return # load local and original json objects local_doc = json.loads(open(doc_path, 'r').read()) original_doc = json.loads(original_file) remove_duplicate_fields(doctype) set_property_setter(doctype, local_doc, original_doc) make_custom_fields(doctype, local_doc, original_doc) with open(doc_path, 'w+') as f: f.write(original_file) f.close() setup_perms_for(doctype) frappe.db.commit()
def nextcloud_before_insert(doc, method=None): doc.flags.ignore_nc = True nc = nextcloud_link() if not nc.isconnect: return doc.flags.ignore_file_validate = True doctype = doc.attached_to_doctype module = get_doctype_module(doctype) # Excluded module if module in nc.excludedmodules: return # File previously attached to another transaction if not doc.file_name or doc.file_name == None: return if " NC/f/" in doc.file_name: return doc.flags.ignore_nc = False site = frappe.local.site if doc.is_private: local_fileobj = "./" + site + doc.file_url else: local_fileobj = "./" + site + "/public" + doc.file_url fileobj = local_fileobj.split('/') uu = len(fileobj) - 1 doc.nc = nc doc.nc.module = module doc.nc.app = get_module_app(module) doc.nc.path = nc.initialpath + "/" + doc.nc.app + "/" + module + "/" + doctype doc.nc.pathglobal = doc.nc.path + "/" + fileobj[uu].encode("ascii", "ignore").decode("ascii") doc.nc.local_fileobj = local_fileobj doc.nc.remote_fileobj=fileobj[uu].encode("ascii", "ignore").decode("ascii")
def nextcloud_insert(doc, method=None): if doc.flags.ignore_nc: return # upload to nextcloud if not "http" in doc.nc.local_fileobj: doc.nc.webdav.upload(local_fileobj=doc.nc.local_fileobj, remote_fileobj=doc.nc.remote_fileobj, nc_path=doc.nc.path) else: data = frappe.db.get_value("File", {"file_url": doc.file_url , "file_name": ["like", "%NC/f/%"]}, ["attached_to_doctype", "name", "file_name"], as_dict=True) if data: if doc.attached_to_doctype != data.attached_to_doctype: doctype = data.attached_to_doctype module = get_doctype_module(doctype) app = get_module_app(module) doc.nc.pathglobal = doc.nc.initialpath + "/" + app + "/" + module + "/" + doctype + "/" + doc.file_name data_json = doc.nc.shareModule(doc) fname = data.file_name.replace(" NC/f/","#") doc.file_name = fname.split("#")[0] + " NC(" + data.name + ")/f/" + fname.split("#")[1] doc.save() return data_json = doc.nc.shareModule(doc) # add public Share in Nextcloud if doc.nc.sharepublic or doc.is_private == False: shareType = 3 data_json = doc.nc.ocs.createShare(doc.nc.pathglobal,shareType) if data_json == "": time.sleep(2) data_json = doc.nc.ocs.createShare(doc.nc.pathglobal,shareType) data_string = json.dumps(data_json) decoded = json.loads(data_string) try: fileid = str(decoded["ocs"]["data"]["file_source"]) except TypeError: fname = frappe.db.get_value("File", {"file_name": ["like", doc.file_name + " NC/f/%"]}, "name") docorigin = frappe.get_doc('File', str(fname)) if docorigin: docorigin.content_hash = doc.content_hash docorigin.flags.ignore_file_validate = True docorigin.save() if doc.nc.enabletagging: fileid = str(docorigin.file_name.replace(" NC/f/","#").split("#")[1]) doc.nc.deletetags(docorigin, fileid, relational=doc.nc.relationaltagging) doc.nc.tagging(docorigin, fileid, relational=doc.nc.relationaltagging) os.remove(doc.nc.local_fileobj) doc.delete() frappe.db.commit() sys.exit() if doc.nc.sharepublic or doc.is_private == False: urllink = str(decoded["ocs"]["data"]["url"]) else: urllink = doc.nc.url + "/f/" + fileid # update doctype file if urllink != None and urllink != "": doc.file_url = urllink doc.file_name = doc.file_name.encode("ascii", "ignore").decode("ascii") + " NC/f/" + fileid doc.save() # delete local file os.remove(doc.nc.local_fileobj) # tagging if doc.nc.enabletagging: doc.nc.tagging(doc, fileid, relational=doc.nc.relationaltagging)
def __init__(self, doc=None): self.isconnect = False docns = frappe.get_doc("Nextcloud Settings") if not docns or not docns.enable: return self.doctype = doc.attached_to_doctype if doc != None else "Nextcloud Settings" self.module = get_doctype_module(self.doctype) nem = frappe.get_all( "Nextcloud Excluded Module", filters={ "parent": "Nextcloud Settings", "excluded_module": self.module }, fields=["excluded_module", "doctypes", "file_formats"], distinct=True) if nem: for row in nem: xdoctypes = row.doctypes.upper() xfile_formats = row.file_formats.upper() if xdoctypes == "" or "ALL" in xdoctypes or self.doctype.upper( ) in xdoctypes.split(","): if doc == None or doc.file_name == None: return a = doc.file_name.split(".") xformat = a[len(a) - 1] if xfile_formats == "" or "ALL" in xfile_formats or xformat.upper( ) in xfile_formats.split(","): return self.app = get_module_app(self.module) self.initialpath = docns.initial_path self.sharepublic = docns.share_public self.enabletagging = docns.enable_tagging self.relationaltagging = docns.relational_tagging self.tagging_background = docns.tagging_background self.url = docns.script_url self.lasttag = 0 host = self.url + docns.webdav_path username = docns.client_id passwd = docns.get_password(fieldname='client_secret', raise_exception=False) # Conect self.webdav = nextcloud_apis.WebDav(host=host, username=username, password=passwd) self.ocs = nextcloud_apis.OCS(docns.script_url, docns.client_id, passwd, js=True) data_json = self.ocs.getUsers() data_string = json.dumps(data_json) decoded = json.loads(data_string) if str(decoded["ocs"]["meta"]["status"]) == 'ok': self.isconnect = True
def nextcloud_delete(doc, method=None): if doc.flags.ignore_nc: return nc = nextcloud_link() if not nc.isconnect: return doctype = doc.attached_to_doctype module = get_doctype_module(doctype) if module in nc.excludedmodules: return ncf = doc.file_name.find(" NC/f/") if ncf == -1: return filename = doc.file_name[0:ncf] app = get_module_app(module) path = nc.initialpath + "/" + app + "/" + module + "/" + doctype + "/" + filename nc.webdav.delete(path)
def nextcloud_downloadtoserver(doc, method=None): nc = nextcloud_link(doc=doc) if not nc.isconnect: return if not " NC" in doc.file_name: return if " NC(" in doc.file_name: nc.doctype = frappe.db.get_value("File", { "file_url": doc.file_url, "file_name": ["like", "%NC/f/%"] }, "attached_to_doctype") nc.module = get_doctype_module(nc.doctype) nc.app = get_module_app(nc.module) ncf = doc.file_name.find(" NC") if ncf == -1: return filename = doc.file_name[0:ncf] path = nc.initialpath + "/" + nc.app + "/" + nc.module + "/" + nc.doctype + "/" + filename site = frappe.local.site local_fileobj = "./" + site + "/private/files/" + filename nc.webdav.downloadtoserver(path, local_fileobj) return local_fileobj
def reset_doc(doctype): """ doctype = name of the DocType that you want to reset """ # fetch module name module = frappe.db.get_value("DocType", doctype, "module") app = utils.get_module_app(module) # get path for doctype's json and its equivalent git url doc_path = os.path.join( get_module_path(module), "doctype", scrub(doctype), scrub(doctype) + ".json" ) try: git_link = "/".join( ["https://raw.githubusercontent.com/frappe", app, branch, doc_path.split("apps/" + app)[1]] ) original_file = urlopen(git_link).read() except: print("Did not find {0} in {1}".format(doctype, app)) return # load local and original json objects local_doc = json.loads(open(doc_path, "r").read()) original_doc = json.loads(original_file) remove_duplicate_fields(doctype) set_property_setter(doctype, local_doc, original_doc) make_custom_fields(doctype, local_doc, original_doc) with open(doc_path, "w+") as f: f.write(original_file) f.close() setup_perms_for(doctype) frappe.db.commit()
def get_webform_list_context(module): if get_module_app(module) != 'erpnext': return return {"get_list": get_webform_transaction_list}