Exemple #1
0
def backup_to_dropbox():
	from dropbox import client, session
	from conf import dropbox_access_key, dropbox_secret_key
	from webnotes.utils.backups import new_backup
	if not webnotes.conn:
		webnotes.connect()


	sess = session.DropboxSession(dropbox_access_key, dropbox_secret_key, "app_folder")

	sess.set_token(webnotes.conn.get_value("Backup Manager", None, "dropbox_access_key"),
		webnotes.conn.get_value("Backup Manager", None, "dropbox_access_secret"))
	
	dropbox_client = client.DropboxClient(sess)

	# upload database
	backup = new_backup()
	filename = backup.backup_path_db
	upload_file_to_dropbox(filename, "database", dropbox_client)

	# upload files
	response = dropbox_client.metadata("files")

	
	# add missing files
	for filename in os.listdir(os.path.join("public", "files")):
		found = False
		for file_metadata in response["contents"]:
 			if filename==os.path.basename(file_metadata["path"]):
				if os.stat(os.path.join("public", "files", filename)).st_size==file_metadata["bytes"]:
					found=True

		if not found:
			upload_file_to_dropbox(os.path.join("public", "files", filename), "files", dropbox_client)
Exemple #2
0
def run():
    webnotes.connect()

    # Confirmation from user
    confirm = ''
    while not confirm:
        confirm = raw_input(
            "Are you sure you want to delete the data from the system (N/Y)?")
    if confirm.lower() != 'y':
        raise Exception

    cleanup_type = ''
    while cleanup_type not in ['1', '2']:
        cleanup_type = raw_input("""\nWhat type of cleanup you want ot perform?
	1. Only Transactions
	2. Both Masters and Transactions

	Please enter your choice (1/2):
		""")

    # delete
    delete_transactions()

    if cleanup_type == '1':
        reset_transaction_series()
    else:
        delete_masters()
        reset_all_series()
        delete_main_masters()
        reset_global_defaults()

    print "System cleaned up succesfully"
    webnotes.conn.close()
Exemple #3
0
def patch(patch_module, site=None, force=False):
	import webnotes.modules.patch_handler
	webnotes.connect(site=site)
	webnotes.local.patch_log_list = []
	webnotes.modules.patch_handler.run_single(patch_module, force=force)
	print "\n".join(webnotes.local.patch_log_list)
	webnotes.destroy()
Exemple #4
0
def clear_web(site=None):
	import webnotes.webutils
	webnotes.connect(site=site)
	from website.doctype.website_sitemap_config.website_sitemap_config import build_website_sitemap_config
	build_website_sitemap_config()
	webnotes.webutils.clear_cache()
	webnotes.destroy()
Exemple #5
0
def reset_perms(site=None):
	webnotes.connect(site=site)
	for d in webnotes.conn.sql_list("""select name from `tabDocType`
		where ifnull(istable, 0)=0 and ifnull(custom, 0)=0"""):
			webnotes.clear_cache(doctype=d)
			webnotes.reset_perms(d)
	webnotes.destroy()
Exemple #6
0
def set_admin_password(admin_password, site=None):
	import webnotes
	webnotes.connect(site=site)
	webnotes.conn.sql("""update __Auth set `password`=password(%s)
		where user='******'""", (admin_password,))
	webnotes.conn.commit()
	webnotes.destroy()
Exemple #7
0
def latest(site=None, verbose=True):
	import webnotes.modules.patch_handler
	import webnotes.model.sync
	import webnotes.plugins
	from website.doctype.website_sitemap_config.website_sitemap_config import build_website_sitemap_config
	
	webnotes.connect(site=site)
	
	try:
		# run patches
		webnotes.local.patch_log_list = []
		webnotes.modules.patch_handler.run_all()
		if verbose:
			print "\n".join(webnotes.local.patch_log_list)
	
		# sync
		webnotes.model.sync.sync_all()
		
		# remove __init__.py from plugins
		webnotes.plugins.remove_init_files()
		
		# build website config if any changes in templates etc.
		build_website_sitemap_config()
		
	except webnotes.modules.patch_handler.PatchError, e:
		print "\n".join(webnotes.local.patch_log_list)
		raise
Exemple #8
0
def make():
	webnotes.connect()
	webnotes.mute_emails = True
	install()
	complete_setup()
	make_items()
	make_customers_suppliers_contacts()
Exemple #9
0
def request(args):
	import webnotes.handler
	webnotes.connect()
	webnotes.form_dict = webnotes._dict([a.split("=") for a in args.split("&")])
	webnotes.handler.execute_cmd(webnotes.form_dict.cmd)
	print webnotes.response
	webnotes.destroy()
Exemple #10
0
def reset_perms(site=None):
    webnotes.connect(site=site)
    for d in webnotes.conn.sql_list("""select name from `tabDocType`
		where ifnull(istable, 0)=0 and ifnull(custom, 0)=0"""):
        webnotes.clear_cache(doctype=d)
        webnotes.reset_perms(d)
    webnotes.destroy()
