Ejemplo n.º 1
0
def execute_patch(patchmodule, method=None, methodargs=None):
	"""execute the patch"""
	success = False
	block_user(True)
	frappe.db.begin()
	try:
		log('Executing %s in %s' % (patchmodule or str(methodargs), frappe.db.cur_db_name))
		if patchmodule:
			if patchmodule.startswith("execute:"):
				exec patchmodule.split("execute:")[1] in globals()
			else:
				frappe.get_attr(patchmodule + ".execute")()
			update_patch_log(patchmodule)
		elif method:
			method(**methodargs)
			
		frappe.db.commit()
		success = True
	except Exception, e:
		frappe.db.rollback()
		tb = frappe.get_traceback()
		log(tb)
		import os
		if frappe.request:
			add_to_patch_log(tb)
Ejemplo n.º 2
0
def get_bootinfo():
	"""build and return boot info"""
	frappe.set_user_lang(frappe.session.user)
	bootinfo = frappe._dict()
	hooks = frappe.get_hooks()
	doclist = []

	# user
	get_user(bootinfo)

	# system info
	bootinfo['sysdefaults'] = frappe.defaults.get_defaults()
	bootinfo['server_date'] = frappe.utils.nowdate()

	if frappe.session['user'] != 'Guest':
		bootinfo['user_info'] = get_fullnames()
		bootinfo['sid'] = frappe.session['sid'];

	# home page
	bootinfo.modules = {}
	for app in frappe.get_installed_apps():
		try:
			bootinfo.modules.update(frappe.get_attr(app + ".config.desktop.get_data")() or {})
		except ImportError:
			pass
		except AttributeError:
			pass

	bootinfo.module_app = frappe.local.module_app
	bootinfo.hidden_modules = frappe.db.get_global("hidden_modules")
	bootinfo.doctype_icons = dict(frappe.db.sql("""select name, icon from
		tabDocType where ifnull(icon,'')!=''"""))
	bootinfo.single_types = frappe.db.sql_list("""select name from tabDocType where ifnull(issingle,0)=1""")
	add_home_page(bootinfo, doclist)
	bootinfo.page_info = get_allowed_pages()
	load_translations(bootinfo)
	add_timezone_info(bootinfo)
	load_conf_settings(bootinfo)
	load_print(bootinfo, doclist)
	doclist.extend(get_meta_bundle("Page"))
	bootinfo.home_folder = frappe.db.get_value("File", {"is_home_folder": 1})

	# ipinfo
	if frappe.session['data'].get('ipinfo'):
		bootinfo['ipinfo'] = frappe.session['data']['ipinfo']

	# add docs
	bootinfo['docs'] = doclist

	for method in hooks.boot_session or []:
		frappe.get_attr(method)(bootinfo)

	if bootinfo.lang:
		bootinfo.lang = unicode(bootinfo.lang)
	bootinfo['versions'] = {k: v['version'] for k, v in get_versions().items()}

	bootinfo.error_report_email = frappe.get_hooks("error_report_email")
	bootinfo.calendars = sorted(frappe.get_hooks("calendars"))

	return bootinfo
Ejemplo n.º 3
0
def main(app=None, module=None, doctype=None, verbose=False, tests=(), force=False):
	frappe.flags.print_messages = verbose
	frappe.flags.in_test = True

	if not frappe.db:
		frappe.connect()

	# if not frappe.conf.get("db_name").startswith("test_"):
	# 	raise Exception, 'db_name must start with "test_"'

	# workaround! since there is no separate test db
	frappe.clear_cache()
	frappe.utils.scheduler.disable_scheduler()
	set_test_email_config()

	if verbose:
		print 'Running "before_tests" hooks'
	for fn in frappe.get_hooks("before_tests", app_name=app):
		frappe.get_attr(fn)()

	if doctype:
		ret = run_tests_for_doctype(doctype, verbose=verbose, tests=tests, force=force)
	elif module:
		ret = run_tests_for_module(module, verbose=verbose, tests=tests)
	else:
		ret = run_all_tests(app, verbose)

	frappe.db.commit()

	# workaround! since there is no separate test db
	frappe.clear_cache()

	return ret
Ejemplo n.º 4
0
def execute_patch(patchmodule, method=None, methodargs=None):
    """execute the patch"""
    success = False
    block_user(True)
    frappe.db.begin()
    try:
        log(
            "Executing {patch} in {site} ({db})".format(
                patch=patchmodule or str(methodargs), site=frappe.local.site, db=frappe.db.cur_db_name
            )
        )
        if patchmodule:
            if patchmodule.startswith("execute:"):
                exec patchmodule.split("execute:")[1] in globals()
            else:
                frappe.get_attr(patchmodule + ".execute")()
            update_patch_log(patchmodule)
        elif method:
            method(**methodargs)

        frappe.db.commit()
        success = True
    except Exception, e:
        frappe.db.rollback()
        tb = frappe.get_traceback()
        log(tb)
Ejemplo n.º 5
0
def scheduler_task(site, event, handler, now=False):
	from frappe.utils.scheduler import log
	traceback = ""
	task_logger.info('running {handler} for {site} for event: {event}'.format(handler=handler, site=site, event=event))
	try:
		frappe.init(site=site)
		if not create_lock(handler):
			return
		if not now:
			frappe.connect(site=site)
		frappe.get_attr(handler)()

	except Exception:
		frappe.db.rollback()
		traceback = log(handler, "Method: {event}, Handler: {handler}".format(event=event, handler=handler))
		task_logger.warn(traceback)
		raise

	else:
		frappe.db.commit()

	finally:
		delete_lock(handler)

		if not now:
			frappe.destroy()

	task_logger.info('ran {handler} for {site} for event: {event}'.format(handler=handler, site=site, event=event))
Ejemplo n.º 6
0
def install_app(name, verbose=False, set_as_patched=True):
	frappe.flags.in_install_app = name
	frappe.clear_cache()

	app_hooks = frappe.get_hooks(app_name=name)
	installed_apps = frappe.get_installed_apps()

	if name not in frappe.get_all_apps(with_frappe=True):
		raise Exception("App not in apps.txt")

	if name in installed_apps:
		print "App Already Installed"
		frappe.msgprint(_("App Already Installed"))
		return

	if name != "frappe":
		frappe.only_for("System Manager")

	for before_install in app_hooks.before_install or []:
		frappe.get_attr(before_install)()

	if name != "frappe":
		add_module_defs(name)
	sync_for(name, force=True, sync_everything=True, verbose=verbose)

	add_to_installed_apps(name)

	if set_as_patched:
		set_all_patches_as_completed(name)

	for after_install in app_hooks.after_install or []:
		frappe.get_attr(after_install)()

	frappe.flags.in_install_app = False
Ejemplo n.º 7
0
def get():
	"""get session boot info"""
	from frappe.core.doctype.notification_count.notification_count import \
		get_notification_info_for_boot, get_notifications
	from frappe.boot import get_bootinfo, get_startup_js

	bootinfo = None
	if not getattr(frappe.conf,'disable_session_cache', None):
		# check if cache exists
		bootinfo = frappe.cache().get_value('bootinfo:' + frappe.session.user)
		if bootinfo:
			bootinfo['from_cache'] = 1
			bootinfo["user"]["recent"] = json.dumps(frappe.cache().get_value("recent:" + frappe.session.user))
			bootinfo["notification_info"].update(get_notifications())

	if not bootinfo:
		if not frappe.cache().get_stats():
			frappe.msgprint(_("memcached is not working / stopped. Please start memcached for best results."))

		# if not create it
		bootinfo = get_bootinfo()
		bootinfo["notification_info"] = get_notification_info_for_boot()
		frappe.cache().set_value('bootinfo:' + frappe.session.user, bootinfo)

	bootinfo["metadata_version"] = frappe.cache().get_value("metadata_version")
	if not bootinfo["metadata_version"]:
		bootinfo["metadata_version"] = frappe.reset_metadata_version()

	bootinfo["startup_js"] = get_startup_js()
	for hook in frappe.get_hooks("extend_bootinfo"):
		frappe.get_attr(hook)(bootinfo=bootinfo)

	return bootinfo
Ejemplo n.º 8
0
def execute_patch(patchmodule, method=None, methodargs=None):
	"""execute the patch"""
	block_user(True)
	frappe.db.begin()

	try:
		log('Executing {patch} in {site} ({db})'.format(patch=patchmodule or str(methodargs),
			site=frappe.local.site, db=frappe.db.cur_db_name))
		if patchmodule:
			if patchmodule.startswith("execute:"):
				exec patchmodule.split("execute:")[1] in globals()
			else:
				frappe.get_attr(patchmodule.split()[0] + ".execute")()
			update_patch_log(patchmodule)
		elif method:
			method(**methodargs)

	except Exception:
		frappe.db.rollback()
		raise

	else:
		frappe.db.commit()
		block_user(False)
		log('Success')

	return True
