Beispiel #1
0
def delete_downloadable_backups():
	path = get_site_path('private', 'backups')
	files = [x for x in os.listdir(path) if os.path.isfile(os.path.join(path, x))]
	backup_limit = get_scheduled_backup_limit()

	if len(files) > backup_limit:
		cleanup_old_backups(path, files, backup_limit)
Beispiel #2
0
def image_writter(profile_id, event_id=None, visit_id=None):
	import os, base64
	data = {"profile_id": profile_id, "event_id": event_id, "visit_id": visit_id}
	filelist = get_image_details(data)
	for file_obj in filelist:
		
		tags = file_obj.get('tag_id').split('-')[2]
		folder = tag_dict.get(tags[:2])
		sub_folder = sub_tag_dict.get(tags[:2]).get(tags[2:])
		path = os.path.join(os.getcwd(), get_site_path().replace('.',"").replace('/', ""), 'public', 'files', data.get('profile_id'), data.get("event_id"),  folder, sub_folder, file_obj.get('visit_id'))
		
		wfile_name = file_obj.get('temp_file_id').split('.')[0] + '-watermark.' + file_obj.get('temp_file_id').split('.')[1]
		if not os.path.exists(os.path.join(path, wfile_name)):
			frappe.create_folder(path)
			img_path = os.path.join(path,  wfile_name)
			frappe.errprint(["visit_id", data.get('visit_id')])
			data = {
				"entityid": file_obj.get('visit_id'),
				"profile_id": data.get('profile_id'),
				"event_id": data.get("event_id") or "",
				"tag_id": file_obj.get('tag_id'),
				"file_id": [
					file_obj.get('temp_file_id')
				],
				"file_location": [
					img_path
				]
			}
			res = write_file(data)
Beispiel #3
0
def get_context(context):
	def get_time(path):
		dt = os.path.getmtime(path)
		return convert_utc_to_user_timezone(datetime.datetime.utcfromtimestamp(dt)).strftime('%Y-%m-%d %H:%M')

	def get_size(path):
		size = os.path.getsize(path)
		if size > 1048576:
			return "{0:.1f}M".format(float(size) / 1048576)
		else:
			return "{0:.1f}K".format(float(size) / 1024)

	path = get_site_path('private', 'backups')
	files = [x for x in os.listdir(path) if os.path.isfile(os.path.join(path, x))]
	backup_limit = get_scheduled_backup_limit()

	if len(files) > backup_limit:
		cleanup_old_backups(path, files, backup_limit)

	files = [('/backups/' + _file,
		get_time(os.path.join(path, _file)),
		get_size(os.path.join(path, _file))) for _file in files if _file.endswith('sql.gz')]
	files.sort(key=lambda x: x[1], reverse=True)

	return {"files": files}
Beispiel #4
0
def write_csv(column_header):
	my_csv = open(get_site_path('public/files','so_csv.csv'), 'wb')
	final_csv = csv.writer(my_csv, quoting=csv.QUOTE_ALL)
	
	for row in column_header:
		final_csv.writerow(row)
	my_csv.close()
Beispiel #5
0
def upload():
	# # get record details
	# dt = frappe.form_dict.doctype
	# dn = frappe.form_dict.docname

	file_url = frappe.form_dict.file_url
	filename = frappe.form_dict.filename

	# frappe.errprint([file_url, filename])
	if not filename and not file_url:
		frappe.msgprint(_("Please select a file or url"),
			raise_exception=True)

	# save
	if filename:
		filedata = save_uploaded()
	# elif file_url:
	# 	filedata = save_url(file_url, dt, dn)

	frappe.errprint(["testing image uploader", filedata])
	return {
		"site_path" : os.path.join(os.getcwd(), get_site_path().replace('.',"").replace('/', ""), 'public', 'files'),
		"file_name": filedata['fname'],
		"success_meg": filedata['msg']
	}
Beispiel #6
0
def write_docfile(data):
	import os
	for indx, file_path in enumerate(data.get('files')):
		base_dir_path = os.path.join(os.getcwd(), get_site_path().replace('.',"").replace('/', ""), 'public', 'files')
		folder_lst = file_path.split('/')
		file_path =  '/'.join(folder_lst[:-1]) 
		doc_name = folder_lst[-1:][0]
		doc_base_path = os.path.join(base_dir_path, file_path)

		if '-watermark' not in doc_name:
			dname = doc_name.split('.')
			doc_name = dname[0]+'-watermark.'+dname[1]

		data.get('files')[indx] = "{doc_base_path}/{doc_name}".format(doc_base_path=doc_base_path, doc_name=doc_name)

		if not os.path.exists(doc_base_path + '/' +doc_name):
			frappe.create_folder(doc_base_path)
			data = {
				"entityid": folder_lst[4],
				"profile_id": folder_lst[0],
				"event_id": folder_lst[1],
				"tag_id": folder_lst[4] + '-' + cstr(folder_lst[2].split('-')[1]) + cstr(folder_lst[3].split('_')[1]),
				"file_id": [
					doc_name.replace('-watermark','')
				],
				"file_location": [
					doc_base_path + '/' + doc_name
				]
			}

			from templates.pages.event import write_file
			res = write_file(data)
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()
Beispiel #8
0
def get_patient_data(data):
	dms_files = []
	filelist = frappe.db.sql("select files_list from `tabShared Requests` where name = '%s'"%data.get('other_param').get('req_id'), as_list=1)[0][0]
	filelist = json.loads(filelist)

	data_dict ={"to_profile_id":data.get('profile_id'),
		"received_from": "desktop", "from_profile_id": data.get('other_param').get('patient_profile_id'),
		"event_tag_id": data.get('other_param').get('event_id')}

	for fl in filelist:
		fl = fl.split('files/')[1].split('/')
		file_dict = {"entityid": fl[4],
			"profile_id": fl[0],
			"event_id": fl[1]}

		file_dict['tag_id'] = fl[4] + '-' +  fl[2].split('-')[1] + fl[3].split('_')[1]
		file_dict["file_id"] = [fl[5].replace('-watermark', '')]

		file_dict['file_location'] = [os.path.join(os.getcwd(), get_site_path().replace('.',"")
									.replace('/', ""), 'public', 'files',
									data_dict.get('to_profile_id'),
									data.get('other_param').get('req_id'), fl[1], fl[2], fl[3], fl[4])]

		frappe.create_folder(file_dict['file_location'][0])

		file_dict['file_location'] = [file_dict['file_location'][0] + '/' + file_dict["file_id"][0]]

		if not  os.path.exists(file_dict['file_location'][0]):
			dms_files.append(file_dict)

		request_type="POST"
		url="%s/dms/getvisitmultiplefile"%get_base_url()
		from phr.phr.phr_api import get_response
		param = {"filelist": dms_files}
		response=get_response(url, json.dumps(param), request_type)