Exemple #11
0
def main():
	import argparse
	
	parser = argparse.ArgumentParser(description='Run tests.')
	parser.add_argument('-d', '--doctype', nargs=1, metavar = "DOCTYPE",
		help="test for doctype")
	parser.add_argument('-v', '--verbose', default=False, action="store_true")
	parser.add_argument('-e', '--export', nargs=2, metavar="DOCTYPE DOCNAME")
	parser.add_argument('-a', '--all', default=False, action="store_true")
	parser.add_argument('-m', '--module', default=1, metavar="MODULE")

	args = parser.parse_args()
	if not webnotes.conn:
		webnotes.connect()

	webnotes.flags.print_messages = args.verbose
	webnotes.flags.in_test = True
	
	if args.doctype:
		run_unittest(args.doctype[0], verbose=args.verbose)
	elif args.all:
		run_all_tests(args.verbose)
	elif args.export:
		export_doc(args.export[0], args.export[1])
	elif args.module:
		import importlib
		
		test_suite = unittest.TestSuite()
		module = importlib.import_module(args.module)
		if hasattr(module, "test_dependencies"):
			for doctype in module.test_dependencies:
				make_test_records(doctype, verbose=args.verbose)
		
		test_suite.addTest(unittest.TestLoader().loadTestsFromModule(sys.modules[args.module]))
		unittest.TextTestRunner(verbosity=1+(args.verbose and 1 or 0)).run(test_suite)
Exemple #12
0
def run():
	webnotes.connect()
	
	# Confirmation from user
	confirm = ''
	while not confirm:
		confirm = raw_input("Are you sure you want to delete the data from the system (N/Y)?")
	if confirm.lower() != 'y':
		raise Exception

	cleanup_type = ''
	while cleanup_type not in ['1', '2']:
		cleanup_type = raw_input("""\nWhat type of cleanup you want ot perform?
	1. Only Transactions
	2. Both Masters and Transactions

	Please enter your choice (1/2):
		""")
		
	# delete
	delete_transactions()
	
	if cleanup_type == '1':
		reset_transaction_series()
	else:
		delete_masters()
		reset_all_series()
		delete_main_masters()
		reset_global_defaults()

	print "System cleaned up succesfully"
	webnotes.conn.close()
def make():
    import os
    import webnotes
    import website.utils
    import startup.event_handlers

    if not webnotes.conn:
        webnotes.connect()

    home_page = website.utils.get_home_page()

    fname = 'js/wn-web.js'
    if os.path.basename(os.path.abspath('.')) != 'public':
        fname = os.path.join('public', fname)

    if hasattr(startup.event_handlers, 'get_web_script'):
        with open(fname, 'w') as f:
            script = 'window.home_page = "%s";\n' % home_page
            script += startup.event_handlers.get_web_script()
            f.write(script)

    fname = 'css/wn-web.css'
    if os.path.basename(os.path.abspath('.')) != 'public':
        fname = os.path.join('public', fname)

    # style - wn.css
    if hasattr(startup.event_handlers, 'get_web_style'):
        with open(fname, 'w') as f:
            f.write(startup.event_handlers.get_web_style())
Exemple #14
0
def main():
	import argparse
	
	parser = argparse.ArgumentParser(description='Run tests.')
	parser.add_argument('-d', '--doctype', nargs=1, metavar = "DOCTYPE",
		help="test for doctype")
	parser.add_argument('-v', '--verbose', default=False, action="store_true")
	parser.add_argument('-e', '--export', nargs=2, metavar="DOCTYPE DOCNAME")
	parser.add_argument('-a', '--all', default=False, action="store_true")
	parser.add_argument('-m', '--module', default=1, metavar="MODULE")

	args = parser.parse_args()
	if not webnotes.conn:
		webnotes.connect()

	webnotes.flags.print_messages = args.verbose
	webnotes.flags.in_test = True
	
	if args.doctype:
		run_unittest(args.doctype[0], verbose=args.verbose)
	elif args.all:
		run_all_tests(args.verbose)
	elif args.export:
		export_doc(args.export[0], args.export[1])
	elif args.module:
		import importlib
		
		test_suite = unittest.TestSuite()
		module = importlib.import_module(args.module)
		if hasattr(module, "test_dependencies"):
			for doctype in module.test_dependencies:
				make_test_records(doctype, verbose=args.verbose)
		
		test_suite.addTest(unittest.TestLoader().loadTestsFromModule(sys.modules[args.module]))
		unittest.TextTestRunner(verbosity=1+(args.verbose and 1 or 0)).run(test_suite)
Exemple #15
0
def latest(site=None, verbose=True):
    import webnotes.modules.patch_handler
    import webnotes.model.sync
    import webnotes.plugins
    from website.doctype.website_sitemap_config.website_sitemap_config import build_website_sitemap_config

    webnotes.connect(site=site)

    try:
        # run patches
        webnotes.local.patch_log_list = []
        webnotes.modules.patch_handler.run_all()
        if verbose:
            print "\n".join(webnotes.local.patch_log_list)

        # sync
        webnotes.model.sync.sync_all()

        # remove __init__.py from plugins
        webnotes.plugins.remove_init_files()

        # build website config if any changes in templates etc.
        build_website_sitemap_config()

    except webnotes.modules.patch_handler.PatchError, e:
        print "\n".join(webnotes.local.patch_log_list)
        raise