Ejemplo n.º 9
0
def migrate(verbose=True, rebuild_website=False):
	'''Migrate all apps to the latest version, will:
	- run before migrate hooks
	- run patches
	- sync doctypes (schema)
	- sync fixtures
	- sync desktop icons
	- sync web pages (from /www)
	- sync web pages (from /www)
	- run after migrate hooks
	'''

	touched_tables_file = frappe.get_site_path('touched_tables.json')
	if os.path.exists(touched_tables_file):
		os.remove(touched_tables_file)

	try:
		frappe.flags.touched_tables = set()
		frappe.flags.in_migrate = True
		clear_global_cache()

		#run before_migrate hooks
		for app in frappe.get_installed_apps():
			for fn in frappe.get_hooks('before_migrate', app_name=app):
				frappe.get_attr(fn)()

		# run patches
		frappe.modules.patch_handler.run_all()
		# sync
		frappe.model.sync.sync_all(verbose=verbose)
		frappe.translate.clear_cache()
		sync_fixtures()
		sync_customizations()
		sync_desktop_icons()
		sync_languages()

		frappe.get_doc('Portal Settings', 'Portal Settings').sync_menu()

		# syncs statics
		render.clear_cache()

		# add static pages to global search
		router.sync_global_search()

		#run after_migrate hooks
		for app in frappe.get_installed_apps():
			for fn in frappe.get_hooks('after_migrate', app_name=app):
				frappe.get_attr(fn)()

		frappe.db.commit()

		clear_notifications()

		frappe.publish_realtime("version-update")
		frappe.flags.in_migrate = False
	finally:
		with open(touched_tables_file, 'w') as f:
			json.dump(list(frappe.flags.touched_tables), f, sort_keys=True, indent=4)
		frappe.flags.touched_tables.clear()
Ejemplo n.º 10
0
def get_website_settings():
	hooks = frappe.get_hooks()

	all_top_items = frappe.db.sql("""\
		select * from `tabTop Bar Item`
		where parent='Website Settings' and parentfield='top_bar_items'
		order by idx asc""", as_dict=1)

	top_items = [d for d in all_top_items if not d['parent_label']]

	# attach child items to top bar
	for d in all_top_items:
		if d['parent_label']:
			for t in top_items:
				if t['label']==d['parent_label']:
					if not 'child_items' in t:
						t['child_items'] = []
					t['child_items'].append(d)
					break

	context = frappe._dict({
		'top_bar_items': top_items,
		'footer_items': frappe.db.sql("""\
			select * from `tabTop Bar Item`
			where parent='Website Settings' and parentfield='footer_items'
			order by idx asc""", as_dict=1),
		"post_login": [
			{"label": "Reset Password", "url": "update-password", "icon": "icon-key"},
			{"label": "Logout", "url": "?cmd=web_logout", "icon": "icon-signout"}
		]
	})

	settings = frappe.get_doc("Website Settings", "Website Settings")
	for k in ["banner_html", "brand_html", "copyright", "twitter_share_via",
		"favicon", "facebook_share", "google_plus_one", "twitter_share", "linked_in_share",
		"disable_signup"]:
		if hasattr(settings, k):
			context[k] = settings.get(k)

	if not context.get("favicon"):
		context["favicon"] = "/assets/frappe/images/favicon.ico"

	if settings.address:
		context["footer_address"] = settings.address

	for k in ["facebook_share", "google_plus_one", "twitter_share", "linked_in_share",
		"disable_signup"]:
		context[k] = int(context.get(k) or 0)

	context.url = quote(str(get_request_site_address(full_address=True)), safe="/:")
	context.encoded_title = quote(encode(context.title or ""), str(""))

	for update_website_context in hooks.update_website_context or []:
		frappe.get_attr(update_website_context)(context)

	context.web_include_js = hooks.web_include_js or []
	context.web_include_css = hooks.web_include_css or []

	return context
Ejemplo n.º 11
0
def install_app(name, verbose=False, set_as_patched=True):
	frappe.flags.in_install = name
	frappe.flags.ignore_in_install = False

	frappe.clear_cache()
	app_hooks = frappe.get_hooks(app_name=name)
	installed_apps = frappe.get_installed_apps()

	# install pre-requisites
	if app_hooks.required_apps:
		for app in app_hooks.required_apps:
			install_app(app)

	frappe.flags.in_install = name
	frappe.clear_cache()

	if name not in frappe.get_all_apps():
		raise Exception("App not in apps.txt")

	if name in installed_apps:
		frappe.msgprint(_("App {0} already installed").format(name))
		return

	print("\nInstalling {0}...".format(name))

	if name != "frappe":
		frappe.only_for("System Manager")

	for before_install in app_hooks.before_install or []:
		out = frappe.get_attr(before_install)()
		if out==False:
			return

	if name != "frappe":
		add_module_defs(name)

	sync_for(name, force=True, sync_everything=True, verbose=verbose, reset_permissions=True)

	sync_from_app(name)

	add_to_installed_apps(name)

	frappe.get_doc('Portal Settings', 'Portal Settings').sync_menu()

	if set_as_patched:
		set_all_patches_as_completed(name)

	for after_install in app_hooks.after_install or []:
		frappe.get_attr(after_install)()

	sync_fixtures(name)
	sync_customizations(name)

	for after_sync in app_hooks.after_sync or []:
		frappe.get_attr(after_sync)() #

	frappe.flags.in_install = False
Ejemplo n.º 12
0
def get_bootinfo():
	"""build and return boot info"""
	frappe.set_user_lang(frappe.session.user)
	bootinfo = frappe._dict()
	hooks = frappe.get_hooks()
	doclist = []

	# user
	get_user(bootinfo)

	# system info
	bootinfo.sysdefaults = frappe.defaults.get_defaults()
	bootinfo.server_date = frappe.utils.nowdate()

	if frappe.session['user'] != 'Guest':
		bootinfo.user_info = get_fullnames()
		bootinfo.sid = frappe.session['sid'];

	bootinfo.modules = {}
	bootinfo.module_list = []
	load_desktop_icons(bootinfo)

	bootinfo.module_app = frappe.local.module_app
	bootinfo.single_types = frappe.db.sql_list("""select name from tabDocType
		where issingle=1""")
	add_home_page(bootinfo, doclist)
	bootinfo.page_info = get_allowed_pages()
	load_translations(bootinfo)
	add_timezone_info(bootinfo)
	load_conf_settings(bootinfo)
	load_print(bootinfo, doclist)
	doclist.extend(get_meta_bundle("Page"))
	bootinfo.home_folder = frappe.db.get_value("File", {"is_home_folder": 1})

	# ipinfo
	if frappe.session.data.get('ipinfo'):
		bootinfo.ipinfo = frappe.session['data']['ipinfo']

	# add docs
	bootinfo.docs = doclist

	for method in hooks.boot_session or []:
		frappe.get_attr(method)(bootinfo)

	if bootinfo.lang:
		bootinfo.lang = unicode(bootinfo.lang)
	bootinfo.versions = {k: v['version'] for k, v in get_versions().items()}

	bootinfo.error_report_email = frappe.get_hooks("error_report_email")
	bootinfo.calendars = sorted(frappe.get_hooks("calendars"))
	bootinfo.treeviews = frappe.get_hooks("treeviews") or []
	bootinfo.email_accounts = frappe.get_all('User Emails', fields=['email_account', 'email_id'],
		filters=dict(parent=frappe.session.user), order_by='idx')
	bootinfo.lang_dict = get_lang_dict()

	return bootinfo
Ejemplo n.º 13
0
def clear_cache(path=None):
	frappe.cache().delete_value("website_generator_routes")
	delete_page_cache(path)
	if not path:
		clear_sitemap()
		frappe.clear_cache("Guest")
		frappe.cache().delete_value("_website_pages")

	for method in frappe.get_hooks("website_clear_cache"):
		frappe.get_attr(method)(path)