Beispiel #9
0
def get_pdf_site_path(profile_id, folder, sub_folder, event_id, timestamp):
	site_path = os.path.join(os.getcwd(), get_site_path().replace('.',"").replace('/', ""), 'public', 'files')
	path = os.path.join(site_path, profile_id, event_id, folder, sub_folder)
	frappe.create_folder(path)
	return {
		'site_path': site_path,
		'timestamp': timestamp 
	}
Beispiel #10
0
def get_file(fname):
	"""Returns [`file_name`, `content`] for given file name `fname`"""
	file_path = get_file_path(fname)

	# read the file
	with open(get_site_path("public", file_path), 'r') as f:
		content = f.read()

	return [file_path.rsplit("/", 1)[-1], content]
Beispiel #11
0
def remove_event_files():
	import os, shutil
	path = os.path.join(os.getcwd(), get_site_path().replace('.',"").replace('/', ""), 'public', 'files')
	session_users = get_active_sessions()
	if os.path.exists(path):
		for root in os.walk(path).next()[1]:
			if root not in session_users:
				for dictn in os.walk(os.path.join(path, root)).next()[1]:
					shutil.rmtree(os.path.join(path, root, dictn))
Beispiel #12
0
def remove_old_task_logs():
	logs_path = get_site_path('task-logs')

	def full_path(_file):
		return os.path.join(logs_path, _file)

	files_to_remove = [full_path(_file) for _file in os.listdir(logs_path)]
	files_to_remove = [_file for _file in files_to_remove if is_file_old(_file) and os.path.isfile(_file)]
	for _file in files_to_remove:
		os.remove(_file)
Beispiel #13
0
def get_file(fname):
	f = frappe.db.sql("""select file_name from `tabFile Data`
		where name=%s or file_name=%s""", (fname, fname))
	if f:
		file_name = f[0][0]
	else:
		file_name = fname

	if not "/" in file_name:
		file_name = "files/" + file_name

	# read the file
	with open(get_site_path("public", file_name), 'r') as f:
		content = f.read()

	return [file_name, content]
Beispiel #14
0
def _new_site(db_name, site, mariadb_root_username=None, mariadb_root_password=None, admin_password=None,
	verbose=False, install_apps=None, source_sql=None,force=False, reinstall=False):
	"""Install a new Frappe site"""

	if not db_name:
		db_name = hashlib.sha1(site.encode()).hexdigest()[:16]

	from frappe.installer import install_db, make_site_dirs
	from frappe.installer import install_app as _install_app
	import frappe.utils.scheduler

	frappe.init(site=site)

	try:
		# enable scheduler post install?
		enable_scheduler = _is_scheduler_enabled()
	except Exception:
		enable_scheduler = False

	make_site_dirs()

	installing = None
	try:
		installing = touch_file(get_site_path('locks', 'installing.lock'))

		install_db(root_login=mariadb_root_username, root_password=mariadb_root_password, db_name=db_name,
			admin_password=admin_password, verbose=verbose, source_sql=source_sql,force=force, reinstall=reinstall)

		apps_to_install = ['frappe'] + (frappe.conf.get("install_apps") or []) + (list(install_apps) or [])
		for app in apps_to_install:
			_install_app(app, verbose=verbose, set_as_patched=not source_sql)

		frappe.utils.scheduler.toggle_scheduler(enable_scheduler)
		frappe.db.commit()

		scheduler_status = "disabled" if frappe.utils.scheduler.is_scheduler_disabled() else "enabled"
		print("*** Scheduler is", scheduler_status, "***")

	except frappe.exceptions.ImproperDBConfigurationError:
		_drop_site(site, mariadb_root_username, mariadb_root_password, force=True)

	finally:
		if installing and os.path.exists(installing):
			os.remove(installing)

		frappe.destroy()
Beispiel #15
0
def upload():
	file_url = frappe.form_dict.file_url
	filename = frappe.form_dict.filename

	if not filename and not file_url:
		frappe.msgprint(_("Please select a file or url"),
			raise_exception=True)

	# save
	if filename:
		filedata = save_uploaded()

	return {
		"site_path" : os.path.join(os.getcwd(), get_site_path().replace('.',"").replace('/', ""), 'public', 'files'),
		"file_name": filedata['fname'],
		"success_meg": filedata['msg']
	}