Exemple #16
0
def clear_web(site=None):
    import webnotes.webutils
    webnotes.connect(site=site)
    from website.doctype.website_sitemap_config.website_sitemap_config import build_website_sitemap_config
    build_website_sitemap_config()
    webnotes.webutils.clear_cache()
    webnotes.destroy()
def make():
	import os
	import webnotes
	import website.utils
	import startup.event_handlers

	if not webnotes.conn:
		webnotes.connect()
	
	home_page = website.utils.get_home_page()

	fname = 'js/wn-web.js'
	if os.path.basename(os.path.abspath('.'))!='public':
		fname = os.path.join('public', fname)
			
	if hasattr(startup.event_handlers, 'get_web_script'):
		with open(fname, 'w') as f:
			script = 'window.home_page = "%s";\n' % home_page
			script += startup.event_handlers.get_web_script()
			f.write(script)

	fname = 'css/wn-web.css'
	if os.path.basename(os.path.abspath('.'))!='public':
		fname = os.path.join('public', fname)

	# style - wn.css
	if hasattr(startup.event_handlers, 'get_web_style'):
		with open(fname, 'w') as f:
			f.write(startup.event_handlers.get_web_style())
Exemple #18
0
def backup_to_dropbox():
	from dropbox import client, session
	from conf import dropbox_access_key, dropbox_secret_key
	from webnotes.utils.backups import new_backup
	if not webnotes.conn:
		webnotes.connect()

	sess = session.DropboxSession(dropbox_access_key, dropbox_secret_key, "app_folder")

	sess.set_token(webnotes.conn.get_value("Backup Manager", None, "dropbox_access_key"),
		webnotes.conn.get_value("Backup Manager", None, "dropbox_access_secret"))
	
	dropbox_client = client.DropboxClient(sess)

	# upload database
	backup = new_backup()
	filename = os.path.join(get_base_path(), "public", "backups", 
		os.path.basename(backup.backup_path_db))
	upload_file_to_dropbox(filename, "database", dropbox_client)

	response = dropbox_client.metadata("/files")
	# upload files to files folder
	path = os.path.join(get_base_path(), "public", "files")
	for filename in os.listdir(path):
		found = False
		filepath = os.path.join(path, filename)
		for file_metadata in response["contents"]:
 			if os.path.basename(filepath) == os.path.basename(file_metadata["path"]) and os.stat(filepath).st_size == int(file_metadata["bytes"]):
				found = True
				break
		if not found:
			upload_file_to_dropbox(filepath, "files", dropbox_client)
Exemple #19
0
def patch(patch_module, site=None, force=False):
    import webnotes.modules.patch_handler
    webnotes.connect(site=site)
    webnotes.local.patch_log_list = []
    webnotes.modules.patch_handler.run_single(patch_module, force=force)
    print "\n".join(webnotes.local.patch_log_list)
    webnotes.destroy()
Exemple #20
0
def get_html(page_name):
	"""get page html"""
	page_name = scrub_page_name(page_name)
	
	html = ''
	
	# load from cache, if auto cache clear is falsy
	if not (hasattr(conf, 'auto_cache_clear') and conf.auto_cache_clear or 0):
		if not page_name in no_cache:
			html = webnotes.cache().get_value("page:" + page_name)
			from_cache = True

	if not html:
		webnotes.connect()
		html = load_into_cache(page_name)
		from_cache = False
	
	if not html:
		html = get_html("404")

	if page_name=="error":
		html = html.replace("%(error)s", webnotes.getTraceback())
	else:
		comments = "\n\npage:"+page_name+\
			"\nload status: " + (from_cache and "cache" or "fresh")
		html += """\n<!-- %s -->""" % webnotes.utils.cstr(comments)

	return html
Exemple #21
0
def set_admin_password(admin_password, site=None):
	import webnotes
	webnotes.connect(site=site)
	webnotes.conn.sql("""update __Auth set `password`=password(%s)
		where user='******'""", (admin_password,))
	webnotes.conn.commit()
	webnotes.destroy()