Ejemplo n.º 14
0
def get_website_settings():
	hooks = frappe.get_hooks()
	context = frappe._dict({
		'top_bar_items': get_items('top_bar_items'),
		'footer_items': get_items('footer_items'),
		"post_login": [
			{"label": _("My Account"), "url": "/me"},
#			{"class": "divider"},
			{"label": _("Logout"), "url": "/?cmd=web_logout"}
		]
	})

	settings = frappe.get_single("Website Settings")
	for k in ["banner_html", "brand_html", "copyright", "twitter_share_via",
		"facebook_share", "google_plus_one", "twitter_share", "linked_in_share",
		"disable_signup", "hide_footer_signup", "head_html", "title_prefix",
		"navbar_search"]:
		if hasattr(settings, k):
			context[k] = settings.get(k)

	if settings.address:
		context["footer_address"] = settings.address

	for k in ["facebook_share", "google_plus_one", "twitter_share", "linked_in_share",
		"disable_signup"]:
		context[k] = int(context.get(k) or 0)

	if frappe.request:
		context.url = quote(str(get_request_site_address(full_address=True)), safe="/:")

	context.encoded_title = quote(encode(context.title or ""), str(""))

	for update_website_context in hooks.update_website_context or []:
		frappe.get_attr(update_website_context)(context)

	context.web_include_js = hooks.web_include_js or []

	context.web_include_css = hooks.web_include_css or []

	via_hooks = frappe.get_hooks("website_context")
	for key in via_hooks:
		context[key] = via_hooks[key]
		if key not in ("top_bar_items", "footer_items", "post_login") \
			and isinstance(context[key], (list, tuple)):
			context[key] = context[key][-1]

	add_website_theme(context)

	if not context.get("favicon"):
		context["favicon"] = "/assets/frappe/images/favicon.png"

	if settings.favicon and settings.favicon != "attach_files:":
		context["favicon"] = settings.favicon

	return context
Ejemplo n.º 15
0
def main(app=None, module=None, doctype=None, verbose=False, tests=(),
	force=False, profile=False, junit_xml_output=None, ui_tests=False,
	doctype_list_path=None, skip_test_records=False, failfast=False):
	global unittest_runner

	if doctype_list_path:
		app, doctype_list_path = doctype_list_path.split(os.path.sep, 1)
		with open(frappe.get_app_path(app, doctype_list_path), 'r') as f:
			doctype = f.read().strip().splitlines()

	xmloutput_fh = None
	if junit_xml_output:
		xmloutput_fh = open(junit_xml_output, 'w')
		unittest_runner = xmlrunner_wrapper(xmloutput_fh)
	else:
		unittest_runner = unittest.TextTestRunner

	try:
		frappe.flags.print_messages = verbose
		frappe.flags.in_test = True

		if not frappe.db:
			frappe.connect()

		# if not frappe.conf.get("db_name").startswith("test_"):
		# 	raise Exception, 'db_name must start with "test_"'

		# workaround! since there is no separate test db
		frappe.clear_cache()
		frappe.utils.scheduler.disable_scheduler()
		set_test_email_config()

		if not frappe.flags.skip_before_tests:
			if verbose:
				print('Running "before_tests" hooks')
			for fn in frappe.get_hooks("before_tests", app_name=app):
				frappe.get_attr(fn)()

		if doctype:
			ret = run_tests_for_doctype(doctype, verbose, tests, force, profile)
		elif module:
			ret = run_tests_for_module(module, verbose, tests, profile)
		else:
			ret = run_all_tests(app, verbose, profile, ui_tests, failfast=failfast)

		frappe.db.commit()

		# workaround! since there is no separate test db
		frappe.clear_cache()
		return ret

	finally:
		if xmloutput_fh:
			xmloutput_fh.flush()
			xmloutput_fh.close()
Ejemplo n.º 16
0
def get():
	"""get session boot info"""
	from frappe.desk.notifications import \
		get_notification_info_for_boot, get_notifications
	from frappe.boot import get_bootinfo, get_unseen_notes
	from frappe.limits import get_limits, get_expiry_message

	bootinfo = None
	if not getattr(frappe.conf,'disable_session_cache', None):
		# check if cache exists
		bootinfo = frappe.cache().hget("bootinfo", frappe.session.user)
		if bootinfo:
			bootinfo['from_cache'] = 1
			bootinfo["notification_info"].update(get_notifications())
			bootinfo["user"]["recent"] = json.dumps(\
				frappe.cache().hget("user_recent", frappe.session.user))

	if not bootinfo:
		# if not create it
		bootinfo = get_bootinfo()
		bootinfo["notification_info"] = get_notification_info_for_boot()
		frappe.cache().hset("bootinfo", frappe.session.user, bootinfo)
		try:
			frappe.cache().ping()
		except redis.exceptions.ConnectionError:
			message = _("Redis cache server not running. Please contact Administrator / Tech support")
			if 'messages' in bootinfo:
				bootinfo['messages'].append(message)
			else:
				bootinfo['messages'] = [message]

		# check only when clear cache is done, and don't cache this
		if frappe.local.request:
			bootinfo["change_log"] = get_change_log()
			bootinfo["in_setup_wizard"] = not cint(frappe.db.get_single_value('System Settings', 'setup_complete'))
			bootinfo["is_first_startup"] = cint(frappe.db.get_single_value('System Settings', 'is_first_startup'))

	bootinfo["metadata_version"] = frappe.cache().get_value("metadata_version")
	if not bootinfo["metadata_version"]:
		bootinfo["metadata_version"] = frappe.reset_metadata_version()

	bootinfo.notes = get_unseen_notes()

	for hook in frappe.get_hooks("extend_bootinfo"):
		frappe.get_attr(hook)(bootinfo=bootinfo)

	bootinfo["lang"] = frappe.translate.get_user_lang()
	bootinfo["disable_async"] = frappe.conf.disable_async

	# limits
	bootinfo.limits = get_limits()
	bootinfo.expiry_message = get_expiry_message()

	return bootinfo
Ejemplo n.º 17
0
def clear_cache(path=None):
	if path:
		delete_page_cache(path)
	else:
		clear_sitemap()
		frappe.clear_cache("Guest")
		frappe.cache().delete_value("_website_pages")
		clear_permissions()

	for method in frappe.get_hooks("website_clear_cache"):
		frappe.get_attr(method)(path)
Ejemplo n.º 18
0
def get_bootinfo():
	"""build and return boot info"""
	frappe.set_user_lang(frappe.session.user)
	bootinfo = frappe._dict()
	hooks = frappe.get_hooks()
	doclist = []

	# user
	get_user(bootinfo)

	# system info
	bootinfo['sysdefaults'] = frappe.defaults.get_defaults()
	bootinfo['server_date'] = frappe.utils.nowdate()
	bootinfo["send_print_in_body_and_attachment"] = frappe.db.get_value("Outgoing Email Settings",
		None, "send_print_in_body_and_attachment")

	if frappe.session['user'] != 'Guest':
		bootinfo['user_info'] = get_fullnames()
		bootinfo['sid'] = frappe.session['sid'];

	# home page
	bootinfo.modules = {}
	for app in frappe.get_installed_apps():
		try:
			bootinfo.modules.update(frappe.get_attr(app + ".config.desktop.data") or {})
		except ImportError:
			pass

	bootinfo.module_app = frappe.local.module_app
	bootinfo.hidden_modules = frappe.db.get_global("hidden_modules")
	bootinfo.doctype_icons = dict(frappe.db.sql("""select name, icon from
		tabDocType where ifnull(icon,'')!=''"""))
	bootinfo.doctype_icons.update(dict(frappe.db.sql("""select name, icon from
		tabPage where ifnull(icon,'')!=''""")))

	add_home_page(bootinfo, doclist)
	add_allowed_pages(bootinfo)
	load_translations(bootinfo)
	load_conf_settings(bootinfo)

	# ipinfo
	if frappe.session['data'].get('ipinfo'):
		bootinfo['ipinfo'] = frappe.session['data']['ipinfo']

	# add docs
	bootinfo['docs'] = doclist

	for method in hooks.boot_session or []:
		frappe.get_attr(method)(bootinfo)

	if bootinfo.lang:
		bootinfo.lang = unicode(bootinfo.lang)

	return bootinfo
Ejemplo n.º 19
0
	def execute_postprocess(self, status):
		# Execute post process
		postprocess_method_path = self.get_plan().postprocess_method

		if postprocess_method_path:
			frappe.get_attr(postprocess_method_path)({
				"status": status,
				"stats": {
					"push_insert": self.push_insert,
					"push_update": self.push_update,
					"push_delete": self.push_delete,
					"pull_insert": self.pull_insert,
					"pull_update": self.pull_update
				}
			})
Ejemplo n.º 20
0
def get_context(context):
	if frappe.session.user == "Guest":
		frappe.throw(_("You need to be logged in to access this page."), frappe.PermissionError)

	context["my_account_list"] = []

	for element in frappe.get_hooks("my_account_context"):
		if isinstance(element, dict):
			context["my_account_list"].append(element)
		else:
			frappe.get_attr(element)(context)

	info = get_fullname_and_avatar(frappe.session.user)
	context["fullname"] = info.fullname
	context["user_image"] = info.avatar