def get_files():
	def get_time(path):
		dt = os.path.getmtime(path)
		return convert_utc_to_user_timezone(datetime.datetime.utcfromtimestamp(dt)).strftime('%Y-%m-%d %H:%M')

	def get_size(path):
		size = os.path.getsize(path)
		if size > 1048576:
			return "{0:.1f}M".format(float(size) / 1048576)
		else:
			return "{0:.1f}K".format(float(size) / 1024)

	path = get_site_path('private', 'backups')
	files = [x for x in os.listdir(path) if os.path.isfile(os.path.join(path, x))]
	files = [('/backups/' + _file,
		get_time(os.path.join(path, _file)),
		get_size(os.path.join(path, _file))) for _file in files]
	return files
Beispiel #17
0
def get_phr_pdf(profile_id):
	import os, time
	path = os.path.join(os.getcwd(), get_site_path().replace('.',"").replace('/', ""), 'public', 'files', profile_id)
	solr_op = 'dms/getPhrPdfwithfilelocation'
	url = get_base_url()+solr_op
	request_type = 'POST'
	path += "/"
	data = {"profileId":profile_id,"file_location": [path]}
	from phr.phr.phr_api import get_response
	response = get_response(url,json.dumps(data),request_type)
	res = json.loads(response.text)
	if res:
		url = ""
		url = get_url()+"/files/%s/"%(profile_id)+cstr(res['file_location'].split('/')[-1]) + '?id=' + str(int(round(time.time() * 1000)))
		res["url"]=url
		response.headers['Content-Disposition'] = 'attachment; filename='+res["file_location"].split("/")[-1]
		return res
	else:
		frappe.msgprint(_("Issue Downloading PDF"))
Beispiel #18
0
def _new_site(db_name, site, mariadb_root_username=None, mariadb_root_password=None, admin_password=None,
	verbose=False, install_apps=None, source_sql=None,force=False, reinstall=False):
	"""Install a new Frappe site"""

	if not db_name:
		db_name = hashlib.sha1(site).hexdigest()[:16]

	from frappe.installer import install_db, make_site_dirs
	from frappe.installer import install_app as _install_app
	import frappe.utils.scheduler

	frappe.init(site=site)

	try:
		# enable scheduler post install?
		enable_scheduler = _is_scheduler_enabled()
	except:
		enable_scheduler = False

	make_site_dirs()

	installing = None
	try:
		installing = touch_file(get_site_path('locks', 'installing.lock'))

		install_db(root_login=mariadb_root_username, root_password=mariadb_root_password, db_name=db_name,
			admin_password=admin_password, verbose=verbose, source_sql=source_sql,force=force, reinstall=reinstall)

		apps_to_install = ['frappe'] + (frappe.conf.get("install_apps") or []) + (list(install_apps) or [])
		for app in apps_to_install:
			_install_app(app, verbose=verbose, set_as_patched=not source_sql)

		frappe.utils.scheduler.toggle_scheduler(enable_scheduler)
		frappe.db.commit()

		scheduler_status = "disabled" if frappe.utils.scheduler.is_scheduler_disabled() else "enabled"
		print "*** Scheduler is", scheduler_status, "***"

	finally:
		if installing and os.path.exists(installing):
			os.remove(installing)

		frappe.destroy()
Beispiel #19
0
def get_file(fname):
    f = frappe.db.sql(
        """select file_name from `tabFile Data`
		where name=%s or file_name=%s""", (fname, fname))
    if f:
        file_name = f[0][0]
    else:
        file_name = fname

    file_path = file_name

    if not "/" in file_path:
        file_path = "files/" + file_path

    # read the file
    with open(get_site_path("public", file_path), 'r') as f:
        content = f.read()

    return [file_name, content]
Beispiel #20
0
def get_context(context):
	def get_time(path):
		dt = os.path.getmtime(path)
		return convert_utc_to_user_timezone(datetime.datetime.utcfromtimestamp(dt)).strftime('%Y-%m-%d %H:%M')

	def get_size(path):
		size = os.path.getsize(path)
		if size > 1048576:
			return "{0:.1f}M".format(float(size) / 1048576)
		else:
			return "{0:.1f}K".format(float(size) / 1024)

	path = get_site_path('private', 'backups','owncloud')
	files = [x for x in os.listdir(path) if os.path.isfile(os.path.join(path, x))]
	files = [('/backups/owncloud/' + _file,
		get_time(os.path.join(path, _file)),
		get_size(os.path.join(path, _file))) for _file in files]
	files.sort(key=lambda x: x[1], reverse=True)

	return {"files": files}
def get_files():
    def get_time(path):
        dt = os.path.getmtime(path)
        return convert_utc_to_user_timezone(
            datetime.datetime.utcfromtimestamp(dt)).strftime('%Y-%m-%d %H:%M')

    def get_size(path):
        size = os.path.getsize(path)
        if size > 1048576:
            return "{0:.1f}M".format(float(size) / 1048576)
        else:
            return "{0:.1f}K".format(float(size) / 1024)

    path = get_site_path('private', 'backups')
    files = [
        x for x in os.listdir(path) if os.path.isfile(os.path.join(path, x))
    ]
    files = [('/backups/' + _file, get_time(os.path.join(path, _file)),
              get_size(os.path.join(path, _file))) for _file in files]
    return files