Exemple #22
0
def build_page(page_name):
	if not webnotes.conn:
		webnotes.connect()

	sitemap = get_website_sitemap()
	page_options = sitemap.get(page_name)
	
	if not page_options:
		if page_name=="index":
			# page not found, try home page
			home_page = get_home_page()
			page_options = sitemap.get(home_page)
			if not page_options:
				raise PageNotFoundError
			page_options["page_name"] = home_page
		else:
			raise PageNotFoundError
	else:
		page_options["page_name"] = page_name
	
	basepath = webnotes.utils.get_base_path()
	module = None
	no_cache = False
	
	if page_options.get("controller"):
		module = webnotes.get_module(page_options["controller"])
		no_cache = getattr(module, "no_cache", False)

	# if generator, then load bean, pass arguments
	if page_options.get("is_generator"):
		if not module:
			raise Exception("Generator controller not defined")
		
		name = webnotes.conn.get_value(module.doctype, {
			page_options.get("page_name_field", "page_name"): page_options["page_name"]})
		obj = webnotes.get_obj(module.doctype, name, with_children=True)

		if hasattr(obj, 'get_context'):
			obj.get_context()

		context = webnotes._dict(obj.doc.fields)
		context["obj"] = obj
	else:
		# page
		context = webnotes._dict({ 'name': page_name })
		if module and hasattr(module, "get_context"):
			context.update(module.get_context())
	
	context.update(get_website_settings())

	jenv = webnotes.get_jenv()
	context["base_template"] = jenv.get_template(webnotes.get_config().get("base_template"))
	
	template_name = page_options['template']	
	html = jenv.get_template(template_name).render(context)
	
	if not no_cache:
		webnotes.cache().set_value("page:" + page_name, html)
	return html
Exemple #23
0
def backup_to_gdrive():
    from webnotes.utils.backups import new_backup
    if not webnotes.conn:
        webnotes.connect()
    get_gdrive_flow()
    credentials_json = webnotes.conn.get_value("Backup Manager", None,
                                               "gdrive_credentials")
    credentials = oauth2client.client.Credentials.new_from_json(
        credentials_json)
    http = httplib2.Http()
    http = credentials.authorize(http)
    drive_service = build('drive', 'v2', http=http)

    # upload database
    backup = new_backup()
    path = os.path.join(get_base_path(), "public", "backups")
    filename = os.path.join(path, os.path.basename(backup.backup_path_db))

    # upload files to database folder
    upload_files(
        filename, 'application/x-gzip', drive_service,
        webnotes.conn.get_value("Backup Manager", None, "database_folder_id"))

    # upload files to files folder
    did_not_upload = []
    error_log = []

    files_folder_id = webnotes.conn.get_value("Backup Manager", None,
                                              "files_folder_id")

    webnotes.conn.close()
    path = os.path.join(get_base_path(), "public", "files")
    for filename in os.listdir(path):
        filename = cstr(filename)
        found = False
        filepath = os.path.join(path, filename)
        ext = filename.split('.')[-1]
        size = os.path.getsize(filepath)
        if ext == 'gz' or ext == 'gzip':
            mimetype = 'application/x-gzip'
        else:
            mimetype = mimetypes.types_map.get(
                "." + ext) or "application/octet-stream"

        #Compare Local File with Server File
        children = drive_service.children().list(
            folderId=files_folder_id).execute()
        for child in children.get('items', []):
            file = drive_service.files().get(fileId=child['id']).execute()
            if filename == file['title'] and size == int(file['fileSize']):
                found = True
                break
        if not found:
            try:
                upload_files(filepath, mimetype, drive_service,
                             files_folder_id)
            except Exception, e:
                did_not_upload.append(filename)
                error_log.append(cstr(e))
Exemple #24
0
def domain(host_url=None, site=None):
	webnotes.connect(site=site)
	if host_url:
		webnotes.conn.set_value("Website Settings", None, "subdomain", host_url)
		webnotes.conn.commit()
	else:
		print webnotes.conn.get_value("Website Settings", None, "subdomain")
	webnotes.destroy()
Exemple #25
0
def run_scheduler():
	from webnotes.utils.file_lock import create_lock, delete_lock
	import webnotes.utils.scheduler
	if create_lock('scheduler'):
		webnotes.connect()
		print webnotes.utils.scheduler.execute()
		delete_lock('scheduler')
	webnotes.destroy()
Exemple #26
0
def domain(host_url=None, site=None):
	webnotes.connect(site=site)
	if host_url:
		webnotes.conn.set_value("Website Settings", None, "subdomain", host_url)
		webnotes.conn.commit()
	else:
		print webnotes.conn.get_value("Website Settings", None, "subdomain")
	webnotes.destroy()
Exemple #27
0
def make():
	webnotes.connect()
	webnotes.print_messages = True
	webnotes.mute_emails = True
	install()
	complete_setup()
	make_items()
	make_customers_suppliers_contacts()
Exemple #28
0
def create_erpnext_folder(service):
	if not webnotes.conn:
		webnotes.connect()
	erpnext = {
		'title': 'erpnext',
		'mimeType': 'application/vnd.google-apps.folder'
	}
	erpnext = service.files().insert(body=erpnext).execute()
	return erpnext['id']
Exemple #29
0
def make(reset=False):
	webnotes.connect()
	#webnotes.print_messages = True
	webnotes.mute_emails = True
	webnotes.rollback_on_exception = True
	
	if reset:
		setup()
	simulate()
Exemple #30
0
def setup():
	install()
	webnotes.connect(db_name=webnotes.conf.demo_db_name)
	complete_setup()
	make_customers_suppliers_contacts()
	make_items()
	make_price_lists()
	make_users_and_employees()
	make_bank_account()
