def get_file_path(file_name): """Returns file path from given file name""" f = frappe.db.sql( """select file_url from `tabFile` where name=%s or file_name=%s""", (file_name, file_name)) if f: file_name = f[0][0] file_path = file_name if "/" not in file_path: file_path = "/files/" + file_path if file_path.startswith("/private/files/"): file_path = get_files_path(*file_path.split("/private/files/", 1)[1].split("/"), is_private=1) elif file_path.startswith("/files/"): file_path = get_files_path( *file_path.split("/files/", 1)[1].split("/")) else: frappe.throw( _("There is some problem with the file url: {0}").format( file_path)) return file_path
def get_full_path(self): """Returns file path from given file name""" file_path = self.file_url or self.file_name if "/" not in file_path: file_path = "/files/" + file_path if file_path.startswith("/private/files/"): file_path = get_files_path(*file_path.split("/private/files/", 1)[1].split("/"), is_private=1) elif file_path.startswith("/files/"): file_path = get_files_path( *file_path.split("/files/", 1)[1].split("/")) elif file_path.startswith("http"): pass elif not self.file_url: frappe.throw( _("There is some problem with the file url: {0}").format( file_path)) return file_path
def execute(): public_files_path = get_files_path() private_files_path = get_files_path(is_private=1) print (public_files_path) print (private_files_path) public_files = [f for f in listdir(public_files_path) if isfile(join(public_files_path, f))] private_files = [f for f in listdir(private_files_path) if isfile(join(private_files_path, f))] orphan_private = 0 orphan_pub = 0 for list_of_files in [public_files, private_files]: if list_of_files: for files in list_of_files: if list_of_files == public_files: file_path = public_files_path + '/' + files else: file_path = private_files_path + '/' + files file_db = frappe.db.sql("""SELECT name, attached_to_doctype, attached_to_name, file_url, file_name FROM `tabFile` WHERE file_name = '%s'"""%(files), as_list=1) if file_db: pass #print(file_db) else: #delete_file(file_path) print("Deleted file with Name = " + files + " and file path = " + file_path) if list_of_files == public_files: orphan_pub += 1 else: orphan_private += 1 print("Orphaned File Name = " + files + " path = " + file_path) print ("Public Files Orphaned and hence Deleted = " + str(orphan_pub)) print ("Private Files Orphaned and hence Deleted = " + str(orphan_private))
def get_file_path(file_name): """Returns file path from given file name""" f = frappe.db.sql( """select file_url from `tabFile` where name=%s or file_name=%s""", (file_name, file_name), ) if f: file_name = f[0][0] file_path = file_name if "/" not in file_path: file_path = "/files/" + file_path if file_path.startswith("/private/files/"): file_path = get_files_path(*file_path.split("/private/files/", 1)[1].split("/"), is_private=1) elif file_path.startswith("/files/"): file_path = get_files_path(*file_path.split("/files/", 1)[1].split("/")) else: frappe.throw(_("There is some problem with the file url: {0}").format(file_path)) return file_path
def rename_replacing_files(): replaced_files = get_replaced_files() if len(replaced_files): missing_files = [v[0] for v in replaced_files] with open(get_site_path('missing_files.txt'), 'w') as f: f.write(('\n'.join(missing_files) + '\n').encode('utf-8')) for file_name, file_datas in replaced_files: print 'processing ' + file_name content_hash = frappe.db.get_value('File', file_datas[0], 'content_hash') if not content_hash: continue new_file_name = get_file_name(file_name, content_hash) if os.path.exists(get_files_path(new_file_name)): continue print 'skipping ' + file_name try: os.rename(get_files_path(file_name), get_files_path(new_file_name)) except OSError: print 'Error renaming ', file_name for name in file_datas: f = frappe.get_doc('File', name) f.file_name = new_file_name f.file_url = '/files/' + new_file_name f.save()
def backup_to_dropbox(): from dropbox import client, session from frappe.utils.backups import new_backup from frappe.utils import get_files_path, get_backups_path if not frappe.db: frappe.connect() sess = session.DropboxSession(frappe.conf.dropbox_access_key, frappe.conf.dropbox_secret_key, "app_folder") sess.set_token( frappe.db.get_value("Dropbox Backup", None, "dropbox_access_key"), frappe.db.get_value("Dropbox Backup", None, "dropbox_access_secret")) dropbox_client = client.DropboxClient(sess) # upload database backup = new_backup(ignore_files=True) filename = os.path.join(get_backups_path(), os.path.basename(backup.backup_path_db)) upload_file_to_dropbox(filename, "/database", dropbox_client) frappe.db.close() # upload files to files folder did_not_upload = [] error_log = [] upload_from_folder(get_files_path(), "/files", dropbox_client, did_not_upload, error_log) upload_from_folder(get_files_path(is_private=1), "/private/files", dropbox_client, did_not_upload, error_log) frappe.connect() return did_not_upload, list(set(error_log))
def backup_to_dropbox(): from dropbox import client, session from frappe.utils.backups import new_backup from frappe.utils import get_files_path, get_backups_path if not frappe.db: frappe.connect() sess = session.DropboxSession(frappe.conf.dropbox_access_key, frappe.conf.dropbox_secret_key, "app_folder") sess.set_token(frappe.db.get_value("Dropbox Backup", None, "dropbox_access_key"), frappe.db.get_value("Dropbox Backup", None, "dropbox_access_secret")) dropbox_client = client.DropboxClient(sess) # upload database backup = new_backup(ignore_files=True) filename = os.path.join(get_backups_path(), os.path.basename(backup.backup_path_db)) upload_file_to_dropbox(filename, "/database", dropbox_client) frappe.db.close() # upload files to files folder did_not_upload = [] error_log = [] upload_from_folder(get_files_path(), "/files", dropbox_client, did_not_upload, error_log) upload_from_folder(get_files_path(is_private=1), "/private/files", dropbox_client, did_not_upload, error_log) frappe.connect() return did_not_upload, list(set(error_log))
def backup_to_dropbox(): if not frappe.db: frappe.connect() dropbox_client = get_dropbox_client() # upload database backup = new_backup(ignore_files=True) filename = os.path.join(get_backups_path(), os.path.basename(backup.backup_path_db)) dropbox_client = upload_file_to_dropbox(filename, "/database", dropbox_client) frappe.db.close() # upload files to files folder did_not_upload = [] error_log = [] dropbox_client = upload_from_folder(get_files_path(), "/files", dropbox_client, did_not_upload, error_log) dropbox_client = upload_from_folder(get_files_path(is_private=1), "/private/files", dropbox_client, did_not_upload, error_log) frappe.connect() return did_not_upload, list(set(error_log))
def get_full_path(self): """Returns file path from given file name""" att = get_attachments(self.doctype, self.name) if att: file_path = att[0].file_url or att[0].file_name else: frappe.throw("No Attachment found") if "/" not in file_path: file_path = "/files/" + file_path if file_path.startswith("/private/files/"): file_path = get_files_path(*file_path.split("/private/files/", 1)[1].split("/"), is_private=1) elif file_path.startswith("/files/"): file_path = get_files_path( *file_path.split("/files/", 1)[1].split("/")) else: frappe.throw( _("There is some problem with the file url: {0}").format( file_path)) return file_path
def backup_to_dropbox(): if not frappe.db: frappe.connect() # upload database dropbox_settings = get_dropbox_settings() if not dropbox_settings['access_token']: access_token = generate_oauth2_access_token_from_oauth1_token(dropbox_settings) if not access_token.get('oauth2_token'): return 'Failed backup upload', 'No Access Token exists! Please generate the access token for Dropbox.' dropbox_settings['access_token'] = access_token['oauth2_token'] set_dropbox_access_token(access_token['oauth2_token']) dropbox_client = dropbox.Dropbox(dropbox_settings['access_token']) backup = new_backup(ignore_files=True) filename = os.path.join(get_backups_path(), os.path.basename(backup.backup_path_db)) upload_file_to_dropbox(filename, "/database", dropbox_client) frappe.db.close() # upload files to files folder did_not_upload = [] error_log = [] upload_from_folder(get_files_path(), "/files", dropbox_client, did_not_upload, error_log) upload_from_folder(get_files_path(is_private=1), "/private/files", dropbox_client, did_not_upload, error_log) frappe.connect() return did_not_upload, list(set(error_log))
def backup_to_service(): from frappe.utils.backups import new_backup from frappe.utils import get_files_path # upload files to files folder did_not_upload = [] error_log = [] if not frappe.db: frappe.connect() older_than = cint(frappe.db.get_value('Backup Manager', None, 'older_than')) if cint(frappe.db.get_value("Backup Manager", None, "enable_database")): # upload database backup = new_backup(ignore_files=True) # filename = os.path.join(get_backups_path(), os.path.basename(backup.backup_path_db)) sync_folder(older_than, get_backups_path(), "database") BASE_DIR = os.path.join(get_backups_path(), '../file_backups') if cint(frappe.db.get_value("Backup Manager", None, "enable_files")): Backup_DIR = os.path.join(BASE_DIR, "files") compress_files(get_files_path(), Backup_DIR) sync_folder(older_than, Backup_DIR, "files") if cint(frappe.db.get_value("Backup Manager", None, "enable_private_files")): Backup_DIR = os.path.join(BASE_DIR, "private/files") compress_files(get_files_path(is_private=1), Backup_DIR) sync_folder(older_than, Backup_DIR, "private/files") frappe.db.close() return did_not_upload, list(set(error_log))
def get_attachments(profile_id, folder, sub_folder, event_id, visit_id=None, req_id=None): files = [] if visit_id: path = os.path.join(get_files_path(), profile_id, event_id, folder, sub_folder, visit_id) if req_id: path = os.path.join(get_files_path(), profile_id, req_id, event_id, folder, sub_folder, visit_id) else: path = os.path.join(get_files_path(), profile_id, event_id, folder, sub_folder) if req_id: path = os.path.join(get_files_path(), profile_id, req_id, event_id, folder, sub_folder) if os.path.exists(path): for root, dirc, filenames in os.walk(path): for di in dirc: for fl in os.listdir(os.path.join(path,di)): if fl.split('.')[-1:][0].lower() in ['jpg','jpeg','pdf','png', 'PDF']: if req_id: files.append({'file_name': fl, 'type':fl.split('.')[-1:][0], 'path': os.path.join('files', profile_id, req_id, event_id, folder, sub_folder, di)}) else: files.append({'file_name': fl, 'type':fl.split('.')[-1:][0], 'path': os.path.join('files', profile_id, event_id, folder, sub_folder, di)}) for fl in os.listdir(path): if fl.split('.')[-1:][0].lower() in ['jpg','jpeg','pdf','png', 'PDF']: if req_id: files.append({'file_name': fl, 'type':fl.split('.')[-1:][0], 'path': os.path.join('files', profile_id, req_id, event_id, folder, sub_folder, visit_id)}) else: files.append({'file_name': fl, 'type':fl.split('.')[-1:][0], 'path': os.path.join('files', profile_id, event_id, folder, sub_folder, visit_id)}) return files
def write_file(content, file_path, fname): """write file to disk with a random name (to compare)""" # create directory (if not exists) frappe.create_folder(get_files_path()) # write the file with open(os.path.join(file_path, fname), 'w+') as f: f.write(content) return get_files_path(fname)
def write_file(content, file_path, fname): """write file to disk with a random name (to compare)""" # create directory (if not exists) frappe.create_folder(get_files_path()) # write the file with open(os.path.join(file_path.encode('utf-8'), fname.encode('utf-8')), 'w+') as f: f.write(content) return get_files_path(fname)
def zip_attachments(document): zip_count = 1 zip_size = 0 document = json.loads(document) document2 = frappe._dict(document) fname = get_file_name(document2.name + " (zip 1).zip", random_string(7)) import zipfile docZip = zipfile.ZipFile(fname, "w", zipfile.ZIP_DEFLATED) for file_url in frappe.db.sql( """select file_url, is_private from `tabFile` where attached_to_doctype = %(doctype)s and attached_to_name = %(docname)s""", { 'doctype': document2.doctype, 'docname': document2.name }, as_dict=True): frappe.msgprint("Adding " + file_url.file_url) if file_url.file_url.startswith("/private/files/"): path = get_files_path(*file_url.file_url.split( "/private/files/", 1)[1].split("/"), is_private=1) elif file_url.file_url.startswith("/files/"): path = get_files_path( *file_url.file_url.split("/files/", 1)[1].split("/")) path = encode(path) if zip_size + os.path.getsize(path) > 10000000: zip_count = zip_count + 1 zip_size = 0 docZip.close() with open(encode(fname), 'r') as f: content = f.read() content = base64.b64encode(content) save_file(fname, content, document2.doctype, document2.name, "Home/Attachments", 1) fname = get_file_name( document2.name + " (zip " + str(zip_count) + ").zip", random_string(7)) docZip = zipfile.ZipFile(fname, "w", zipfile.ZIP_DEFLATED) docZip.write(path, os.path.basename(path)) zip_size = zip_size + docZip.getinfo( os.path.basename(path)).compress_size docZip.close() with open(encode(fname), 'r') as f: content = f.read() content = base64.b64encode(content) save_file(fname, content, document2.doctype, document2.name, "Home/Attachments", 1)
def write_file(content, fname, is_private=0): """write file to disk with a random name (to compare)""" file_path = get_files_path(is_private=is_private) # create directory (if not exists) frappe.create_folder(file_path) # write the file with open(os.path.join(file_path.encode("utf-8"), fname.encode("utf-8")), "w+") as f: f.write(content) return get_files_path(fname, is_private=is_private)
def write_file(content, fname, is_private=0): """write file to disk with a random name (to compare)""" file_path = get_files_path(is_private=is_private) # create directory (if not exists) frappe.create_folder(file_path) # write the file with open(os.path.join(file_path.encode('utf-8'), fname.encode('utf-8')), 'w+') as f: f.write(content) return get_files_path(fname, is_private=is_private)
def create_tortal_user_csv(filename): EmpIdentifier = frappe.db.get_value("Tortal LMS System Settings", None, "emp_identifier") # list of system users system_user_details = frappe.db.sql( """select first_name,middle_name,last_name,email,username, tortal_lms_password,'','','','','','',frappe_userid,is_active_tortal_lms_user from `tabUser` where frappe_userid IS NOT NULL and is_active_tortal_lms_user=1 and user_type='System User' """, as_list=1) private_files = get_files_path().replace("/public/", "/private/") private_files_path = get_bench_path() + "/sites" + private_files.replace( "./", "/") # list of website users having company website_user_details = frappe.db.sql( """select first_name,middle_name,last_name,email,username, tortal_lms_password,'','','','','','',frappe_userid,is_active_tortal_lms_user from `tabUser` where frappe_userid IS NOT NULL and is_active_tortal_lms_user=1 and user_type='Website User' and frappe_userid in( select user.frappe_userid from `tabContact` contact inner join `tabDynamic Link` customer on customer.parent=contact.name inner join `tabCustomer` cust on cust.name=customer.link_name inner join `tabUser` user on contact.user=user.name where customer.link_doctype='Customer' and customer.parenttype='Contact' and user.frappe_userid is not null and user_type='Website User' and customer_type in ('Company','Individual'))""", as_list=1) private_files = get_files_path().replace("/public/", "/private/") private_files_path = get_bench_path() + "/sites" + private_files.replace( "./", "/") with open(private_files_path + '/' + filename, 'wb') as f_handle: writer = csv.writer(f_handle) # Add the header/column names # header = ['First Name','Middle Name','Last Name','Email','Username','Password','Company','Address1','Address2','City','State','Postal Code','Identifier','IsActive'] # writer.writerow(header) for row in system_user_details: writer.writerow(row) for row in website_user_details: writer.writerow(row) return os.path.realpath(f_handle.name)
def backup_to_dropbox(upload_db_backup=True): if not frappe.db: frappe.connect() # upload database dropbox_settings = get_dropbox_settings() if not dropbox_settings["access_token"]: access_token = generate_oauth2_access_token_from_oauth1_token( dropbox_settings) if not access_token.get("oauth2_token"): return ( "Failed backup upload", "No Access Token exists! Please generate the access token for Dropbox.", ) dropbox_settings["access_token"] = access_token["oauth2_token"] set_dropbox_access_token(access_token["oauth2_token"]) dropbox_client = dropbox.Dropbox( oauth2_access_token=dropbox_settings["access_token"], timeout=None) if upload_db_backup: if frappe.flags.create_new_backup: backup = new_backup(ignore_files=True) filename = os.path.join(get_backups_path(), os.path.basename(backup.backup_path_db)) site_config = os.path.join( get_backups_path(), os.path.basename(backup.backup_path_conf)) else: filename, site_config = get_latest_backup_file() upload_file_to_dropbox(filename, "/database", dropbox_client) upload_file_to_dropbox(site_config, "/database", dropbox_client) # delete older databases if dropbox_settings["no_of_backups"]: delete_older_backups(dropbox_client, "/database", dropbox_settings["no_of_backups"]) # upload files to files folder did_not_upload = [] error_log = [] if dropbox_settings["file_backup"]: upload_from_folder(get_files_path(), 0, "/files", dropbox_client, did_not_upload, error_log) upload_from_folder(get_files_path(is_private=1), 1, "/private/files", dropbox_client, did_not_upload, error_log) return did_not_upload, list(set(error_log))
def write_file(content, fname, is_private=0): """write file to disk with a random name (to compare)""" file_path = get_files_path(is_private=is_private) # create directory (if not exists) frappe.create_folder(file_path) # write the file if isinstance(content, text_type): content = content.encode() with open(os.path.join(file_path.encode('utf-8'), fname.encode('utf-8')), 'wb+') as f: f.write(content) return get_files_path(fname, is_private=is_private)
def write_file(content, fname, is_private=0): """write file to disk with a random name (to compare)""" file_path = get_files_path(is_private=is_private) # create directory (if not exists) frappe.create_folder(file_path) # write the file if isinstance(content, text_type): content = content.encode() with open(os.path.join(file_path.encode("utf-8"), fname.encode("utf-8")), "wb+") as f: f.write(content) return get_files_path(fname, is_private=is_private)
def write_file(self): """write file to disk with a random name (to compare)""" file_path = get_files_path(is_private=self.is_private) # create directory (if not exists) frappe.create_folder(file_path) # write the file self.content = self.get_content() if isinstance(self.content, text_type): self.content = self.content.encode() with open(os.path.join(file_path.encode('utf-8'), self.file_name.encode('utf-8')), 'wb+') as f: f.write(self.content) return get_files_path(self.file_name, is_private=self.is_private)
def get_files_doc(event_data, data): tag_dict = {'11': "consultancy-11", "12": "event_snap-12", "13": "lab_reports-13", "14":"prescription-14", "15": "cost_of_care-15"} files_list = [] if not data.get('files'): for d in event_data.get('sharelist'): for key, values in tag_dict.items(): for sub_tab in ['A_51', 'B_52', 'C_53']: attachments = get_attachments(d.get("from_profile_id"), values, sub_tab, d.get("event_tag_id")) for att in attachments: files_list.append(os.path.join(get_files_path(), att.get('path').split('files/')[1], att.get('file_name'))) return files_list else: for fl in event_data.get('file_path'): files_list.append(os.path.join(get_files_path(), fl)) return files_list
def share_via_email(data): attachments = [] files = data.get('files') for fl in files: fname = os.path.join(get_files_path(), fl) attachments.append({ "fname": fname, "fcontent": file(fname).read() }) if attachments: msg = """Event Name is %(event)s <br> Event Date is %(event_date)s <br> Provider Name is %(provider_name)s <br> Sharing reason is %(reason)s <br> <hr> %(event_body)s <br> Please find below attachment(s) <br> """%{'event': data.get('event_title'), 'event_date': data.get('event_date'), 'provider_name': data.get('doctor_name'), 'event_body': data.get('email_body'), 'reason': data.get('reason')} from frappe.utils.email_lib import sendmail sendmail([data.get('email_id')], subject="PHR-Event Data", msg=cstr(msg), attachments=attachments) make_log(data.get('profile_id'),"Event","Shared Via Email","Event Shared Via Email to %s"%(data.get('email_id'))) return """Selected image(s) has been shared with %(provider_name)s for event %(event)s """%{ 'event': data.get('event_title'), 'provider_name': data.get('doctor_name')} else: return 'Please select file(s) for sharing'
def sync_item_image_handle (doc, site_doc, api_map, headers, image_path=None): # Check if we have image if (not image_path or image_path==''): frappe.throw('There is no image to sync') # Let's get the file image_file_data = frappe.get_doc("File Data", { "file_url": image_path, "attached_to_doctype": "Item", "attached_to_name": doc.get('name') }) if (image_file_data is None): frappe.throw('Cannot find image with path %s' %image_path) file_path = get_files_path() + '/' + image_file_data.get('file_name') # Push image onto oc server res = oc_requests(site_doc.get('server_base_url'), headers, api_map, 'Product Image', \ file_path=file_path, url_params={'id': doc.get(OC_PROD_ID)}, stop=True) if res: if (res.get('success')): doc.update({'last_sync_image': datetime.now()}) frappe.msgprint('Successfully updated product\'s image on Opencart') return doc.last_sync_image else: frappe.throw('Unknown error posting image. Image not updated %s' %json.dumps(res))
def send_email(share_info, profile_id, disease): from email.mime.audio import MIMEAudio from email.mime.base import MIMEBase from email.mime.image import MIMEImage from email.mime.text import MIMEText import mimetypes import datetime attachments = [] file_name = disease + '.pdf' files = os.path.join(get_files_path(), profile_id, file_name) attachments.append({"fname": files, "fcontent": file(files).read()}) if attachments: msg = """Disease Name is %(event)s <br> Provider Name is %(provider_name)s <br> <hr> %(event_body)s <br> Please see attachment <br> """ % { 'event': disease, 'provider_name': share_info.get('doctor_name'), 'event_body': share_info.get('email_body') } from frappe.utils.email_lib import sendmail sendmail([share_info.get('email_id')], subject="PHR-Disease Monitoring Data", msg=cstr(msg), attachments=attachments)
def save_pdf(data, header, profile_id, disease): import pdfkit data = eval(data) rows = '' for row in data: rows += "<tr>%s<tr>" % row html_str = """ <html> <body> <table class="table table-striped"> <thead> %(header)s </thead> %(data_rows)s </table> </body> </html> """ % { 'data_rows': rows, 'header': header } file_name = disease + '.pdf' pdfkit.from_string(html_str, os.path.join(get_files_path(), profile_id, file_name))
def sync_item_image(item): image_info = {"image": {}} if item.image: img_details = frappe.db.get_value("File", {"file_url": item.image}, ["file_name", "content_hash"]) if img_details and img_details[0] and img_details[1]: is_private = item.image.startswith("/private/files/") with open( get_files_path(img_details[0].strip("/"), is_private=is_private), "rb") as image_file: image_info["image"]["attachment"] = base64.b64encode( image_file.read()) image_info["image"]["filename"] = img_details[0] #to avoid 422 : Unprocessable Entity if not image_info["image"]["attachment"] or not image_info[ "image"]["filename"]: return False elif item.image.startswith("http") or item.image.startswith("ftp"): if validate_image_url(item.image): #to avoid 422 : Unprocessable Entity image_info["image"]["src"] = item.image if image_info["image"]: if not item_image_exists(item.shopify_product_id, image_info): # to avoid image duplication post_request( "/admin/products/{0}/images.json".format( item.shopify_product_id), image_info)
def sync_item_image(item): image_info = { "image": {} } if item.image: img_details = frappe.db.get_value("File", {"file_url": item.image}, ["file_name", "content_hash"]) if img_details and img_details[0] and img_details[1]: is_private = item.image.startswith("/private/files/") with open(get_files_path(img_details[0].strip("/"), is_private=is_private), "rb") as image_file: image_info["image"]["attachment"] = base64.b64encode(image_file.read()) image_info["image"]["filename"] = img_details[0] #to avoid 422 : Unprocessable Entity if not image_info["image"]["attachment"] or not image_info["image"]["filename"]: return False elif item.image.startswith("http") or item.image.startswith("ftp"): if validate_image_url(item.image): #to avoid 422 : Unprocessable Entity image_info["image"]["src"] = item.image if image_info["image"]: if not item_image_exists(item.shopify_product_id, image_info): # to avoid image duplication post_request("/admin/products/{0}/images.json".format(item.shopify_product_id), image_info)
def send_document(self): try: token = frappe.db.get_value('Telegram Bot', self.bot, 'api_token') chat_id = frappe.db.get_value('Telegram Chat', self.telegram_chat, 'chat_id') file_name = frappe.db.get_value('File', self.file, 'file_name') if self.status == 'Error': self.resend_count += 1 bot = telegram.Bot(token=token) res = bot.sendDocument(chat_id, document=open( get_files_path(file_name, is_private=0), "rb")) if res: self.status = 'Completed' self.error_message = None if self.delete_linked_file_after_sent: file_name = self.file self.file = None self.save() frappe.delete_doc('File', file_name, force=1) except: self.status = 'Error' self.error_message = frappe.get_traceback() self.save()
def get_app_editor_file_path(app, fn=""): basedir = get_files_path('app_center_files') path = os.path.join(basedir, app, ".editor", fn) if len(path) < (len(basedir) + len(app) + len(fn)): print(basedir, app, fn, path) throw(_("EEEEEEEEEEEEEEEEEEEEEEE")) return path
def setProfileImage(): import os from frappe.utils import get_files_path data = json.loads(frappe.local.request.data) if data.get('file_name'): file_path = "%(files_path)s/%(profile_id)s/%(file_name)s"%{'files_path': get_files_path(), "profile_id": data.get('profile_id'), 'file_name': data.get('file_name') } path = os.path.join(os.getcwd(), get_files_path()[2:], data.get('profile_id')) frappe.create_folder(path) with open("%s/%s"%(path,data.get('file_name')), 'wb') as f: f.write(base64.b64decode(data.get('bin_img'))) res = update_profile_image(data.get('profile_id'), data.get('file_name')) return {"filestatus": res}
def get_app_release_path(app): basedir = get_files_path('app_center_files') file_dir = os.path.join(basedir, app) if not os.path.exists(file_dir): os.makedirs(file_dir) return file_dir
def sync_item_image_handle(doc, site_doc, api_map, headers, image_path=None): # Check if we have image if (not image_path or image_path == ''): frappe.throw('There is no image to sync') # Let's get the file image_file_data = frappe.get_doc( "File Data", { "file_url": image_path, "attached_to_doctype": "Item", "attached_to_name": doc.get('name') }) if (image_file_data is None): frappe.throw('Cannot find image with path %s' % image_path) file_path = get_files_path() + '/' + image_file_data.get('file_name') # Push image onto oc server res = oc_requests(site_doc.get('server_base_url'), headers, api_map, 'Product Image', \ file_path=file_path, url_params={'id': doc.get(OC_PROD_ID)}, stop=True) if res: if (res.get('success')): doc.update({'last_sync_image': datetime.now()}) frappe.msgprint( 'Successfully updated product\'s image on Opencart') return doc.last_sync_image else: frappe.throw('Unknown error posting image. Image not updated %s' % json.dumps(res))
def sync_item_image(item): image_info = {"image": {}} if item.image: img_details = frappe.db.get_value("File", {"file_url": item.image}, ["file_name", "content_hash"]) if img_details and img_details[0] and img_details[1]: is_private = item.image.startswith("/private/files/") with open( get_files_path(img_details[0].strip("/"), is_private=is_private), "rb") as image_file: image_info["image"]["attachment"] = base64.b64encode( image_file.read()) image_info["image"]["filename"] = img_details[0] elif item.image.startswith("http") or item.image.startswith("ftp"): image_info["image"]["src"] = item.image if image_info["image"]: try: if not exist_item_image(item.shopify_id, image_info): post_request( "/admin/products/{0}/images.json".format( item.shopify_id), image_info) except ShopifyError: raise ShopifyError
def save_file(self, content=None, decode=False): file_exists = False self.content = content if decode: if isinstance(content, text_type): self.content = content.encode("utf-8") if b"," in self.content: self.content = self.content.split(b",")[1] self.content = base64.b64decode(self.content) if not self.is_private: self.is_private = 0 self.file_size = self.check_max_file_size() self.content_hash = get_content_hash(self.content) self.content_type = mimetypes.guess_type(self.file_name)[0] _file = frappe.get_value("File", {"content_hash": self.content_hash}, ["file_url"]) if _file: self.file_url = _file file_exists = True if not file_exists: if os.path.exists(encode(get_files_path(self.file_name))): self.file_name = get_file_name(self.file_name, self.content_hash[-6:]) call_hook_method("before_write_file", file_size=self.file_size) write_file_method = get_hook_method('write_file') if write_file_method: return write_file_method(self) return self.save_file_on_filesystem()
def create_invoice_pdf(sinv, fid, iuser): file_path = get_files_path("portal-files") frappe.create_folder(file_path) output = PdfFileWriter() output = frappe.get_print("Sales Invoice", sinv, "standard", as_pdf=True, output=output) file = os.path.join(file_path, "{0}.pdf".format(fid)) output.write(open(file, "wb")) frappe.publish_realtime(event="invoice_ready", message={ "sub": sinv, "dict": { "bgCompleted": 1, "fid": fid } }, user=iuser) frappe.publish_realtime("new_notifications", { "type": "default", "message": "PDF for Invoice# {sub} is available to download.".format(sub=sinv) }, user=iuser)
def get_requisition_file_path(comp_name_hash): basedir = get_files_path(COMPANY_REQUISITION_FILES) file_dir = os.path.join(basedir, comp_name_hash) if not os.path.exists(file_dir): os.makedirs(file_dir) return file_dir
def validate_file(self): if (self.file_url or "").startswith("/files/"): if not self.file_name: self.file_name = self.file_url.split("/files/")[-1] if not os.path.exists(get_files_path(self.file_name.lstrip("/"))): frappe.throw(_("File {0} does not exist").format(self.file_url), IOError)
def send_email(share_info, profile_id, disease): from email.mime.audio import MIMEAudio from email.mime.base import MIMEBase from email.mime.image import MIMEImage from email.mime.text import MIMEText import mimetypes import datetime attachments = [] file_name = disease + '.pdf' files = os.path.join(get_files_path(), profile_id, file_name) attachments.append({ "fname": files, "fcontent": file(files).read() }) if attachments: msg = """Disease Name is %(event)s <br> Provider Name is %(provider_name)s <br> <hr> %(event_body)s <br> Please see attachment <br> """%{'event': disease, 'provider_name': share_info.get('doctor_name'), 'event_body': share_info.get('email_body')} from frappe.utils.email_lib import sendmail sendmail([share_info.get('email_id')], subject="PHR-Disease Monitoring Data", msg=cstr(msg), attachments=attachments) make_log(profile_id, "Disease Monitoring", "Shared Via Email to provider %s"% share_info.get('doctor_name') , "DM of <b style='color: #89c148;'>%s</b> Shared Via Email to provider %s"%(disease,share_info.get('doctor_name'))) return {"returncode":1,"message_display":"Disease Monitoring records has been shared"}
def share_via_email(data): attachments = [] files = data.get('files') patient_name = frappe.db.get_value("User", {"profile_id":data.get('profile_id')}, 'concat(first_name, " ", last_name)') or data.get('lphr_name') for fl in files: fname = os.path.join(get_files_path(), fl) attachments.append({ "fname": fname.split('/')[-1:][0], "fcontent": file(fname).read() }) if attachments: msg = """Event Name is %(event)s <br> Event Date is %(event_date)s <br> Provider Name is %(provider_name)s <br> Sharing reason is %(reason)s <br> <hr> %(event_body)s <br> Please find below attachment(s) <br> """%{'event': data.get('event_title'), 'event_date': data.get('event_date') or data.get("str_visit_date"), 'provider_name': data.get('doctor_name'), 'event_body': data.get('email_body'), 'reason': data.get('reason')} from frappe.utils.email_lib import sendmail sendmail([data.get('email_id')], subject="PHR-Event Data", msg=cstr(msg), attachments=attachments) make_log(data.get('profile_id'),"Event","Shared Via Email","Event <b style='color: #89c148;'>%s</b> Shared Via Email %s"%(data.get('event_title'), data.get('email_id'))) # args = {"patient":patient_name,"email":data.get('email_id')} # notify_provider(data.get('doctor_id'),data.get('profile_id'),"Event Share Email",args) return { "returncode":1,"message_summary":"Selected image(s) has been shared with %(provider_name)s for event %(event)s "%{'event': data.get('event_title'),'provider_name': data.get('doctor_name')}} else: return {"returncode":0,"message_summary":"Please select file(s) for sharing"}
def backup_to_service(): from frappe.utils.backups import new_backup from frappe.utils import get_files_path # upload files to files folder did_not_upload = [] error_log = [] if not frappe.db: frappe.connect() older_than_hrs = cint( frappe.db.get_value('Backup Manager', None, 'older_than')) cloud_sync = cint(frappe.db.get_value('Backup Manager', None, 'cloud_sync')) # site = cstr(frappe.local.site) site = get_site_base_path()[2:] if cint(frappe.db.get_value("Backup Manager", None, "enable_database")): # upload database backup = new_backup(older_than_hrs, ignore_files=True) # filename = os.path.join(get_backups_path(), os.path.basename(backup.backup_path_db)) if cloud_sync: sync_folder(site, older_than_hrs, get_backups_path(), "database", did_not_upload, error_log) BASE_DIR = os.path.join(get_backups_path(), '../file_backups') if cint(frappe.db.get_value("Backup Manager", None, "enable_files")): Backup_DIR = os.path.join(BASE_DIR, "files") compress_files(get_files_path(), Backup_DIR) if cloud_sync: sync_folder(site, older_than_hrs, Backup_DIR, "public-files", did_not_upload, error_log) if cint(frappe.db.get_value("Backup Manager", None, "enable_private_files")): Backup_DIR = os.path.join(BASE_DIR, "private/files") compress_files(get_files_path(is_private=1), Backup_DIR, "private") if cloud_sync: sync_folder(site, older_than_hrs, Backup_DIR, "private-files", did_not_upload, error_log) frappe.db.close() # frappe.connect() return did_not_upload, list(set(error_log))
def zip_attachments(document): zip_count = 1 zip_size = 0 document = json.loads(document) document2 = frappe._dict(document) fname = get_file_name(document2.name + " (zip 1).zip", random_string(7)) import zipfile docZip = zipfile.ZipFile(fname,"w", zipfile.ZIP_DEFLATED) for file_url in frappe.db.sql("""select file_url, is_private from `tabFile` where attached_to_doctype = %(doctype)s and attached_to_name = %(docname)s""", {'doctype': document2.doctype, 'docname': document2.name}, as_dict=True ): frappe.msgprint("Adding " + file_url.file_url) if file_url.file_url.startswith("/private/files/"): path = get_files_path(*file_url.file_url.split("/private/files/", 1)[1].split("/"), is_private=1) elif file_url.file_url.startswith("/files/"): path = get_files_path(*file_url.file_url.split("/files/", 1)[1].split("/")) path = encode(path) if zip_size + os.path.getsize(path) > 10000000: zip_count = zip_count + 1 zip_size = 0 docZip.close() with open(encode(fname), 'r') as f: content = f.read() content = base64.b64encode(content) save_file(fname, content, document2.doctype, document2.name, "Home/Attachments", 1) fname = get_file_name(document2.name + " (zip " + str(zip_count) + ").zip", random_string(7)) docZip = zipfile.ZipFile(fname,"w", zipfile.ZIP_DEFLATED) docZip.write(path, os.path.basename(path)) zip_size = zip_size + docZip.getinfo(os.path.basename(path)).compress_size docZip.close() with open(encode(fname), 'r') as f: content = f.read() content = base64.b64encode(content) save_file(fname, content, document2.doctype, document2.name, "Home/Attachments", 1)
def save_file_on_filesystem(fname, content, content_type=None): public_path = os.path.join(frappe.local.site_path, "public") fpath = write_file(content, get_files_path(), fname) path = os.path.relpath(fpath, public_path) return { 'file_name': os.path.basename(path), 'file_url': '/' + path }
def share_dm(data, header, share_info, profile_id, disease): share_info = json.loads(share_info) frappe.create_folder(os.path.join(get_files_path() , profile_id)) save_pdf(data, header, profile_id, disease) if share_info.get('share_via') == 'Email': return send_email(share_info, profile_id, disease) else: return share_via_phr(share_info, profile_id, disease)
def save_file_on_filesystem(fname, content, content_type=None): profile_id = frappe.form_dict.profile_id folder = frappe.form_dict.folder sub_folder = frappe.form_dict.sub_folder event_id = frappe.form_dict.event_id public_path = os.path.join(frappe.local.site_path, "public") if event_id and folder and sub_folder: folder_path = os.path.join(get_files_path(), profile_id, event_id, folder, sub_folder) elif profile_id: folder_path = os.path.join(get_files_path(), profile_id) fpath = write_file(content, folder_path, fname) path = os.path.relpath(fpath, public_path) return { 'file_name': os.path.basename(path), 'file_url': '/' + path }
def validate_file(self): """Validates existence of public file TODO: validate for private file """ if (self.file_url or "").startswith("/files/"): if not self.file_name: self.file_name = self.file_url.split("/files/")[-1] if not os.path.exists(get_files_path(frappe.as_unicode(self.file_name.lstrip("/")))): frappe.throw(_("File {0} does not exist").format(self.file_url), IOError)
def get_file_name(fname, optional_suffix): n_records = frappe.db.sql("select name from `tabFile Data` where file_name=%s", fname) if len(n_records) > 0 or os.path.exists(get_files_path(fname.encode('utf-8'))): f = fname.rsplit('.', 1) if len(f) == 1: partial, extn = f[0], "" else: partial, extn = f[0], "." + f[1] return '{partial}{suffix}{extn}'.format(partial=partial, extn=extn, suffix=optional_suffix) return fname
def backup_to_service(): from frappe.utils.backups import new_backup from frappe.utils import get_files_path # upload files to files folder did_not_upload = [] error_log = [] if not frappe.db: frappe.connect() older_than_hrs = cint(frappe.db.get_value('Backup Manager', None, 'older_than')) cloud_sync = cint(frappe.db.get_value('Backup Manager', None, 'cloud_sync')) # site = cstr(frappe.local.site) site = get_site_base_path()[2:] if cint(frappe.db.get_value("Backup Manager", None, "enable_database")): # upload database backup = new_backup(older_than_hrs,ignore_files=True) # filename = os.path.join(get_backups_path(), os.path.basename(backup.backup_path_db)) if cloud_sync: sync_folder(site,older_than_hrs,get_backups_path(), "database",did_not_upload,error_log) BASE_DIR = os.path.join( get_backups_path(), '../file_backups' ) if cint(frappe.db.get_value("Backup Manager", None, "enable_files")): Backup_DIR = os.path.join(BASE_DIR, "files") compress_files(get_files_path(), Backup_DIR) if cloud_sync: sync_folder(site,older_than_hrs,Backup_DIR, "public-files",did_not_upload,error_log) if cint(frappe.db.get_value("Backup Manager", None, "enable_private_files")): Backup_DIR = os.path.join(BASE_DIR, "private/files") compress_files(get_files_path(is_private=1), Backup_DIR,"private") if cloud_sync: sync_folder(site,older_than_hrs,Backup_DIR, "private-files",did_not_upload,error_log) frappe.db.close() # frappe.connect() return did_not_upload, list(set(error_log))
def generate_content_hash(self): if self.content_hash or not self.file_url: return if self.file_url.startswith("/files/"): try: with open(get_files_path(self.file_name.lstrip("/")), "r") as f: self.content_hash = get_content_hash(f.read()) except IOError: frappe.msgprint(_("File {0} does not exist").format(self.file_url)) raise
def get_files_doc(event_data, data, selected_files=None, event_dict = None, sub_event_count = None): tag_dict = {'11': "consultancy-11", "12": "event_snap-12", "13": "lab_reports-13", "14":"prescription-14", "15": "cost_of_care-15"} if selected_files and len(selected_files) > 1: tag_dict = {k: tag_dict[k] for k in selected_files[:-1]} files_list = [] if not data.get('files'): for d in event_data.get('sharelist'): for key, values in tag_dict.items(): for sub_tab in ['A_51', 'B_52', 'C_53']: attachments = get_attachments(d.get("from_profile_id"), values, sub_tab, d.get("event_tag_id"), d.get("visit_tag_id")) for att in attachments: shared_file_count(os.path.join(att.get('path').split('files/')[1], att.get('file_name')), event_dict, sub_event_count) files_list.append(os.path.join(get_files_path(), att.get('path').split('files/')[1], att.get('file_name'))) return files_list else: for fl in event_data.get('file_path'): shared_file_count(fl, event_dict, sub_event_count) files_list.append(os.path.join(get_files_path(), fl)) return files_list
def share_via_phr(share_info, profile_id, disease): dm_sharing = frappe.new_doc('Disease Sharing Log') file_name = disease + '.pdf' dm_sharing.disease_name = disease dm_sharing.from_profile = profile_id dm_sharing.to_profile = share_info.get('doctor_id') dm_sharing.pdf_path = os.path.join(get_files_path(), profile_id, file_name) dm_sharing.save(ignore_permissions=True) make_sharing_request(share_info, disease, dm_sharing, profile_id) make_log(profile_id, "Disease Monitoring", "Shared DM over PHR account to provider ", "DM of <b style='color: #89c148;'>%s</b> has been shared with provider <b style='color: #89c148;'>%s</b> \ till <b style='color: #89c148;'>%s</b>"% (disease, share_info.get('doctor_name'), share_info.get('sharing_duration') )) return {"returncode":1,"message_display":"Disease Monitoring records has been shared"}
def get_file_name(fname, optional_suffix): # convert to unicode fname = cstr(fname) n_records = frappe.db.sql("select name from `tabFile Data` where file_name=%s", fname) if len(n_records) > 0 or os.path.exists(encode(get_files_path(fname))): f = fname.rsplit(".", 1) if len(f) == 1: partial, extn = f[0], "" else: partial, extn = f[0], "." + f[1] return "{partial}{suffix}{extn}".format(partial=partial, extn=extn, suffix=optional_suffix) return fname
def share_via_phr(share_info, profile_id, disease): dm_sharing = frappe.new_doc('Disease Sharing Log') file_name = disease + '.pdf' dm_sharing.disease_name = disease dm_sharing.from_profile = profile_id dm_sharing.to_profile = share_info.get('doctor_id') dm_sharing.pdf_path = os.path.join(get_files_path(), profile_id, file_name) dm_sharing.save(ignore_permissions=True) make_sharing_request(share_info, disease, dm_sharing, profile_id) make_log(profile_id, "Disease Monitoring", "Shared over PHR account to provider %s"% share_info.get('doctor_name') , "Shared over PHR account to provider %s"% share_info.get('doctor_name')) args = {"dr":share_info.get('doctor_name')} email_msg = "Patient has shared DM with You. \n\n Thank you. \nTeam HealthSnapp." from phr.templates.pages.event import notify_provider notify_provider(share_info.get('doctor_id'),profile_id,"Event Share",args,email_msg) return "Disease Monitoring records has been shared"
def execute(): files_path = get_files_path() # get files that don't have attached_to_name but exist unlinked_files = get_unlinked_files(files_path) if not unlinked_files: return fixed_files = fix_files_for_item(files_path, unlinked_files) # fix remaining files for key, file_data in unlinked_files.items(): if key not in fixed_files: rename_and_set_content_hash(files_path, unlinked_files, key) frappe.db.commit()
def backup_to_dropbox(): from dropbox import client, session from frappe.utils.backups import new_backup from frappe.utils import get_files_path, get_backups_path if not frappe.db: frappe.connect() sess = session.DropboxSession(frappe.conf.dropbox_access_key, frappe.conf.dropbox_secret_key, "app_folder") sess.set_token(frappe.db.get_value("Dropbox Backup", None, "dropbox_access_key"), frappe.db.get_value("Dropbox Backup", None, "dropbox_access_secret")) dropbox_client = client.DropboxClient(sess) # upload database backup = new_backup() filename = os.path.join(get_backups_path(), os.path.basename(backup.backup_path_db)) upload_file_to_dropbox(filename, "/database", dropbox_client) frappe.db.close() response = dropbox_client.metadata("/files") # upload files to files folder did_not_upload = [] error_log = [] path = get_files_path() for filename in os.listdir(path): filename = cstr(filename) if filename in ignore_list: continue found = False filepath = os.path.join(path, filename) for file_metadata in response["contents"]: if os.path.basename(filepath) == os.path.basename(file_metadata["path"]) and os.stat(filepath).st_size == int(file_metadata["bytes"]): found = True break if not found: try: upload_file_to_dropbox(filepath, "/files", dropbox_client) except Exception: did_not_upload.append(filename) error_log.append(frappe.get_traceback()) frappe.connect() return did_not_upload, list(set(error_log))
def download_file(file_url): """ Download file using token and REST API. Valid session or token is required to download private files. Method : GET Endpoint : frappe.utils.file_manager.download_file URL Params : file_name = /path/to/file relative to site path """ file_doc = frappe.get_doc("File", {"file_url":file_url}) file_doc.check_permission("read") path = os.path.join(get_files_path(), os.path.basename(file_url)) with open(path, "rb") as fileobj: filedata = fileobj.read() frappe.local.response.filename = os.path.basename(file_url) frappe.local.response.filecontent = filedata frappe.local.response.type = "download"