Beispiel #22
0
def upload():
    file_url = frappe.form_dict.file_url
    filename = frappe.form_dict.filename

    if not filename and not file_url:
        frappe.msgprint(_("Please select a file or url"), raise_exception=True)

    # save
    if filename:
        filedata = save_uploaded()

    return {
        "site_path":
        os.path.join(os.getcwd(),
                     get_site_path().replace('.', "").replace('/', ""),
                     'public', 'files'),
        "file_name":
        filedata['fname'],
        "success_meg":
        filedata['msg']
    }
Beispiel #23
0
def image_writter(profile_id, event_id):
	import os, base64
	data = {"profile_id": profile_id, "event_id": event_id}
	
	filelist = get_image_details(data)

	for file_obj in filelist:
		
		tags = file_obj.get('tag_id').split('-')[2]
		folder = tag_dict.get(tags[:2])
		sub_folder = sub_tag_dict.get(tags[:2]).get(tags[2:])
		path = os.path.join(os.getcwd(), get_site_path().replace('.',"").replace('/', ""), 'public', 'files', data.get('profile_id'), data.get("event_id"),  folder, sub_folder, file_obj.get('visit_id'))
		
		wfile_name = file_obj.get('temp_file_id').split('.')[0] + '-watermark.' + file_obj.get('temp_file_id').split('.')[1]
		if not os.path.exists(os.path.join(path, wfile_name)):
			frappe.create_folder(path)
			# filedata = file_obj.get('base64StringFile')
			# # frappe.errprint(filedata)
			# decoded_image = base64.b64decode(filedata)
			# # decoded_image = filedata.decode('base64','strict')
			# with open(img_path, 'wb') as f:
			# 	f.write(filedata)

			img_path = os.path.join(path,  wfile_name)
			data = {
				"entityid": file_obj.get('visit_id'),
				"profile_id": data.get('profile_id'),
				"event_id": data.get("event_id"),
				"tag_id": file_obj.get('tag_id'),
				"file_id": [
					file_obj.get('temp_file_id')
				],
				"file_location": [
					img_path
				]
			}
			
			res = write_file(data)
Beispiel #24
0
def get_phr_pdf(profile_id):
    path = os.path.join(os.getcwd(),
                        get_site_path().replace('.', "").replace('/', ""),
                        'public', 'files', profile_id)
    solr_op = 'dms/getPhrPdfwithfilelocation'
    url = get_base_url() + solr_op
    request_type = 'POST'
    path += "/"
    data = {"profileId": profile_id, "file_location": [path]}
    from phr.phr.phr_api import get_response
    response = get_response(url, json.dumps(data), request_type)
    res = json.loads(response.text)
    if res:
        #frappe.errprint(res['file_location'].split('/')[-1])
        url = get_url() + "/files/%s/" % (profile_id) + cstr(
            res['file_location'].split('/')[-1])
        res["url"] = url
        response.headers[
            'Content-Disposition'] = 'attachment; filename=' + res[
                "file_location"].split("/")[-1]
        return res
    else:
        frappe.msgprint(_("Issue Downloading PDF"))
Beispiel #25
0
def _new_site(db_name, site, mariadb_root_username=None, mariadb_root_password=None, admin_password=None,
	verbose=False, install_apps=None, source_sql=None,force=False, reinstall=False):
	"""Install a new Frappe site"""

	from frappe.installer import install_db, make_site_dirs
	from frappe.installer import install_app as _install_app
	import frappe.utils.scheduler

	frappe.init(site=site)

	try:
		# enable scheduler post install?
		enable_scheduler = _is_scheduler_enabled()
	except:
		enable_scheduler = False

	make_site_dirs()

	try:
		installing = touch_file(get_site_path('locks', 'installing.lock'))

		install_db(root_login=mariadb_root_username, root_password=mariadb_root_password, db_name=db_name,
			admin_password=admin_password, verbose=verbose, source_sql=source_sql,force=force, reinstall=reinstall)

		apps_to_install = ['frappe'] + (frappe.conf.get("install_apps") or []) + (list(install_apps) or [])
		for app in apps_to_install:
			_install_app(app, verbose=verbose, set_as_patched=not source_sql)

		frappe.utils.scheduler.toggle_scheduler(enable_scheduler)
		frappe.db.commit()

		scheduler_status = "disabled" if frappe.utils.scheduler.is_scheduler_disabled() else "enabled"
		print "*** Scheduler is", scheduler_status, "***"

	finally:
		os.remove(installing)
		frappe.destroy()
Beispiel #26
0
def get_patient_data(data):
	dms_files = []
	filelist = frappe.db.sql("select files_list from `tabShared Requests` where name = '%s'"%data.get('other_param').get('req_id'), as_list=1)[0][0]
	filelist = json.loads(filelist)

	data_dict ={"to_profile_id":data.get('profile_id'), 
		"received_from": "desktop", "from_profile_id": data.get('other_param').get('patient_profile_id'), 
		"event_tag_id": data.get('other_param').get('event_id')}
	
	for fl in filelist:
		fl = fl.split('files/')[1].split('/')
		file_dict = {"entityid": fl[4],
			"profile_id": fl[0],
			"event_id": fl[1]}

		file_dict['tag_id'] = fl[4] + '-' +  fl[2].split('-')[1] + fl[3].split('_')[1]
		file_dict["file_id"] = [fl[5].replace('-watermark', '')]

		file_dict['file_location'] = [os.path.join(os.getcwd(), get_site_path().replace('.',"")
									.replace('/', ""), 'public', 'files',
									data_dict.get('to_profile_id'), 
									data.get('other_param').get('req_id'), fl[1], fl[2], fl[3], fl[4])]

		frappe.create_folder(file_dict['file_location'][0])

		file_dict['file_location'] = [file_dict['file_location'][0] + '/' + file_dict["file_id"][0]]

		if not  os.path.exists(file_dict['file_location'][0]):
			dms_files.append(file_dict)

		request_type="POST"
		url="%s/dms/getvisitmultiplefile"%get_base_url()
		from phr.phr.phr_api import get_response
		print dms_files
		param = {"filelist": dms_files}
		response=get_response(url, json.dumps(param), request_type)