Exemple #31
0
def setup():
    install()
    webnotes.connect(db_name=webnotes.conf.demo_db_name)
    complete_setup()
    make_customers_suppliers_contacts()
    make_items()
    make_price_lists()
    make_users_and_employees()
    make_bank_account()
Exemple #32
0
def backup(site=None, with_files=False, verbose=True, backup_path_db=None, backup_path_files=None):
	from webnotes.utils.backups import scheduled_backup
	webnotes.connect(site=site)
	print backup_path_db
	odb = scheduled_backup(ignore_files=not with_files, backup_path_db=backup_path_db, backup_path_files=backup_path_files)
	if verbose:
		from webnotes.utils import now
		print "backup taken -", odb.backup_path_db, "- on", now()
	return odb
Exemple #33
0
def run_scheduler(site=None):
	from webnotes.utils.file_lock import create_lock, delete_lock
	import webnotes.utils.scheduler
	webnotes.init(site=site)
	if create_lock('scheduler'):
		webnotes.connect(site=site)
		print webnotes.utils.scheduler.execute()
		delete_lock('scheduler')
	webnotes.destroy()
Exemple #34
0
def make_demo_app():
    webnotes.mute_emails = 1
    webnotes.connect()
    utilities.demo.make_demo.make(reset=True, simulate=False)
    # setup demo user etc so that the site it up faster, while the data loads
    make_demo_user()
    make_demo_login_page()
    make_demo_on_login_script()
    utilities.demo.make_demo.make(reset=False, simulate=True)
Exemple #35
0
def run_scheduler(site=None):
    from webnotes.utils.file_lock import create_lock, delete_lock
    import webnotes.utils.scheduler
    webnotes.init(site=site)
    if create_lock('scheduler'):
        webnotes.connect(site=site)
        print webnotes.utils.scheduler.execute()
        delete_lock('scheduler')
    webnotes.destroy()
def make_demo_app():
    webnotes.mute_emails = 1
    webnotes.connect()
    utilities.demo.make_demo.make(reset=True, simulate=False)
    # setup demo user etc so that the site it up faster, while the data loads
    make_demo_user()
    make_demo_login_page()
    make_demo_on_login_script()
    utilities.demo.make_demo.make(reset=False, simulate=True)
Exemple #37
0
def make(reset=False):
	webnotes.connect()
	webnotes.print_messages = True
	webnotes.mute_emails = True
	webnotes.rollback_on_exception = True
	
	if reset:
		setup()
	simulate()
Exemple #38
0
def create_owrang_folder(service):
	if not webnotes.conn:
		webnotes.connect()
	owrang = {
		'title': 'owrang',
		'mimeType': 'application/vnd.google-apps.folder'
	}
	owrang = service.files().insert(body=owrang).execute()
	return owrang['id']
def get_home_page():
    if not webnotes.conn:
        webnotes.connect()
    doc_name = webnotes.conn.get_value('Website Settings', None, 'home_page')
    if doc_name:
        page_name = webnotes.conn.get_value('Web Page', doc_name, 'page_name')
    else:
        page_name = 'login'

    return page_name
Exemple #40
0
def get_home_page():
	if not webnotes.conn:
		webnotes.connect()
	doc_name = webnotes.conn.get_value('Website Settings', None, 'home_page')
	if doc_name:
		page_name = webnotes.conn.get_value('Web Page', doc_name, 'page_name')
	else:
		page_name = 'login'

	return page_name
def build_page(page_name):
    if not webnotes.conn:
        webnotes.connect()

    sitemap_options = webnotes.doc("Website Sitemap", page_name).fields

    page_options = webnotes.doc(
        "Website Sitemap Config",
        sitemap_options.get("website_sitemap_config")).fields.update({
            "page_name":
            sitemap_options.page_name,
            "docname":
            sitemap_options.docname
        })

    if not page_options:
        raise PageNotFoundError
    else:
        page_options["page_name"] = page_name

    basepath = webnotes.utils.get_base_path()
    no_cache = page_options.get("no_cache")

    # if generator, then load bean, pass arguments
    if page_options.get("page_or_generator") == "Generator":
        doctype = page_options.get("ref_doctype")
        obj = webnotes.get_obj(doctype,
                               page_options["docname"],
                               with_children=True)

        if hasattr(obj, 'get_context'):
            obj.get_context()

        context = webnotes._dict(obj.doc.fields)
        context["obj"] = obj
    else:
        # page
        context = webnotes._dict({'name': page_name})
        if page_options.get("controller"):
            module = webnotes.get_module(page_options.get("controller"))
            if module and hasattr(module, "get_context"):
                context.update(module.get_context())

    context.update(get_website_settings())

    jenv = webnotes.get_jenv()
    context["base_template"] = jenv.get_template(
        webnotes.get_config().get("base_template"))

    template_name = page_options['template_path']
    html = jenv.get_template(template_name).render(context)

    if not no_cache:
        webnotes.cache().set_value("page:" + page_name, html)
    return html