Ejemplo n.º 21
0
def migrate(verbose=True, rebuild_website=False):
	'''Migrate all apps to the latest version, will:
	- run before migrate hooks
	- run patches
	- sync doctypes (schema)
	- sync fixtures
	- sync desktop icons
	- sync web pages (from /www)
	- sync web pages (from /www)
	- run after migrate hooks
	'''
	frappe.flags.in_migrate = True
	clear_global_cache()

	#run before_migrate hooks
	for app in frappe.get_installed_apps():
		for fn in frappe.get_hooks('before_migrate', app_name=app):
			frappe.get_attr(fn)()

	# run patches
	frappe.modules.patch_handler.run_all()
	# sync
	frappe.model.sync.sync_all(verbose=verbose)
	frappe.translate.clear_cache()
	sync_fixtures()
	sync_customizations()
	sync_desktop_icons()
	sync_languages()

	frappe.get_doc('Portal Settings', 'Portal Settings').sync_menu()

	# syncs statics
	render.clear_cache()

	# add static pages to global search
	router.sync_global_search()

	#run after_migrate hooks
	for app in frappe.get_installed_apps():
		for fn in frappe.get_hooks('after_migrate', app_name=app):
			frappe.get_attr(fn)()

	frappe.db.commit()

	clear_notifications()

	frappe.publish_realtime("version-update")
	frappe.flags.in_migrate = False
Ejemplo n.º 22
0
def get_hook_method(hook_name, fallback=None):
	method = (frappe.get_hooks().get(hook_name))
	if method:
		method = frappe.get_attr(method[0])
		return method
	if fallback:
		return fallback
Ejemplo n.º 23
0
	def _get_home_page():
		home_page = None

		get_website_user_home_page = frappe.get_hooks('get_website_user_home_page')
		if get_website_user_home_page:
			home_page = frappe.get_attr(get_website_user_home_page[-1])(frappe.session.user)

		if not home_page:
			role_home_page = frappe.get_hooks("role_home_page")
			if role_home_page:
				for role in frappe.get_roles():
					if role in role_home_page:
						home_page = role_home_page[role][-1]
						break

		if not home_page:
			home_page = frappe.get_hooks("home_page")
			if home_page:
				home_page = home_page[-1]

		if not home_page:
			home_page = frappe.db.get_value("Website Settings", None, "home_page") or "login"

		home_page = home_page.strip('/')

		return home_page
Ejemplo n.º 24
0
	def caller(*args, **kwargs):
		region = get_region()
		fn_name = inspect.getmodule(fn).__name__ + '.' + fn.__name__
		if region in regional_overrides and fn_name in regional_overrides[region]:
			return frappe.get_attr(regional_overrides[region][fn_name])(*args, **kwargs)
		else:
			return fn(*args, **kwargs)
Ejemplo n.º 25
0
def clear_cache(path=None):
	cache = frappe.cache()

	if path:
		delete_page_cache(path)

	else:
		for p in frappe.db.sql_list("""select name from `tabWebsite Route`"""):
			if p is not None:
				delete_page_cache(p)

		cache.delete_value("home_page")
		clear_permissions()

	for method in frappe.get_hooks("website_clear_cache"):
		frappe.get_attr(method)(path)
Ejemplo n.º 26
0
def get_website_user_home_page(user):
	home_page_method = frappe.get_hooks('get_website_user_home_page')
	if home_page_method:
		home_page = frappe.get_attr(home_page_method[-1])(user)
		return '/' + home_page.strip('/')
	else:
		return '/me'
Ejemplo n.º 27
0
def get_notifications_for_doctypes(config, notification_count):
	"""Notifications for DocTypes"""
	can_read = frappe.get_user().get_can_read()
	open_count_doctype = {}

	for d in config.for_doctype:
		if d in can_read:
			condition = config.for_doctype[d]

			if d in notification_count:
				open_count_doctype[d] = notification_count[d]
			else:
				try:
					if isinstance(condition, dict):
						result = len(frappe.get_list(d, fields=["name"],
							filters=condition, limit_page_length = 21, as_list=True, ignore_ifnull=True))
					else:
						result = frappe.get_attr(condition)()

				except frappe.PermissionError:
					frappe.msgprint("Permission Error in notifications for {0}".format(d))

				except Exception, e:
					# OperationalError: (1412, 'Table definition has changed, please retry transaction')
					if e.args[0]!=1412:
						raise

				else:
					open_count_doctype[d] = result
					frappe.cache().hset("notification_count:" + d, frappe.session.user, result)
Ejemplo n.º 28
0
def execute(method):
	frappe.connect()
	ret = frappe.get_attr(method)()
	frappe.db.commit()
	frappe.destroy()
	if ret:
		print ret
Ejemplo n.º 29
0
def execute(context, method, args=None, kwargs=None):
	"execute a function"
	for site in context.sites:
		try:
			frappe.init(site=site)
			frappe.connect()

			if args:
				args = eval(args)
			else:
				args = ()

			if kwargs:
				kwargs = eval(args)
			else:
				kwargs = {}

			ret = frappe.get_attr(method)(*args, **kwargs)

			if frappe.db:
				frappe.db.commit()
		finally:
			frappe.destroy()
		if ret:
			print json.dumps(ret)
Ejemplo n.º 30
0
def get_allowed_functions_for_jenv():
	import frappe
	import frappe.utils
	import frappe.utils.data
	from frappe.utils.autodoc import automodule, get_version
	from frappe.model.document import get_controller
	from frappe.website.utils import get_shade
	from frappe.modules import scrub
	import mimetypes

	datautils = {}
	for key, obj in frappe.utils.data.__dict__.items():
		if key.startswith("_"):
			# ignore
			continue

		if hasattr(obj, "__call__"):
			# only allow functions
			datautils[key] = obj

	if "_" in frappe.local.form_dict:
		del frappe.local.form_dict["_"]

	return {
		# make available limited methods of frappe
		"frappe": {
			"_": frappe._,
			"get_url": frappe.utils.get_url,
			"format_value": frappe.format_value,
			"format_date": frappe.utils.data.global_date_format,
			"form_dict": frappe.local.form_dict,
			"local": frappe.local,
			"get_hooks": frappe.get_hooks,
			"get_meta": frappe.get_meta,
			"get_doc": frappe.get_doc,
			"db": {
				"get_value": frappe.db.get_value,
			},
			"get_list": frappe.get_list,
			"get_all": frappe.get_all,
			"utils": datautils,
			"user": getattr(frappe.local, "session", None) and frappe.local.session.user or "Guest",
			"date_format": frappe.db.get_default("date_format") or "yyyy-mm-dd",
			"get_fullname": frappe.utils.get_fullname,
			"get_gravatar": frappe.utils.get_gravatar,
			"full_name": getattr(frappe.local, "session", None) and frappe.local.session.data.full_name or "Guest",
			"render_template": frappe.render_template
		},
		"autodoc": {
			"get_version": get_version,
			"automodule": automodule,
			"get_controller": get_controller
		},
		"get_visible_columns": \
			frappe.get_attr("frappe.templates.pages.print.get_visible_columns"),
		"_": frappe._,
		"get_shade": get_shade,
		"scrub": scrub,
		"guess_mimetype": mimetypes.guess_type
	}
Ejemplo n.º 31
0
def run_setup_success(args):
    for hook in frappe.get_hooks("setup_wizard_success"):
        frappe.get_attr(hook)(args)
    install_fixtures.install()
Ejemplo n.º 32
0
 def run_trigger(self, event='on_login'):
     for method in frappe.get_hooks().get(event, []):
         frappe.call(frappe.get_attr(method), login_manager=self)
Ejemplo n.º 33
0
def get_bootinfo():
	"""build and return boot info"""
	frappe.set_user_lang(frappe.session.user)
	bootinfo = frappe._dict()
	hooks = frappe.get_hooks()
	doclist = []

	# user
	get_user(bootinfo)

	# system info
	bootinfo['sysdefaults'] = frappe.defaults.get_defaults()
	bootinfo['server_date'] = frappe.utils.nowdate()

	if frappe.session['user'] != 'Guest':
		bootinfo['user_info'] = get_fullnames()
		bootinfo['sid'] = frappe.session['sid'];

	bootinfo.modules = {}
	bootinfo.module_list = []
	for app in frappe.get_installed_apps(frappe_last=True):
		try:
			modules = frappe.get_attr(app + ".config.desktop.get_data")() or {}
			if isinstance(modules, dict):
				bootinfo.modules.update(modules)
			else:
				for m in modules:
					bootinfo.modules[m['module_name']] = m
					bootinfo.module_list.append(m['module_name'])
		except ImportError:
			pass
		except AttributeError:
			pass

	bootinfo.module_app = frappe.local.module_app
	bootinfo.hidden_modules = frappe.db.get_global("hidden_modules")
	bootinfo.doctype_icons = dict(frappe.db.sql("""select name, icon from
		tabDocType where ifnull(icon,'')!=''"""))
	bootinfo.single_types = frappe.db.sql_list("""select name from tabDocType where issingle=1""")
	add_home_page(bootinfo, doclist)
	bootinfo.page_info = get_allowed_pages()
	load_translations(bootinfo)
	add_timezone_info(bootinfo)
	load_conf_settings(bootinfo)
	load_print(bootinfo, doclist)
	doclist.extend(get_meta_bundle("Page"))
	bootinfo.home_folder = frappe.db.get_value("File", {"is_home_folder": 1})

	# ipinfo
	if frappe.session['data'].get('ipinfo'):
		bootinfo['ipinfo'] = frappe.session['data']['ipinfo']

	# add docs
	bootinfo['docs'] = doclist

	for method in hooks.boot_session or []:
		frappe.get_attr(method)(bootinfo)

	if bootinfo.lang:
		bootinfo.lang = unicode(bootinfo.lang)
	bootinfo['versions'] = {k: v['version'] for k, v in get_versions().items()}

	bootinfo.error_report_email = frappe.get_hooks("error_report_email")
	bootinfo.calendars = sorted(frappe.get_hooks("calendars"))
	bootinfo["lang_dict"] = get_lang_dict()
	
	return bootinfo