Beispiel #27
0
def save_file(fname, content, dt, dn, decode=False):
    if decode:
        if isinstance(content, unicode):
            content = content.encode("utf-8")
        content = base64.b64decode(content)

    import filecmp
    from frappe.model.code import load_doctype_module
    files_path = os.path.join(frappe.local.site_path, "public", "files")
    module = load_doctype_module(dt,
                                 frappe.db.get_value("DocType", dt, "module"))

    if hasattr(module, "attachments_folder"):
        files_path = os.path.join(files_path, module.attachments_folder)

    file_size = check_max_file_size(content)
    temp_fname = write_file(content, files_path)
    fname = scrub_file_name(fname)

    fname_parts = fname.split(".", -1)
    main = ".".join(fname_parts[:-1])
    extn = fname_parts[-1]
    versions = get_file_versions(files_path, main, extn)

    if versions:
        found_match = False
        for version in versions:
            if filecmp.cmp(os.path.join(files_path, version), temp_fname):
                # remove new file, already exists!
                os.remove(temp_fname)
                fname = version
                fpath = os.path.join(files_path, fname)
                found_match = True
                break

        if not found_match:
            # get_new_version name
            fname = get_new_fname_based_on_version(files_path, main, extn,
                                                   versions)
            fpath = os.path.join(files_path, fname)

            # rename
            if os.path.exists(fpath.encode("utf-8")):
                frappe.throw("File already exists: " + fname)

            os.rename(temp_fname, fpath.encode("utf-8"))
    else:
        fpath = os.path.join(files_path, fname)

        # rename new file
        if os.path.exists(fpath.encode("utf-8")):
            frappe.throw("File already exists: " + fname)

        os.rename(temp_fname, fpath.encode("utf-8"))

    f = frappe.bean({
        "doctype":
        "File Data",
        "file_name":
        os.path.relpath(os.path.join(files_path, fname),
                        get_site_path("public")),
        "attached_to_doctype":
        dt,
        "attached_to_name":
        dn,
        "file_size":
        file_size
    })
    f.ignore_permissions = True
    try:
        f.insert()
    except frappe.DuplicateEntryError:
        return frappe.doc("File Data", f.doc.duplicate_entry)

    return f.doc
    def import_abp_file(self):
        '''
			Trigger on import button
		'''
        columName = [["Etablissement", ""], ["Filière",
                                             ""], ["Classement", ""],
                     ["Réponse", ""], ["Numéro", ""], ["Nom", "last_name"],
                     ["Prénom", "first_name"],
                     ["Deuxième prénom", "middle_name"], ["Civilité", ""],
                     ["Sexe", "gender", "s", ["M", "Male"], ["F", "Female"]],
                     ["Date de naissance", "date_of_birth", "d", "dd/mm/yyyy"],
                     ["Ville de naissance", ""],
                     ["Département de naissance",
                      ""], ["Pays de naissance", ""], ["Nationalité", ""],
                     ["Adresse 1", "address_line_1"],
                     ["Adresse 2", "address_line_2"], ["Adresse 3", ""],
                     ["Code postal", "pincode"], ["Commune", "city"],
                     ["Pays", ""], ["Téléphone", ""],
                     ["Téléphone portable", ""]]

        file_path = os.getcwd() + get_site_path()[1:].encode(
            'utf8') + self.import_file
        wb = load_workbook(filename=file_path, read_only=True)
        ws = wb.active

        start = 2
        for i, row in enumerate(ws.iter_rows(min_row=start)):

            try:
                error = False
                #				program_doc = frappe.get_doc("Program", "BTSESF1)
                new_doc = frappe.new_doc("Student Applicant")
                new_doc.program = "BTS Économie Sociale Familiale"
                for j, cell in enumerate(row):
                    if j < 21 and columName[j][1] <> "":
                        #						new_doc.last_name = cell.value
                        if len(columName[j]) <= 2:
                            setattr(new_doc, columName[j][1], cell.value)
                        else:
                            if columName[j][2] == "s":
                                if columName[j][2][0] == cell.value:
                                    setattr(new_doc, columName[j][1],
                                            columName[j][2][1])
                                elif columName[j][3][0] == cell.value:
                                    setattr(new_doc, columName[j][1],
                                            columName[j][3][1])
                            elif columName[j][2] == "d":
                                #								setattr(new_doc, columName[j][1], formatdate(cell.value, columName[j][3]))
                                setattr(
                                    new_doc, columName[j][1],
                                    babel.dates.parse_date(cell.value,
                                                           locale='fr_FR'))