Exemple #42
0
def make():
    import os
    import webnotes
    # TODO: why is jinja2 imported?
    from jinja2 import Template
    import webnotes.cms

    if not webnotes.conn:
        webnotes.connect()

    make_web_core()
Exemple #43
0
def backup(site=None, with_files=False, verbose=True, backup_path_db=None, backup_path_files=None):
	from webnotes.utils.backups import scheduled_backup
	webnotes.connect(site=site)
	odb = scheduled_backup(ignore_files=not with_files, backup_path_db=backup_path_db, backup_path_files=backup_path_files)
	if verbose:
		from webnotes.utils import now
		print "database backup taken -", odb.backup_path_db, "- on", now()
		if with_files:
			print "files backup taken -", odb.backup_path_files, "- on", now()
	webnotes.destroy()
	return odb
Exemple #44
0
def backup(site=None, with_files=False, verbose=True, backup_path_db=None, backup_path_files=None, delete_older_than=6):
	from webnotes.utils.backups import scheduled_backup
	webnotes.connect(site=site)
	odb = scheduled_backup(delete_older_than=delete_older_than, ignore_files=not with_files, backup_path_db=backup_path_db, backup_path_files=backup_path_files)
	if verbose:
		from webnotes.utils import now
		print "database backup taken -", odb.backup_path_db, "- on", now()
		if with_files:
			print "files backup taken -", odb.backup_path_files, "- on", now()
	webnotes.destroy()
	return odb
Exemple #45
0
def make():
	import os
	import webnotes
	# TODO: why is jinja2 imported?
	from jinja2 import Template
	import webnotes.cms
	
	if not webnotes.conn:
		webnotes.connect()
	
	make_web_core()
Exemple #46
0
def execute(site=None):
    """
	execute jobs
	this method triggers the other scheduler events
	Database connection: Ideally it should be connected from outside, if there is
	no connection, it will connect from defs.py
	"""
    from datetime import datetime
    import webnotes.utils

    format = '%Y-%m-%d %H:%M:%S'

    if not webnotes.conn:
        webnotes.connect(site=site)

    out = []

    nowtime = webnotes.utils.now_datetime()
    last = webnotes.conn.get_global('scheduler_last_event')

    # set scheduler last event
    webnotes.conn.begin()
    webnotes.conn.set_global('scheduler_last_event', nowtime.strftime(format))
    webnotes.conn.commit()

    if last:
        last = datetime.strptime(last, format)

        if nowtime.day != last.day:
            # if first task of the day execute daily tasks
            out.append(
                nowtime.strftime("%Y-%m-%d %H:%M:%S") + ' - daily:' +
                trigger('execute_daily'))

            if nowtime.month != last.month:
                out.append(
                    nowtime.strftime("%Y-%m-%d %H:%M:%S") + ' - monthly:' +
                    trigger('execute_monthly'))

            if nowtime.weekday() == 0:
                out.append(
                    nowtime.strftime("%Y-%m-%d %H:%M:%S") + ' - weekly:' +
                    trigger('execute_weekly'))

        if nowtime.hour != last.hour:
            out.append(
                nowtime.strftime("%Y-%m-%d %H:%M:%S") + ' - hourly:' +
                trigger('execute_hourly'))

    out.append(
        nowtime.strftime("%Y-%m-%d %H:%M:%S") + ' - all:' +
        trigger('execute_all'))

    return '\n'.join(out)
Exemple #47
0
def backup_to_dropbox():
    from dropbox import client, session
    from conf import dropbox_access_key, dropbox_secret_key
    from webnotes.utils.backups import new_backup
    from webnotes.utils import get_files_path, get_backups_path
    if not webnotes.conn:
        webnotes.connect()

    sess = session.DropboxSession(dropbox_access_key, dropbox_secret_key,
                                  "app_folder")

    sess.set_token(
        webnotes.conn.get_value("Backup Manager", None, "dropbox_access_key"),
        webnotes.conn.get_value("Backup Manager", None,
                                "dropbox_access_secret"))

    dropbox_client = client.DropboxClient(sess)

    # upload database
    backup = new_backup()
    filename = os.path.join(get_backups_path(),
                            os.path.basename(backup.backup_path_db))
    upload_file_to_dropbox(filename, "/database", dropbox_client)

    webnotes.conn.close()
    response = dropbox_client.metadata("/files")

    # upload files to files folder
    did_not_upload = []
    error_log = []
    path = get_files_path()
    for filename in os.listdir(path):
        filename = cstr(filename)
        if filename in ignore_list:
            continue

        found = False
        filepath = os.path.join(path, filename)
        for file_metadata in response["contents"]:
            if os.path.basename(filepath) == os.path.basename(
                    file_metadata["path"]) and os.stat(
                        filepath).st_size == int(file_metadata["bytes"]):
                found = True
                break
        if not found:
            try:
                upload_file_to_dropbox(filepath, "/files", dropbox_client)
            except Exception:
                did_not_upload.append(filename)
                error_log.append(webnotes.getTraceback())

    webnotes.connect()
    return did_not_upload, list(set(error_log))