Ejemplo n.º 34
0
def validate_auth_via_hooks():
    for auth_hook in frappe.get_hooks("auth_hooks", []):
        frappe.get_attr(auth_hook)()
Ejemplo n.º 35
0
	def create_charge_on_braintree(self):
		self.configure_braintree()

		redirect_to = self.data.get("redirect_to") or None
		redirect_message = self.data.get("redirect_message") or None

		result = braintree.Transaction.sale(
			{
				"amount": self.data.amount,
				"payment_method_nonce": self.data.payload_nonce,
				"options": {"submit_for_settlement": True},
			}
		)

		if result.is_success:
			self.integration_request.db_set("status", "Completed", update_modified=False)
			self.flags.status_changed_to = "Completed"
			self.integration_request.db_set("output", result.transaction.status, update_modified=False)

		elif result.transaction:
			self.integration_request.db_set("status", "Failed", update_modified=False)
			error_log = frappe.log_error(
				"code: "
				+ str(result.transaction.processor_response_code)
				+ " | text: "
				+ str(result.transaction.processor_response_text),
				"Braintree Payment Error",
			)
			self.integration_request.db_set("error", error_log.error, update_modified=False)
		else:
			self.integration_request.db_set("status", "Failed", update_modified=False)
			for error in result.errors.deep_errors:
				error_log = frappe.log_error(
					"code: " + str(error.code) + " | message: " + str(error.message), "Braintree Payment Error"
				)
				self.integration_request.db_set("error", error_log.error, update_modified=False)

		if self.flags.status_changed_to == "Completed":
			status = "Completed"
			if self.data.reference_doctype and self.data.reference_docname:
				custom_redirect_to = None
				try:
					custom_redirect_to = frappe.get_doc(
						self.data.reference_doctype, self.data.reference_docname
					).run_method("on_payment_authorized", self.flags.status_changed_to)
					braintree_success_page = frappe.get_hooks("braintree_success_page")
					if braintree_success_page:
						custom_redirect_to = frappe.get_attr(braintree_success_page[-1])(self.data)
				except Exception:
					frappe.log_error(frappe.get_traceback())

				if custom_redirect_to:
					redirect_to = custom_redirect_to

			redirect_url = "payment-success"
		else:
			status = "Error"
			redirect_url = "payment-failed"

		if redirect_to:
			redirect_url += "?" + urlencode({"redirect_to": redirect_to})
		if redirect_message:
			redirect_url += "&" + urlencode({"redirect_message": redirect_message})

		return {"redirect_to": redirect_url, "status": status}
Ejemplo n.º 36
0
def call_hook_method(hook, *args, **kwargs):
    for method_name in frappe.get_hooks(hook):
        frappe.get_attr(method_name)(*args, **kwargs)
Ejemplo n.º 37
0
def call_hook_method(hook, *args, **kwargs):
    out = None
    for method_name in frappe.get_hooks(hook):
        out = out or frappe.get_attr(method_name)(*args, **kwargs)

    return out
Ejemplo n.º 38
0
def search_widget(doctype,
                  txt,
                  query=None,
                  searchfield=None,
                  start=0,
                  page_length=20,
                  filters=None,
                  filter_fields=None,
                  as_dict=False,
                  reference_doctype=None,
                  ignore_user_permissions=False):

    start = cint(start)

    if isinstance(filters, string_types):
        filters = json.loads(filters)

    if searchfield:
        sanitize_searchfield(searchfield)

    if not searchfield:
        searchfield = "name"

    standard_queries = frappe.get_hooks().standard_queries or {}

    if query and query.split()[0].lower() != "select":
        # by method
        try:
            is_whitelisted(frappe.get_attr(query))
            frappe.response["values"] = frappe.call(query,
                                                    doctype,
                                                    txt,
                                                    searchfield,
                                                    start,
                                                    page_length,
                                                    filters,
                                                    as_dict=as_dict)
        except Exception as e:
            if frappe.local.conf.developer_mode:
                raise e
            else:
                frappe.respond_as_web_page(title='Invalid Method',
                                           html='Method not found',
                                           indicator_color='red',
                                           http_status_code=404)
            return
    elif not query and doctype in standard_queries:
        # from standard queries
        search_widget(doctype, txt, standard_queries[doctype][0], searchfield,
                      start, page_length, filters)
    else:
        meta = frappe.get_meta(doctype)

        if query:
            frappe.throw(_("This query style is discontinued"))
            # custom query
            # frappe.response["values"] = frappe.db.sql(scrub_custom_query(query, searchfield, txt))
        else:
            if isinstance(filters, dict):
                filters_items = filters.items()
                filters = []
                for f in filters_items:
                    if isinstance(f[1], (list, tuple)):
                        filters.append([doctype, f[0], f[1][0], f[1][1]])
                    else:
                        filters.append([doctype, f[0], "=", f[1]])

            if filters == None:
                filters = []
            or_filters = []

            # build from doctype
            if txt:
                search_fields = ["name"]
                if meta.title_field:
                    search_fields.append(meta.title_field)

                if meta.search_fields:
                    search_fields.extend(meta.get_search_fields())

                for f in search_fields:
                    fmeta = meta.get_field(f.strip())
                    if (doctype not in UNTRANSLATED_DOCTYPES) and (
                            f == "name" or (fmeta and fmeta.fieldtype in [
                                "Data", "Text", "Small Text", "Long Text",
                                "Link", "Select", "Read Only", "Text Editor"
                            ])):
                        or_filters.append(
                            [doctype,
                             f.strip(), "like", "%{0}%".format(txt)])

            if meta.get("fields", {
                    "fieldname": "enabled",
                    "fieldtype": "Check"
            }):
                filters.append([doctype, "enabled", "=", 1])
            if meta.get("fields", {
                    "fieldname": "disabled",
                    "fieldtype": "Check"
            }):
                filters.append([doctype, "disabled", "!=", 1])

            # format a list of fields combining search fields and filter fields
            fields = get_std_fields_list(meta, searchfield or "name")
            if filter_fields:
                fields = list(set(fields + json.loads(filter_fields)))
            formatted_fields = [
                '`tab%s`.`%s`' % (meta.name, f.strip()) for f in fields
            ]

            # find relevance as location of search term from the beginning of string `name`. used for sorting results.
            formatted_fields.append(
                """locate({_txt}, `tab{doctype}`.`name`) as `_relevance`""".
                format(_txt=frappe.db.escape(
                    (txt or "").replace("%", "").replace("@", "")),
                       doctype=doctype))

            # In order_by, `idx` gets second priority, because it stores link count
            from frappe.model.db_query import get_order_by
            order_by_based_on_meta = get_order_by(doctype, meta)
            # 2 is the index of _relevance column
            order_by = "_relevance, {0}, `tab{1}`.idx desc".format(
                order_by_based_on_meta, doctype)

            ignore_permissions = True if doctype == "DocType" else (
                cint(ignore_user_permissions) and has_permission(doctype))

            if doctype in UNTRANSLATED_DOCTYPES:
                page_length = None

            values = frappe.get_list(doctype,
                                     filters=filters,
                                     fields=formatted_fields,
                                     or_filters=or_filters,
                                     limit_start=start,
                                     limit_page_length=page_length,
                                     order_by=order_by,
                                     ignore_permissions=ignore_permissions,
                                     reference_doctype=reference_doctype,
                                     as_list=not as_dict,
                                     strict=False)

            if doctype in UNTRANSLATED_DOCTYPES:
                values = tuple([
                    v for v in list(values) if re.search(
                        re.escape(txt) + ".*", (
                            _(v.name) if as_dict else _(v[0])), re.IGNORECASE)
                ])

            # remove _relevance from results
            if as_dict:
                for r in values:
                    r.pop("_relevance")
                frappe.response["values"] = values
            else:
                frappe.response["values"] = [r[:-1] for r in values]