#								parse_date('01.04.2004', locale='de_DE')
#					if j == 6:
#						new_doc.first_name = cell.value
                new_doc.insert()
                new_doc.save()

            except Exception, e:
                error = True
                if new_doc:
                    frappe.errprint(new_doc if isinstance(new_doc, dict) else
                                    new_doc.as_dict())
                frappe.errprint(frappe.get_traceback())

            finally:
def backup_to_service():
	from frappe.utils.backups import new_backup
	from frappe.utils import get_files_path


	#delete old
	path = get_site_path('private', 'backups')
	files = [x for x in os.listdir(path) if os.path.isfile(os.path.join(path, x))]
	backup_limit = get_scheduled_backup_limit()
	endswith='sql.gz'
	if len(files) > backup_limit:
		cleanup_old_backups(path, files, backup_limit,endswith)

	endswith='files.tar'
	if len(files) > backup_limit:
		cleanup_old_backups(path, files, backup_limit,endswith)
	
	endswith='private-files.tar'
	if len(files) > backup_limit:
		cleanup_old_backups(path, files, backup_limit,endswith)


	#delete old
	
	# upload files to files folder
	did_not_upload = []
	error_log = []
	
	if not frappe.db:
		frappe.connect()
	
	older_than = cint(frappe.db.get_value('Backup Settings', None, 'older_than'))
	cloud_sync = cint(frappe.db.get_value('Backup Settings', None, 'cloud_sync'))

	site = frappe.db.get_value('Global Defaults', None, 'default_company')

	if cint(frappe.db.get_value("Backup Settings", None, "enable_database")):
		# upload database
		# backup = new_backup(older_than,ignore_files=True)
		backup = new_backup(ignore_files=False, backup_path_db=None,
							backup_path_files=None, backup_path_private_files=None, force=True)
		db_filename = os.path.join(get_backups_path(), os.path.basename(backup.backup_path_db))
		files_filename = os.path.join(get_backups_path(), os.path.basename(backup.backup_path_files))
		private_files = os.path.join(get_backups_path(), os.path.basename(backup.backup_path_private_files))
		folder = os.path.basename(db_filename)[:15] + '/'
		print db_filename
		print files_filename
		print private_files
		print folder


		# filename = os.path.join(get_backups_path(), os.path.basename(backup.backup_path_db))
		if cloud_sync:
			print 'inside uploading db to cloud'
			sync_folder(site,older_than,db_filename, "database",did_not_upload,error_log)

	BASE_DIR = os.path.join( get_backups_path(), '../file_backups' )
	# print(get_backups_path())
	# print BASE_DIR


	if cint(frappe.db.get_value("Backup Settings", None, "enable_public_files")):
		# Backup_DIR = os.path.join(BASE_DIR, "files")
		# compress_files(get_files_path(), Backup_DIR)
		if cloud_sync:
			print 'inside uploading public to cloud'
			sync_folder(site,older_than,files_filename, "public-files",did_not_upload,error_log)

	
	if cint(frappe.db.get_value("Backup Settings", None, "enable_private_files")):
		# Backup_DIR = os.path.join(BASE_DIR, "private/files")
		# compress_files(get_files_path(is_private=1), Backup_DIR)
		if cloud_sync:
			print 'inside uploading private to cloud'
			sync_folder(site,older_than,private_files, "private-files",did_not_upload,error_log)
		
	frappe.db.close()
	# frappe.connect()
	return did_not_upload, list(set(error_log))
Beispiel #30
0
def get_jasper_path(for_all_sites = False):
	if not for_all_sites:
		return 	get_site_path("jasper")

	module_path = os.path.normpath(os.path.join(os.path.dirname(jasper_erpnext_report.__file__), "jasper"))
	return module_path
Beispiel #31
0
    def validate(self):
        filename, file_extension = os.path.splitext(self.splash_photo)
        if file_extension != ".jpg":
            frappe.throw('The splash photo must be a jpg image.')

        filename, file_extension = os.path.splitext(self.icon)
        if file_extension != ".png":
            frappe.throw('The icon must be a png image.')

        bench_fo = os.path.join(frappe.__file__, '..', '..', '..', '..')
        site_name = utils.get_site_path()

        splash = self.splash_photo
        assets_folder = os.path.join(frappe.__file__, '..', '..', '..', '..',
                                     'sites', 'assets', 'flexone', 'images')
        file_p = os.path.join(site_name, 'public' + self.splash_photo)
        file_logo = os.path.join(site_name, 'public' + self.icon)
        logo = os.path.join(site_name, 'public' + self.logo)
        import shutil

        if os.path.isfile(file_p):
            os.rename(
                file_p,
                os.path.join(
                    os.path.join(site_name, 'public' + '/files/logo.jpg')))
            shutil.copyfile(
                os.path.join(
                    os.path.join(site_name, 'public' + '/files/logo.jpg')),
                os.path.join(
                    os.path.join(
                        site_name,
                        "../../apps/flexone/flexone/public/images/logo.jpg")))
            shutil.copyfile(
                os.path.join(
                    os.path.join(site_name, 'public' + '/files/logo.jpg')),
                os.path.join(
                    os.path.join(site_name,
                                 "../assets/flexone/images/logo.jpg")))

        if os.path.isfile(file_logo):
            os.rename(
                file_logo,
                os.path.join(
                    os.path.join(site_name, 'public' + '/files/icon.png')))
            shutil.copyfile(
                os.path.join(
                    os.path.join(site_name, 'public' + '/files/icon.png')),
                os.path.join(
                    os.path.join(
                        site_name,
                        "../../apps/flexone/flexone/public/images/icon.png")))
            shutil.copyfile(
                os.path.join(site_name, 'public' + '/files/icon.png'),
                os.path.join(site_name, "../assets/flexone/images/icon.png"))

        if os.path.isfile(logo):
            frappe.msgprint("s")
            os.rename(logo,
                      os.path.join(site_name, 'public' + '/files/logo1.png'))
            shutil.copyfile(
                os.path.join(site_name, 'public' + '/files/logo1.png'),
                os.path.join(
                    site_name,
                    "../../apps/flexone/flexone/public/images/logo1.png"))
            shutil.copyfile(
                os.path.join(site_name, 'public' + '/files/logo1.png'),
                os.path.join(site_name, "../assets/flexone/images/logo1.png"))

        self.splash_photo = "./files/logo.jpg"
        self.icon = "./files/icon.png"
        self.logo = "./files/logo1.png"

        if self.background_color:
            sty = """
.skin-origin .main-header .navbar {
	background-color: """ + self.background_color + """ !important;
}"""
            f = open(
                os.path.join(
                    site_name,
                    "../../apps/flexone/flexone/public/css/flexone_custom.css"
                ), "a+")
            f.write(sty)

            m = open(
                os.path.join(site_name,
                             "../assets/flexone/css/flexone_custom.css"), "a+")
            m.write(sty)

            #self.run_frappe_cmd('build', bench_path=os.path.join(site_name,"../../"))

            #import sys

            s = open(os.path.join(site_name, "../assets/css/flexone.min.css"),
                     "a+")
            s.write(sty)
            frappe.clear_cache()