def backup_to_gdrive():
	from webnotes.utils.backups import new_backup
	if not webnotes.conn:
		webnotes.connect()
	get_gdrive_flow()
	credentials_json = webnotes.conn.get_value("Backup Manager", None, "gdrive_credentials")
	credentials = oauth2client.client.Credentials.new_from_json(credentials_json)
	http = httplib2.Http()
	http = credentials.authorize(http)
	drive_service = build('drive', 'v2', http=http)

	# upload database
	backup = new_backup()
	path = os.path.join(get_base_path(), "public", "backups")
	filename = os.path.join(path, os.path.basename(backup.backup_path_db))
	
	# upload files to database folder
	upload_files(filename, 'application/x-gzip', drive_service, 
		webnotes.conn.get_value("Backup Manager", None, "database_folder_id"))
	
	# upload files to files folder
	did_not_upload = []
	error_log = []
	
	files_folder_id = webnotes.conn.get_value("Backup Manager", None, "files_folder_id")
	
	webnotes.conn.close()
	path = os.path.join(get_base_path(), "public", "files")
	for filename in os.listdir(path):
		found = False
		filepath = os.path.join(path, filename)
		ext = filename.split('.')[-1]
		size = os.path.getsize(filepath)
		if ext == 'gz' or ext == 'gzip':
			mimetype = 'application/x-gzip'
		else:
			mimetype = mimetypes.types_map.get("." + ext) or "application/octet-stream"
		
		#Compare Local File with Server File
		param = {}
	  	children = drive_service.children().list(folderId=files_folder_id, **param).execute()
	  	for child in children.get('items', []):
			file = drive_service.files().get(fileId=child['id']).execute()
			if filename == file['title'] and size == int(file['fileSize']):
				found = True
				break
		if not found:
			try:
				upload_files(filepath, mimetype, drive_service, files_folder_id)
			except Exception, e:
				did_not_upload.append(filename)
				error_log.append(cstr(e))
Exemple #49
0
def backup_to_dropbox():
    from dropbox import client, session
    from conf import dropbox_access_key, dropbox_secret_key
    from webnotes.utils.backups import new_backup

    if not webnotes.conn:
        webnotes.connect()

    sess = session.DropboxSession(dropbox_access_key, dropbox_secret_key, "app_folder")

    sess.set_token(
        webnotes.conn.get_value("Backup Manager", None, "dropbox_access_key"),
        webnotes.conn.get_value("Backup Manager", None, "dropbox_access_secret"),
    )

    dropbox_client = client.DropboxClient(sess)

    # upload database
    backup = new_backup()
    filename = os.path.join(get_base_path(), "public", "backups", os.path.basename(backup.backup_path_db))
    upload_file_to_dropbox(filename, "/database", dropbox_client)

    webnotes.conn.close()
    response = dropbox_client.metadata("/files")

    # upload files to files folder
    did_not_upload = []
    error_log = []
    path = os.path.join(get_base_path(), "public", "files")
    for filename in os.listdir(path):
        filename = cstr(filename)
        if filename in ignore_list:
            continue

        found = False
        filepath = os.path.join(path, filename)
        for file_metadata in response["contents"]:
            if os.path.basename(filepath) == os.path.basename(file_metadata["path"]) and os.stat(
                filepath
            ).st_size == int(file_metadata["bytes"]):
                found = True
                break
        if not found:
            try:
                upload_file_to_dropbox(filepath, "/files", dropbox_client)
            except Exception:
                did_not_upload.append(filename)
                error_log.append(webnotes.getTraceback())

    webnotes.connect()
    return did_not_upload, list(set(error_log))
Exemple #50
0
def build(page_name):
	if not webnotes.conn:
		webnotes.connect()
	
	build_method = (build_json if is_ajax() else build_page)
	try:
		return build_method(page_name)

	except webnotes.DoesNotExistError:
		hooks = webnotes.get_hooks()
		if hooks.website_catch_all:
			return build_method(hooks.website_catch_all[0])
		else:
			return build_method("404")
Exemple #51
0
def build_page(page_name):
	if not webnotes.conn:
		webnotes.connect()

	sitemap_options = webnotes.doc("Website Sitemap", page_name).fields
	
	page_options = webnotes.doc("Website Sitemap Config", 
		sitemap_options.get("website_sitemap_config")).fields.update({
			"page_name":sitemap_options.page_name,
			"docname":sitemap_options.docname
		})
		
	if not page_options:
		raise PageNotFoundError
	else:
		page_options["page_name"] = page_name
	
	basepath = webnotes.utils.get_base_path()
	no_cache = page_options.get("no_cache")
	

	# if generator, then load bean, pass arguments
	if page_options.get("page_or_generator")=="Generator":
		doctype = page_options.get("ref_doctype")
		obj = webnotes.get_obj(doctype, page_options["docname"], with_children=True)

		if hasattr(obj, 'get_context'):
			obj.get_context()

		context = webnotes._dict(obj.doc.fields)
		context["obj"] = obj
	else:
		# page
		context = webnotes._dict({ 'name': page_name })
		if page_options.get("controller"):
			module = webnotes.get_module(page_options.get("controller"))
			if module and hasattr(module, "get_context"):
				context.update(module.get_context())
	
	context.update(get_website_settings())

	jenv = webnotes.get_jenv()
	context["base_template"] = jenv.get_template(webnotes.get_config().get("base_template"))
	
	template_name = page_options['template_path']	
	html = jenv.get_template(template_name).render(context)
	
	if not no_cache:
		webnotes.cache().set_value("page:" + page_name, html)
	return html