Ejemplo n.º 39
0
 def get_domain_data(self, module):
     return frappe.get_attr(
         frappe.get_hooks('domains')[self.name] + '.data')
Ejemplo n.º 40
0
def search_widget(doctype,
                  txt,
                  query=None,
                  searchfield="name",
                  start=0,
                  page_len=50,
                  filters=None):
    if isinstance(filters, basestring):
        import json
        filters = json.loads(filters)

    meta = frappe.get_meta(doctype)

    standard_queries = frappe.get_hooks().standard_queries or []
    if standard_queries:
        standard_queries = dict([v.split(":") for v in standard_queries])

    if query and query.split()[0].lower() != "select":
        # by method
        frappe.response["values"] = frappe.get_attr(query)(doctype, txt,
                                                           searchfield, start,
                                                           page_len, filters)
    elif not query and doctype in standard_queries:
        # from standard queries
        search_widget(doctype, txt, standard_queries[doctype], searchfield,
                      start, page_len, filters)
    else:
        if query:
            # custom query
            frappe.response["values"] = frappe.db.sql(
                scrub_custom_query(query, searchfield, txt))
        else:
            if isinstance(filters, dict):
                filters_items = filters.items()
                filters = []
                for f in filters_items:
                    if isinstance(f[1], (list, tuple)):
                        filters.append([doctype, f[0], f[1][0], f[1][1]])
                    else:
                        filters.append([doctype, f[0], "=", f[1]])

            if filters == None:
                filters = []

            # build from doctype
            if txt:
                filters.append(
                    [doctype, searchfield or "name", "like", txt + "%"])
            if meta.get("fields", {
                    "fieldname": "enabled",
                    "fieldtype": "Check"
            }):
                filters.append([doctype, "enabled", "=", 1])
            if meta.get("fields", {
                    "fieldname": "disabled",
                    "fieldtype": "Check"
            }):
                filters.append([doctype, "disabled", "!=", 1])

            frappe.response["values"] = frappe.widgets.reportview.execute(
                doctype,
                filters=filters,
                fields=get_std_fields_list(meta, searchfield or "name"),
                limit_start=start,
                limit_page_length=page_len,
                as_list=True)
Ejemplo n.º 41
0
def generate_report_result(report, filters=None, user=None):
    status = None
    if not user:
        user = frappe.session.user
    if not filters:
        filters = []

    if filters and isinstance(filters, string_types):
        filters = json.loads(filters)
    columns, result, message, chart, data_to_be_printed = [], [], None, None, None
    if report.report_type == "Query Report":
        if not report.query:
            status = "error"
            frappe.msgprint(_("Must specify a Query to run"),
                            raise_exception=True)

        if not report.query.lower().startswith("select"):
            status = "error"
            frappe.msgprint(_("Query must be a SELECT"), raise_exception=True)

        result = [list(t) for t in frappe.db.sql(report.query, filters)]
        columns = [cstr(c[0]) for c in frappe.db.get_description()]
    else:
        module = report.module or frappe.db.get_value(
            "DocType", report.ref_doctype, "module")
        if report.is_standard == "Yes":
            method_name = get_report_module_dotted_path(
                module, report.name) + ".execute"
            threshold = 10
            res = []

            start_time = datetime.datetime.now()
            # The JOB
            res = frappe.get_attr(method_name)(frappe._dict(filters))

            end_time = datetime.datetime.now()

            if (end_time - start_time
                ).seconds > threshold and not report.prepared_report:
                report.db_set('prepared_report', 1)

            columns, result = res[0], res[1]
            if len(res) > 2:
                message = res[2]
            if len(res) > 3:
                chart = res[3]
            if len(res) > 4:
                data_to_be_printed = res[4]

    if result:
        result = get_filtered_data(report.ref_doctype, columns, result, user)

    if cint(report.add_total_row) and result:
        result = add_total_row(result, columns)

    return {
        "result": result,
        "columns": columns,
        "message": message,
        "chart": chart,
        "data_to_be_printed": data_to_be_printed,
        "status": status
    }
Ejemplo n.º 42
0
def execute_job(site,
                method,
                event,
                job_name,
                kwargs,
                user=None,
                is_async=True,
                retry=0):
    '''Executes job in a worker, performs commit/rollback and logs if there is any error'''
    if is_async:
        frappe.connect(site)
        if os.environ.get('CI'):
            frappe.flags.in_test = True

        if user:
            frappe.set_user(user)

    if isinstance(method, string_types):
        method_name = method
        method = frappe.get_attr(method)
    else:
        method_name = cstr(method.__name__)

    try:
        method(**kwargs)

    except (frappe.db.InternalError, frappe.RetryBackgroundJobError) as e:
        frappe.db.rollback()

        if (retry < 5 and
            (isinstance(e, frappe.RetryBackgroundJobError) or
             (frappe.db.is_deadlocked(e) or frappe.db.is_timedout(e)))):
            # retry the job if
            # 1213 = deadlock
            # 1205 = lock wait timeout
            # or RetryBackgroundJobError is explicitly raised
            frappe.destroy()
            time.sleep(retry + 1)

            return execute_job(site,
                               method,
                               event,
                               job_name,
                               kwargs,
                               is_async=is_async,
                               retry=retry + 1)

        else:
            frappe.log_error(method_name)
            raise

    except:
        frappe.db.rollback()
        frappe.log_error(method_name)
        frappe.db.commit()
        print(frappe.get_traceback())
        raise

    else:
        frappe.db.commit()

    finally:
        if is_async:
            frappe.destroy()
Ejemplo n.º 43
0
def get_bootinfo():
    """build and return boot info"""
    frappe.set_user_lang(frappe.session.user)
    bootinfo = frappe._dict()
    hooks = frappe.get_hooks()
    doclist = []

    # user
    get_user(bootinfo)

    # system info
    bootinfo['sysdefaults'] = frappe.defaults.get_defaults()
    bootinfo['server_date'] = frappe.utils.nowdate()

    if frappe.session['user'] != 'Guest':
        bootinfo['user_info'] = get_fullnames()
        bootinfo['sid'] = frappe.session['sid']

    # home page
    bootinfo.modules = {}
    for app in frappe.get_installed_apps():
        try:
            bootinfo.modules.update(
                frappe.get_attr(app + ".config.desktop.get_data")() or {})
        except ImportError:
            pass
        except AttributeError:
            pass

    bootinfo.module_app = frappe.local.module_app
    bootinfo.hidden_modules = frappe.db.get_global("hidden_modules")
    bootinfo.doctype_icons = dict(
        frappe.db.sql("""select name, icon from
		tabDocType where ifnull(icon,'')!=''"""))
    bootinfo.single_types = frappe.db.sql_list(
        """select name from tabDocType where ifnull(issingle,0)=1""")
    add_home_page(bootinfo, doclist)
    bootinfo.page_info = get_allowed_pages()
    load_translations(bootinfo)
    add_timezone_info(bootinfo)
    load_conf_settings(bootinfo)
    load_print(bootinfo, doclist)
    doclist.extend(get_meta_bundle("Page"))

    # ipinfo
    if frappe.session['data'].get('ipinfo'):
        bootinfo['ipinfo'] = frappe.session['data']['ipinfo']

    # add docs
    bootinfo['docs'] = doclist

    for method in hooks.boot_session or []:
        frappe.get_attr(method)(bootinfo)

    if bootinfo.lang:
        bootinfo.lang = unicode(bootinfo.lang)
    bootinfo['versions'] = {k: v['version'] for k, v in get_versions().items()}

    bootinfo.error_report_email = frappe.get_hooks("error_report_email")
    bootinfo.default_background_image = get_url(
        "/assets/frappe/images/ui/into-the-dawn.jpg")

    return bootinfo
Ejemplo n.º 44
0
def get_stages_hooks(args):
    stages = []
    for method in frappe.get_hooks("setup_wizard_stages"):
        stages += frappe.get_attr(method)(args)
    return stages