Beispiel #32
0
 def get_env_cmd(self, cmd, bench_path='.'):
     site_name = utils.get_site_path()
     return os.path.abspath(
         os.path.join(os.path.join(site_name, "../../"), 'env', 'bin', cmd))
Beispiel #33
0
def get_lock_path(name):
    name = name.lower()
    lock_path = get_site_path(name + '.lock')
    return lock_path
Beispiel #34
0
def _new_site(db_name,
              site,
              mariadb_root_username=None,
              mariadb_root_password=None,
              admin_password=None,
              verbose=False,
              install_apps=None,
              source_sql=None,
              force=False,
              no_mariadb_socket=False,
              reinstall=False,
              db_type=None,
              db_host=None,
              db_password=None,
              db_port=None):
    """Install a new Frappe site"""

    if not force and os.path.exists(site):
        print('Site {0} already exists'.format(site))
        sys.exit(1)

    if no_mariadb_socket and not db_type == "mariadb":
        print('--no-mariadb-socket requires db_type to be set to mariadb.')
        sys.exit(1)

    if not db_name:
        db_name = '_' + hashlib.sha1(site.encode()).hexdigest()[:16]

    from frappe.installer import install_db, make_site_dirs
    from frappe.installer import install_app as _install_app
    import frappe.utils.scheduler

    frappe.init(site=site)

    try:
        # enable scheduler post install?
        enable_scheduler = _is_scheduler_enabled()
    except Exception:
        enable_scheduler = False

    make_site_dirs()

    installing = None
    try:
        installing = touch_file(get_site_path('locks', 'installing.lock'))

        install_db(root_login=mariadb_root_username,
                   root_password=mariadb_root_password,
                   db_name=db_name,
                   admin_password=admin_password,
                   verbose=verbose,
                   source_sql=source_sql,
                   force=force,
                   reinstall=reinstall,
                   db_type=db_type,
                   db_host=db_host,
                   db_port=db_port,
                   db_password=db_password,
                   no_mariadb_socket=no_mariadb_socket)

        apps_to_install = ['frappe'] + (frappe.conf.get("install_apps")
                                        or []) + (list(install_apps) or [])
        for app in apps_to_install:
            _install_app(app, verbose=verbose, set_as_patched=not source_sql)

        frappe.utils.scheduler.toggle_scheduler(enable_scheduler)
        frappe.db.commit()

        scheduler_status = "disabled" if frappe.utils.scheduler.is_scheduler_disabled(
        ) else "enabled"
        print("*** Scheduler is", scheduler_status, "***")

    except frappe.exceptions.ImproperDBConfigurationError:
        _drop_site(site,
                   mariadb_root_username,
                   mariadb_root_password,
                   force=True)

    finally:
        if installing and os.path.exists(installing):
            os.remove(installing)

        frappe.destroy()
Beispiel #35
0
def get_lock_path(name):
    name = name.lower()
    locks_dir = "locks"
    lock_path = get_site_path(locks_dir, name + ".lock")
    return lock_path
Beispiel #36
0
def get_lock_path(name):
    name = name.lower()
    locks_dir = 'locks'
    lock_path = get_site_path(locks_dir, name + '.lock')
    return lock_path