def make_test_records(doctype, verbose=0):
    webnotes.mute_emails = True
    if not webnotes.conn:
        webnotes.connect()

    for options in get_dependencies(doctype):
        if options.startswith("link:"):
            options = options[5:]
        if options == "[Select]":
            continue

        if options not in webnotes.test_objects:
            webnotes.test_objects[options] = []
            make_test_records(options, verbose)
            make_test_records_for_doctype(options, verbose)
Exemple #53
0
def build_message_files():
    """build from doctypes, pages, database and framework"""
    if not webnotes.conn:
        webnotes.connect()

    build_for_pages('lib/core')
    build_for_pages('app')

    build_from_doctype_code('lib/core')
    build_from_doctype_code('app')

    # doctype
    build_from_database()

    build_for_framework('lib/webnotes', 'py', with_doctype_names=True)
    build_for_framework('lib/public/js/wn', 'js')
    build_for_framework('app/public/js', 'js', with_doctype_names=True)
Exemple #54
0
def upload_files(name, mimetype, service, folder_id):
	if not webnotes.conn:
		webnotes.connect()
	file_name = os.path.basename(name)
	media_body = MediaFileUpload(name, mimetype=mimetype, resumable=True)
	body = {
		'title': file_name,
		'description': 'Backup File',
		'mimetype': mimetype,
		'parents': [{
			'kind': 'drive#filelink',
			'id': folder_id
		}]
	}
	request = service.files().insert(body=body, media_body=media_body)
	response = None
	while response is None:
		status, response = request.next_chunk()
Exemple #55
0
def latest(site=None, verbose=True):
    import webnotes.modules.patch_handler
    import webnotes.model.sync

    webnotes.connect(site=site)

    try:
        # run patches
        webnotes.local.patch_log_list = []
        webnotes.modules.patch_handler.run_all()
        if verbose:
            print "\n".join(webnotes.local.patch_log_list)

        # sync
        webnotes.model.sync.sync_all()
    except webnotes.modules.patch_handler.PatchError, e:
        print "\n".join(webnotes.local.patch_log_list)
        raise e
Exemple #56
0
def make(reset=False, simulate=True):
    #webnotes.flags.print_messages = True
    webnotes.flags.mute_emails = True
    webnotes.flags.rollback_on_exception = True

    if not webnotes.conf.demo_db_name:
        raise Exception("conf.py does not have demo_db_name")

    if reset:
        setup()
    else:
        if webnotes.conn:
            webnotes.conn.close()

        webnotes.connect(db_name=webnotes.conf.demo_db_name)

    if simulate:
        _simulate()
Exemple #57
0
def log(method):
    """log error in patch_log"""
    import webnotes

    if not (webnotes.conn and webnotes.conn._conn):
        webnotes.connect()

    webnotes.conn.rollback()
    traceback = webnotes.getTraceback()

    import webnotes.utils
    webnotes.conn.begin()
    d = webnotes.doc("Scheduler Log")
    d.method = method
    d.error = traceback
    d.save()
    webnotes.conn.commit()

    return traceback
Exemple #58
0
def take_backups_dropbox():
    did_not_upload, error_log = [], []
    try:
        from setup.doctype.backup_manager.backup_dropbox import backup_to_dropbox
        did_not_upload, error_log = backup_to_dropbox()
        if did_not_upload: raise Exception

        send_email(True, "Dropbox")
    except Exception:
        file_and_error = [
            " - ".join(f) for f in zip(did_not_upload, error_log)
        ]
        error_message = ("\n".join(file_and_error) + "\n" +
                         webnotes.getTraceback())
        webnotes.errprint(error_message)

        if not webnotes.conn:
            webnotes.connect()

        send_email(False, "Dropbox", error_message)
def make():
	from webnotes.webutils import get_home_page
	from webnotes.utils import get_path

	if not webnotes.conn:
		webnotes.connect()
	
	home_page = get_home_page()

	if not os.path.exists(get_path("public", "js")):
		os.makedirs(get_path("public", "js"))
	fname = os.path.join(get_path("public", "js", "wn-web.js"))
	with open(fname, 'w') as f:
		f.write(get_web_script())

	if not os.path.exists(get_path("public", "css")):
		os.makedirs(get_path("public", "css"))
	fname = os.path.join(get_path("public", "css", "wn-web.css"))
	with open(fname, 'w') as f:
		f.write(get_web_style())