def get_website_settings():
    hooks = frappe.get_hooks()
    context = frappe._dict({
        'top_bar_items':
        get_items('top_bar_items'),
        'footer_items':
        get_items('footer_items'),
        "post_login": [
            {
                "label": "My Account",
                "url": "/me"
            },
            #			{"class": "divider"},
            {
                "label": "Logout",
                "url": "/?cmd=web_logout"
            }
        ]
    })

    settings = frappe.get_doc("Website Settings", "Website Settings")
    for k in [
            "banner_html", "brand_html", "copyright", "twitter_share_via",
            "facebook_share", "google_plus_one", "twitter_share",
            "linked_in_share", "disable_signup", "hide_footer_signup",
            "head_html"
    ]:
        if hasattr(settings, k):
            context[k] = settings.get(k)

    if settings.address:
        context["footer_address"] = settings.address

    for k in [
            "facebook_share", "google_plus_one", "twitter_share",
            "linked_in_share", "disable_signup"
    ]:
        context[k] = int(context.get(k) or 0)

    if frappe.request:
        context.url = quote(str(get_request_site_address(full_address=True)),
                            safe="/:")

    context.encoded_title = quote(encode(context.title or ""), str(""))

    for update_website_context in hooks.update_website_context or []:
        frappe.get_attr(update_website_context)(context)

    context.web_include_js = hooks.web_include_js or []

    context.web_include_css = hooks.web_include_css or []

    via_hooks = frappe.get_hooks("website_context")
    for key in via_hooks:
        context[key] = via_hooks[key]
        if key not in ("top_bar_items", "footer_items", "post_login") \
         and isinstance(context[key], (list, tuple)):
            context[key] = context[key][0]

    add_website_theme(context)

    if not context.get("favicon"):
        context["favicon"] = "/assets/frappe/images/favicon.png"

    if settings.favicon and settings.favicon != "attach_files:":
        context["favicon"] = settings.favicon

    return context
Ejemplo n.º 46
0
def install_app(name, verbose=False, set_as_patched=True):
    from frappe.core.doctype.scheduled_job_type.scheduled_job_type import sync_jobs
    from frappe.model.sync import sync_for
    from frappe.modules.utils import sync_customizations
    from frappe.utils.fixtures import sync_fixtures

    frappe.flags.in_install = name
    frappe.flags.ignore_in_install = False

    frappe.clear_cache()
    app_hooks = frappe.get_hooks(app_name=name)
    installed_apps = frappe.get_installed_apps()

    # install pre-requisites
    if app_hooks.required_apps:
        for app in app_hooks.required_apps:
            install_app(app, verbose=verbose)

    frappe.flags.in_install = name
    frappe.clear_cache()

    if name not in frappe.get_all_apps():
        raise Exception("App not in apps.txt")

    if name in installed_apps:
        frappe.msgprint(frappe._("App {0} already installed").format(name))
        return

    print("\nInstalling {0}...".format(name))

    if name != "frappe":
        frappe.only_for("System Manager")

    for before_install in app_hooks.before_install or []:
        out = frappe.get_attr(before_install)()
        if out == False:
            return

    if name != "frappe":
        add_module_defs(name)

    sync_for(name,
             force=True,
             sync_everything=True,
             verbose=verbose,
             reset_permissions=True)

    add_to_installed_apps(name)

    frappe.get_doc('Portal Settings', 'Portal Settings').sync_menu()

    if set_as_patched:
        set_all_patches_as_completed(name)

    for after_install in app_hooks.after_install or []:
        frappe.get_attr(after_install)()

    sync_jobs()
    sync_fixtures(name)
    sync_customizations(name)

    for after_sync in app_hooks.after_sync or []:
        frappe.get_attr(after_sync)()  #

    frappe.flags.in_install = False
Ejemplo n.º 47
0
def get_bootinfo():
    """build and return boot info"""
    frappe.set_user_lang(frappe.session.user)
    bootinfo = frappe._dict()
    hooks = frappe.get_hooks()
    doclist = []

    # user
    get_user(bootinfo)

    # system info
    bootinfo.sitename = frappe.local.site
    bootinfo.sysdefaults = frappe.defaults.get_defaults()
    bootinfo.user_permissions = get_user_permissions()
    bootinfo.server_date = frappe.utils.nowdate()

    if frappe.session['user'] != 'Guest':
        bootinfo.user_info = get_fullnames()
        bootinfo.sid = frappe.session['sid']

    bootinfo.modules = {}
    bootinfo.module_list = []
    load_desktop_icons(bootinfo)
    bootinfo.letter_heads = get_letter_heads()
    bootinfo.active_domains = frappe.get_active_domains()
    bootinfo.all_domains = [d.get("name") for d in frappe.get_all("Domain")]

    bootinfo.module_app = frappe.local.module_app
    bootinfo.single_types = frappe.db.sql_list("""select name from tabDocType
		where issingle=1""")
    add_home_page(bootinfo, doclist)
    bootinfo.page_info = get_allowed_pages()
    load_translations(bootinfo)
    add_timezone_info(bootinfo)
    load_conf_settings(bootinfo)
    load_print(bootinfo, doclist)
    doclist.extend(get_meta_bundle("Page"))
    bootinfo.home_folder = frappe.db.get_value("File", {"is_home_folder": 1})

    # ipinfo
    if frappe.session.data.get('ipinfo'):
        bootinfo.ipinfo = frappe.session['data']['ipinfo']

    # add docs
    bootinfo.docs = doclist

    for method in hooks.boot_session or []:
        frappe.get_attr(method)(bootinfo)

    if bootinfo.lang:
        bootinfo.lang = text_type(bootinfo.lang)
    bootinfo.versions = {k: v['version'] for k, v in get_versions().items()}

    bootinfo.error_report_email = frappe.get_hooks("error_report_email")
    bootinfo.calendars = sorted(frappe.get_hooks("calendars"))
    bootinfo.treeviews = frappe.get_hooks("treeviews") or []
    bootinfo.lang_dict = get_lang_dict()
    bootinfo.feedback_triggers = get_enabled_feedback_trigger()
    bootinfo.gsuite_enabled = get_gsuite_status()
    bootinfo.update(get_email_accounts(user=frappe.session.user))

    return bootinfo
Ejemplo n.º 48
0
def get_version(app="frappe"):
    try:
        return frappe.get_attr(app + ".__version__")
    except AttributeError:
        return '0.0.1'
Ejemplo n.º 49
0
def get_website_user_home_page(user):
	home_page_method = frappe.get_hooks('get_website_user_home_page')
	if home_page_method:
		home_page = frappe.get_attr(home_page_method[-1])(user)
		return '/' + home_page.strip('/')
	else:
Ejemplo n.º 50
0
def get_allowed_functions_for_jenv():
    import os
    import frappe
    import frappe.utils
    import frappe.utils.data
    from frappe.utils.autodoc import automodule, get_version
    from frappe.model.document import get_controller
    from frappe.website.utils import get_shade
    from frappe.modules import scrub
    import mimetypes

    datautils = {}
    for key, obj in frappe.utils.data.__dict__.items():
        if key.startswith("_"):
            # ignore
            continue

        if hasattr(obj, "__call__"):
            # only allow functions
            datautils[key] = obj

    if "_" in getattr(frappe.local, 'form_dict', {}):
        del frappe.local.form_dict["_"]

    out = {
        # make available limited methods of frappe
        "frappe": {
            "_":
            frappe._,
            "get_url":
            frappe.utils.get_url,
            "format_value":
            frappe.format_value,
            "format_date":
            frappe.utils.data.global_date_format,
            "form_dict":
            getattr(frappe.local, 'form_dict', {}),
            "local":
            frappe.local,
            "get_hooks":
            frappe.get_hooks,
            "get_meta":
            frappe.get_meta,
            "get_doc":
            frappe.get_doc,
            "get_list":
            frappe.get_list,
            "get_all":
            frappe.get_all,
            "utils":
            datautils,
            "user":
            getattr(frappe.local, "session", None)
            and frappe.local.session.user or "Guest",
            "get_fullname":
            frappe.utils.get_fullname,
            "get_gravatar":
            frappe.utils.get_gravatar_url,
            "full_name":
            getattr(frappe.local, "session", None)
            and frappe.local.session.data.full_name or "Guest",
            "render_template":
            frappe.render_template
        },
        "autodoc": {
            "get_version": get_version,
            "automodule": automodule,
            "get_controller": get_controller
        },
        "_": frappe._,
        "get_shade": get_shade,
        "scrub": scrub,
        "guess_mimetype": mimetypes.guess_type,
        "dev_server": 1 if os.environ.get('DEV_SERVER', False) else 0
    }

    if not frappe.flags.in_setup_help:
        out['get_visible_columns'] = frappe.get_attr(
            "frappe.www.print.get_visible_columns")
        out['frappe']['date_format'] = frappe.db.get_default(
            "date_format") or "yyyy-mm-dd"
        out['frappe']["db"] = {
            "get_value": frappe.db.get_value,
            "get_default": frappe.db.get_default,
        }

    return out
                job_name,
                kwargs,
                user=None,
                async=True,
                retry=0):
    '''Executes job in a worker, performs commit/rollback and logs if there is any error'''
    from frappe.utils.scheduler import log

    if async:
        frappe.connect(site)
        if user:
            frappe.set_user(user)

    if isinstance(method, basestring):
        method_name = method
        method = frappe.get_attr(method)
    else:
        method_name = cstr(method.__name__)

    try:
        method(**kwargs)

    except (MySQLdb.OperationalError, frappe.RetryBackgroundJobError), e:
        frappe.db.rollback()

        if (retry < 5 and (isinstance(e, frappe.RetryBackgroundJobError)
                           or e.args[0] in (1213, 1205))):
            # retry the job if
            # 1213 = deadlock
            # 1205 = lock wait timeout
            # or RetryBackgroundJobError is explicitly raised