Beispiel #37
0
def _new_site(
    db_name,
    site,
    mariadb_root_username=None,
    mariadb_root_password=None,
    admin_password=None,
    verbose=False,
    install_apps=None,
    source_sql=None,
    force=False,
    no_mariadb_socket=False,
    reinstall=False,
    db_password=None,
    db_type=None,
    db_host=None,
    db_port=None,
    new_site=False,
):
    """Install a new Frappe site"""

    if not force and os.path.exists(site):
        print("Site {0} already exists".format(site))
        sys.exit(1)

    if no_mariadb_socket and not db_type == "mariadb":
        print("--no-mariadb-socket requires db_type to be set to mariadb.")
        sys.exit(1)

    if not db_name:
        import hashlib
        db_name = "_" + hashlib.sha1(site.encode()).hexdigest()[:16]

    frappe.init(site=site)

    from frappe.commands.scheduler import _is_scheduler_enabled
    from frappe.utils import get_site_path, scheduler, touch_file

    try:
        # enable scheduler post install?
        enable_scheduler = _is_scheduler_enabled()
    except Exception:
        enable_scheduler = False

    make_site_dirs()

    installing = touch_file(get_site_path("locks", "installing.lock"))

    install_db(
        root_login=mariadb_root_username,
        root_password=mariadb_root_password,
        db_name=db_name,
        admin_password=admin_password,
        verbose=verbose,
        source_sql=source_sql,
        force=force,
        reinstall=reinstall,
        db_password=db_password,
        db_type=db_type,
        db_host=db_host,
        db_port=db_port,
        no_mariadb_socket=no_mariadb_socket,
    )
    apps_to_install = (["frappe"] + (frappe.conf.get("install_apps") or []) +
                       (list(install_apps) or []))

    for app in apps_to_install:
        install_app(app, verbose=verbose, set_as_patched=not source_sql)

    os.remove(installing)

    scheduler.toggle_scheduler(enable_scheduler)
    frappe.db.commit()

    scheduler_status = ("disabled"
                        if frappe.utils.scheduler.is_scheduler_disabled() else
                        "enabled")
    print("*** Scheduler is", scheduler_status, "***")
Beispiel #38
0
def get_lock_path(name):
	name = name.lower()
	locks_dir = 'locks'
	lock_path = get_site_path(locks_dir, name + '.lock')
	return lock_path
Beispiel #39
0
def pull_backup(context,
                site,
                remote,
                api_key,
                api_secret,
                mariadb_root_username=None,
                mariadb_root_password=None):
    site = get_site(context)
    frappe.init(site=site)

    site_path = get_site_path()
    remote_backup_dir = os.path.join(site_path, 'private/remote_backups')

    config_file_path = os.path.join(site_path, "pull_backup.json")
    config = frappe._dict()
    if os.path.exists(config_file_path):
        with open(config_file_path, 'r') as f:
            config = frappe._dict(json.loads(f.read()))

    remote = remote or config.remote_url
    if not remote:
        print('Remote URL not provided')
        sys.exit(1)

    if not remote.endswith('/'):
        remote = remote + '/'

    api_key = api_key or config.api_key
    api_secret = api_secret or config.api_secret
    if not api_key or not api_secret:
        print('API Key not provided')
        sys.exit(1)

    base_url = remote
    backups_query_url = base_url + "api/method/frappe.utils.backups.fetch_latest_backups"

    def get_download_url(fn):
        return base_url + "api/method/pull_backup.download_backup?{0}".format(
            urlencode({'filename': fn}))

    api_concat = "{}:{}".format(api_key, api_secret)
    api_encoded = base64.b64encode(api_concat.encode('utf-8'))
    api_encoded = api_encoded.decode()
    headers = {'Authorization': "Basic {}".format(api_encoded)}

    query_response = requests.get(backups_query_url, headers=headers)
    r = query_response.json().get('message')

    files_remote = frappe._dict({
        'database': r.get('database'),
        'public': r.get('public'),
        'private': r.get('private')
    })
    files_local = frappe._dict({})

    if not files_remote.database:
        print('No backup available in remote site')
        sys.exit(1)

    if not os.path.exists(remote_backup_dir):
        os.makedirs(remote_backup_dir)

    to_remove = glob.glob(os.path.join(remote_backup_dir, '*'))
    for fn in to_remove:
        os.remove(fn)

    for filetype in files_remote:
        filename = os.path.basename(
            files_remote.get(filetype)) if files_remote.get(filetype) else None
        files_remote[filetype] = filename
        files_local[filetype] = os.path.join(remote_backup_dir,
                                             filename) if filename else None

    for filetype, filename in files_remote.items():
        print("{0}: {1}".format(filetype, filename))

    for filetype, filename in files_remote.items():
        if filename:
            download_url = get_download_url(filename)
            print("Downloading {0}".format(download_url))

            download_response = requests.get(download_url, headers=headers)

            if is_downloadable(download_response):
                open(files_local.get(filetype),
                     'wb').write(download_response.content)
            else:
                print('Invalid file received')
                sys.exit(1)

    mariadb_root_username = mariadb_root_username or config.mariadb_root_username
    mariadb_root_password = mariadb_root_password or config.mariadb_root_password

    restore_command = "bench --site {0} --force restore '{1}'".format(
        site, files_local.database)
    if files_local.public:
        restore_command += " --with-public-files '{0}'".format(
            files_local.public)
    if files_local.private:
        restore_command += " --with-private-files '{0}'".format(
            files_local.private)
    if mariadb_root_username:
        restore_command += " --mariadb-root-username '{0}'".format(
            mariadb_root_username)
    if mariadb_root_password:
        restore_command += " --mariadb-root-password '{0}'".format(
            mariadb_root_password)

    print("$ " + restore_command)
    restore_status = os.system(restore_command)
    if restore_status != 0:
        sys.exit(1)

    migrate_command = "bench --site {0} migrate".format(site)
    print("$ " + migrate_command)
    os.system(migrate_command)
Beispiel #40
0
def get_snapshots_path():
    return get_site_path("private", "snapshots")