Ejemplo n.º 52
0
def install_country_fixtures(company):
	company_doc = frappe.get_doc("Company", company)
	path = frappe.get_app_path('erpnext', 'regional', frappe.scrub(company_doc.country))
	if os.path.exists(path.encode("utf-8")):
		frappe.get_attr("erpnext.regional.{0}.setup.setup"
			.format(frappe.scrub(company_doc.country)))(company_doc, False)
Ejemplo n.º 53
0
def load_startup_js(bootinfo):
    bootinfo.startup_js = ""
    for method in frappe.get_hooks().startup_js or []:
        bootinfo.startup_js += frappe.get_attr(method)()
Ejemplo n.º 54
0
    add_home_page(bootinfo, doclist)
    add_allowed_pages(bootinfo)
    load_translations(bootinfo)
    load_conf_settings(bootinfo)
    load_startup_js(bootinfo)

    # ipinfo
    if frappe.session['data'].get('ipinfo'):
        bootinfo['ipinfo'] = frappe.session['data']['ipinfo']

    # add docs
    bootinfo['docs'] = doclist

    for method in hooks.boot_session or []:
        frappe.get_attr(method)(bootinfo)

    from frappe.model.utils import compress
    bootinfo['docs'] = compress(bootinfo['docs'])

    if bootinfo.lang:
        bootinfo.lang = unicode(bootinfo.lang)

    bootinfo.metadata_version = frappe.cache().get_value("metadata_version")
    if not bootinfo.metadata_version:
        bootinfo.metadata_version = frappe.reset_metadata_version()

    return bootinfo


def load_conf_settings(bootinfo):
Ejemplo n.º 55
0
 def install_country_fixtures(self):
     path = frappe.get_app_path('erpnext', 'regional',
                                frappe.scrub(self.country))
     if os.path.exists(path.encode("utf-8")):
         frappe.get_attr("erpnext.regional.{0}.setup.setup".format(
             self.country.lower()))(self)
Ejemplo n.º 56
0
def load_print_css(bootinfo, print_settings):
	bootinfo.print_css = frappe.get_attr("frappe.templates.pages.print.get_print_style")(print_settings.print_style or "Modern", for_legacy=True)
Ejemplo n.º 57
0
def main(app=None,
         module=None,
         doctype=None,
         verbose=False,
         tests=(),
         force=False,
         profile=False,
         junit_xml_output=None,
         ui_tests=False,
         doctype_list_path=None,
         skip_test_records=False,
         failfast=False):
    global unittest_runner

    if doctype_list_path:
        app, doctype_list_path = doctype_list_path.split(os.path.sep, 1)
        with open(frappe.get_app_path(app, doctype_list_path), 'r') as f:
            doctype = f.read().strip().splitlines()

    xmloutput_fh = None
    if junit_xml_output:
        xmloutput_fh = open(junit_xml_output, 'wb')
        unittest_runner = xmlrunner_wrapper(xmloutput_fh)
    else:
        unittest_runner = unittest.TextTestRunner

    try:
        frappe.flags.print_messages = verbose
        frappe.flags.in_test = True

        if not frappe.db:
            frappe.connect()

        # if not frappe.conf.get("db_name").startswith("test_"):
        # 	raise Exception, 'db_name must start with "test_"'

        # workaround! since there is no separate test db
        frappe.clear_cache()
        frappe.utils.scheduler.disable_scheduler()
        set_test_email_config()

        if not frappe.flags.skip_before_tests:
            if verbose:
                print('Running "before_tests" hooks')
            for fn in frappe.get_hooks("before_tests", app_name=app):
                frappe.get_attr(fn)()

        if doctype:
            ret = run_tests_for_doctype(doctype,
                                        verbose,
                                        tests,
                                        force,
                                        profile,
                                        junit_xml_output=junit_xml_output)
        elif module:
            ret = run_tests_for_module(module,
                                       verbose,
                                       tests,
                                       profile,
                                       junit_xml_output=junit_xml_output)
        else:
            ret = run_all_tests(app,
                                verbose,
                                profile,
                                ui_tests,
                                failfast=failfast,
                                junit_xml_output=junit_xml_output)

        frappe.db.commit()

        # workaround! since there is no separate test db
        frappe.clear_cache()
        return ret

    finally:
        if xmloutput_fh:
            xmloutput_fh.flush()
            xmloutput_fh.close()
Ejemplo n.º 58
0
def handle_setup_exception(args):
	frappe.db.rollback()
	if args:
		traceback = frappe.get_traceback()
		for hook in frappe.get_hooks("setup_wizard_exception"):
			frappe.get_attr(hook)(traceback, args)
Ejemplo n.º 59
0
def setup_account(args=None):
    try:
        if frappe.db.sql("select name from tabCompany"):
            frappe.throw(_("Setup Already Complete!!"))

        if not args:
            args = frappe.local.form_dict
        if isinstance(args, basestring):
            args = json.loads(args)

        args = frappe._dict(args)

        if args.language and args.language != "english":
            set_default_language(args.language)

        install_fixtures.install(args.get("country"))

        update_user_name(args)
        frappe.local.message_log = []

        create_fiscal_year_and_company(args)
        frappe.local.message_log = []

        create_users(args)
        frappe.local.message_log = []

        set_defaults(args)
        frappe.local.message_log = []

        create_territories()
        frappe.local.message_log = []

        create_price_lists(args)
        frappe.local.message_log = []

        create_feed_and_todo()
        frappe.local.message_log = []

        create_email_digest()
        frappe.local.message_log = []

        create_letter_head(args)
        frappe.local.message_log = []

        create_taxes(args)
        frappe.local.message_log = []

        create_items(args)
        frappe.local.message_log = []

        create_customers(args)
        frappe.local.message_log = []

        create_suppliers(args)
        frappe.local.message_log = []

        frappe.db.set_default('desktop:home_page', 'desktop')

        website_maker(args.company_name.strip(), args.company_tagline,
                      args.name)
        create_logo(args)

        frappe.db.commit()

        login_as_first_user(args)

        frappe.db.commit()

        frappe.clear_cache()

        if args.get("add_sample_data"):
            try:
                make_sample_data()
            except FiscalYearError:
                pass
    except:
        if args:
            traceback = frappe.get_traceback()
            for hook in frappe.get_hooks("setup_wizard_exception"):
                frappe.get_attr(hook)(traceback, args)

        raise

    else:
        for hook in frappe.get_hooks("setup_wizard_success"):
            frappe.get_attr(hook)(args)
Ejemplo n.º 60
0
def run(report_name, filters=()):
    report = get_report_doc(report_name)

    if filters and isinstance(filters, basestring):
        filters = json.loads(filters)

    if not frappe.has_permission(report.ref_doctype, "report"):
        frappe.msgprint(
            _("Must have report permission to access this report."),
            raise_exception=True)

    columns, results = [], []
    if report.report_type == "Query Report":
        if not report.query:
            frappe.msgprint(_("Must specify a Query to run"),
                            raise_exception=True)

        if not report.query.lower().startswith("select"):
            frappe.msgprint(_("Query must be a SELECT"), raise_exception=True)

        result = [list(t) for t in frappe.db.sql(report.query, filters)]
        columns = [c[0] for c in frappe.db.get_description()]
    else:
        module = report.module or frappe.db.get_value(
            "DocType", report.ref_doctype, "module")
        if report.is_standard == "Yes":
            method_name = get_report_module_dotted_path(
                module, report.name) + ".execute"
            columns, result = frappe.get_attr(method_name)(
                frappe._dict(filters))

    if report.apply_user_permissions and result:
        result = get_filtered_data(report.ref_doctype, columns, result)

    if cint(report.add_total_row) and result:
        result = add_total_row(result, columns)

    #hotfix for data in report
    #find column first, if columns starts with:
    # Status, Source
    #then, the value need to be translated
    column_index = []
    i = 0
    for column_n in columns:
        if isinstance(column_n, dict) == False and isinstance(column_n,
                                                              tuple) == False:
            if column_n.find('Status') == 0 or column_n.find(
                    _("Status")) == 0 or column_n.find("status") == 0:
                column_index.append(i)
            if column_n.find('Source') == 0 or column_n.find(
                    _("Source")) == 0 or column_n.find("source") == 0:
                column_index.append(i)
            if column_n.find('Gender') == 0 or column_n.find(
                    _("Gender")) == 0 or column_n.find("gender") == 0:
                column_index.append(i)

        i = i + 1

    #translate value
    for index in column_index:
        for result_row in result:
            if isinstance(result_row, tuple) == False and isinstance(
                    column_n, tuple) == False:
                result_row[index] = _(result_row[index])

    return {"result": result, "columns": columns}