def backup_to_dropbox(upload_db_backup=True): if not dataent.db: dataent.connect() # upload database dropbox_settings = get_dropbox_settings() if not dropbox_settings['access_token']: access_token = generate_oauth2_access_token_from_oauth1_token(dropbox_settings) if not access_token.get('oauth2_token'): return 'Failed backup upload', 'No Access Token exists! Please generate the access token for Dropbox.' dropbox_settings['access_token'] = access_token['oauth2_token'] set_dropbox_access_token(access_token['oauth2_token']) # Wait for 60 seconds before throwing ReadTimeout, in case server doesn't respond dropbox_client = dropbox.Dropbox(dropbox_settings['access_token'], timeout=60) if upload_db_backup: backup = new_backup(ignore_files=True) filename = os.path.join(get_backups_path(), os.path.basename(backup.backup_path_db)) upload_file_to_dropbox(filename, "/database", dropbox_client) # upload files to files folder did_not_upload = [] error_log = [] if dropbox_settings['file_backup']: upload_from_folder(get_files_path(), 0, "/files", dropbox_client, did_not_upload, error_log) upload_from_folder(get_files_path(is_private=1), 1, "/private/files", dropbox_client, did_not_upload, error_log) return did_not_upload, list(set(error_log))
def list_apps(context): "List apps in site" site = get_site(context) dataent.init(site=site) dataent.connect() print("\n".join(dataent.get_installed_apps())) dataent.destroy()
def make_demo(context, site, domain='Manufacturing', days=100, resume=False, reinstall=False): "Reinstall site and setup demo" from dataent.commands.site import _reinstall from dataent.installer import install_app site = get_site(context) if resume: with dataent.init_site(site): dataent.connect() from epaas.demo import demo demo.simulate(days=days) else: if reinstall: _reinstall(site, yes=True) with dataent.init_site(site=site): dataent.connect() if not 'epaas' in dataent.get_installed_apps(): install_app('epaas') # import needs site from epaas.demo import demo demo.make(domain, days)
def _reinstall(site, admin_password=None, mariadb_root_username=None, mariadb_root_password=None, yes=False, verbose=False): if not yes: click.confirm( 'This will wipe your database. Are you sure you want to reinstall?', abort=True) try: dataent.init(site=site) dataent.connect() dataent.clear_cache() installed = dataent.get_installed_apps() dataent.clear_cache() except Exception: installed = [] finally: if dataent.db: dataent.db.close() dataent.destroy() dataent.init(site=site) _new_site(dataent.conf.db_name, site, verbose=verbose, force=True, reinstall=True, install_apps=installed, mariadb_root_username=mariadb_root_username, mariadb_root_password=mariadb_root_password, admin_password=admin_password)
def send_email(success, service_name, error_status=None): if success: if dataent.db.get_value("Dropbox Settings", None, "send_email_for_successful_backup") == '0': return subject = "Backup Upload Successful" message ="""<h3>Backup Uploaded Successfully</h3><p>Hi there, this is just to inform you that your backup was successfully uploaded to your %s account. So relax!</p> """ % service_name else: subject = "[Warning] Backup Upload Failed" message ="""<h3>Backup Upload Failed</h3><p>Oops, your automated backup to %s failed.</p> <p>Error message: <br> <pre><code>%s</code></pre> </p> <p>Please contact your system manager for more information.</p> """ % (service_name, error_status) if not dataent.db: dataent.connect() recipients = split_emails(dataent.db.get_value("Dropbox Settings", None, "send_notifications_to")) dataent.sendmail(recipients=recipients, subject=subject, message=message)
def request(context, args=None, path=None): "Run a request as an admin" import dataent.handler import dataent.api for site in context.sites: try: dataent.init(site=site) dataent.connect() if args: if "?" in args: dataent.local.form_dict = dataent._dict( [a.split("=") for a in args.split("?")[-1].split("&")]) else: dataent.local.form_dict = dataent._dict() if args.startswith("/api/method"): dataent.local.form_dict.cmd = args.split("?")[0].split( "/")[-1] elif path: with open(os.path.join('..', path), 'r') as f: args = json.loads(f.read()) dataent.local.form_dict = dataent._dict(args) dataent.handler.execute_cmd(dataent.form_dict.cmd) print(dataent.response) finally: dataent.destroy()
def import_csv(context, path, only_insert=False, submit_after_import=False, ignore_encoding_errors=False, no_email=True): "Import CSV using data import" from dataent.core.doctype.data_import import importer from dataent.utils.csvutils import read_csv_content site = get_site(context) if not os.path.exists(path): path = os.path.join('..', path) if not os.path.exists(path): print('Invalid path {0}'.format(path)) sys.exit(1) with open(path, 'r') as csvfile: content = read_csv_content(csvfile.read()) dataent.init(site=site) dataent.connect() try: importer.upload(content, submit_after_import=submit_after_import, no_email=no_email, ignore_encoding_errors=ignore_encoding_errors, overwrite=not only_insert, via_console=True) dataent.db.commit() except Exception: print(dataent.get_traceback()) dataent.destroy()
def scheduler(context, state, site=None): from dataent.installer import update_site_config import dataent.utils.scheduler if not site: site = get_site(context) try: dataent.init(site=site) if state == 'pause': update_site_config('pause_scheduler', 1) elif state == 'resume': update_site_config('pause_scheduler', 0) elif state == 'disable': dataent.connect() dataent.utils.scheduler.disable_scheduler() dataent.db.commit() elif state == 'enable': dataent.connect() dataent.utils.scheduler.enable_scheduler() dataent.db.commit() print('Scheduler {0}d for site {1}'.format(state, site)) finally: dataent.destroy()
def backup(context, with_files=False, backup_path_db=None, backup_path_files=None, backup_path_private_files=None, quiet=False): "Backup" from dataent.utils.backups import scheduled_backup verbose = context.verbose for site in context.sites: dataent.init(site=site) dataent.connect() odb = scheduled_backup( ignore_files=not with_files, backup_path_db=backup_path_db, backup_path_files=backup_path_files, backup_path_private_files=backup_path_private_files, force=True) if verbose: from dataent.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()) print("private files backup taken -", odb.backup_path_private_files, "- on", now()) dataent.destroy()
def enqueue_events_for_site(site, queued_jobs): def log_and_raise(): dataent.logger(__name__).error( 'Exception in Enqueue Events for Site {0}'.format(site) + '\n' + dataent.get_traceback()) raise # pylint: disable=misplaced-bare-raise try: dataent.init(site=site) if dataent.local.conf.maintenance_mode: return if dataent.local.conf.pause_scheduler: return dataent.connect() if is_scheduler_disabled(): return enqueue_events(site=site, queued_jobs=queued_jobs) dataent.logger(__name__).debug( 'Queued events for site {0}'.format(site)) except pymysql.OperationalError as e: if e.args[0] == ER.ACCESS_DENIED_ERROR: dataent.logger(__name__).debug( 'Access denied for site {0}'.format(site)) else: log_and_raise() except: log_and_raise() finally: dataent.destroy()
def scheduler_task(site, event, handler, now=False): '''This is a wrapper function that runs a hooks.scheduler_events method''' dataent.logger(__name__).info( 'running {handler} for {site} for event: {event}'.format( handler=handler, site=site, event=event)) try: if not now: dataent.connect(site=site) dataent.flags.in_scheduler = True dataent.get_attr(handler)() except Exception: dataent.db.rollback() traceback = log( handler, "Method: {event}, Handler: {handler}".format(event=event, handler=handler)) dataent.logger(__name__).error(traceback) raise else: dataent.db.commit() dataent.logger(__name__).info( 'ran {handler} for {site} for event: {event}'.format(handler=handler, site=site, event=event))
def console(context): "Start ipython console for a site" site = get_site(context) dataent.init(site=site) dataent.connect() dataent.local.lang = dataent.db.get_default("lang") import IPython IPython.embed(display_banner="")
def disable_user(context, email): site = get_site(context) with dataent.init_site(site): dataent.connect() user = dataent.get_doc("User", email) user.enabled = 0 user.save(ignore_permissions=True) dataent.db.commit()
def clear_website_cache(context): "Clear website cache" import dataent.website.render for site in context.sites: try: dataent.init(site=site) dataent.connect() dataent.website.render.clear_cache() finally: dataent.destroy()
def install_app(context, app): "Install a new app to site" from dataent.installer import install_app as _install_app for site in context.sites: dataent.init(site=site) dataent.connect() try: _install_app(app, verbose=context.verbose) finally: dataent.destroy()
def export_csv(context, doctype, path): "Export data import template with data for DocType" from dataent.core.doctype.data_import import data_import for site in context.sites: try: dataent.init(site=site) dataent.connect() data_import.export_csv(doctype, path) finally: dataent.destroy()
def export_doc(context, doctype, docname): "Export a single document to csv" import dataent.modules for site in context.sites: try: dataent.init(site=site) dataent.connect() dataent.modules.export_doc(doctype, docname) finally: dataent.destroy()
def uninstall(context, app, dry_run=False, yes=False): "Remove app and linked modules from site" from dataent.installer import remove_app for site in context.sites: try: dataent.init(site=site) dataent.connect() remove_app(app, dry_run, yes) finally: dataent.destroy()
def remove_from_installed_apps(context, app): "Remove app from site's installed-apps list" from dataent.installer import remove_from_installed_apps for site in context.sites: try: dataent.init(site=site) dataent.connect() remove_from_installed_apps(app) finally: dataent.destroy()
def reload_doctype(context, doctype): "Reload schema for a DocType" for site in context.sites: try: dataent.init(site=site) dataent.connect() dataent.reload_doctype(doctype, force=context.force) dataent.db.commit() finally: dataent.destroy()
def trigger_scheduler_event(context, event): "Trigger a scheduler event" import dataent.utils.scheduler for site in context.sites: try: dataent.init(site=site) dataent.connect() dataent.utils.scheduler.trigger(site, event, now=True) finally: dataent.destroy()
def export_json(context, doctype, path, name=None): "Export doclist as json to the given path, use '-' as name for Singles." from dataent.core.doctype.data_import import data_import for site in context.sites: try: dataent.init(site=site) dataent.connect() data_import.export_json(doctype, path, name=name) finally: dataent.destroy()
def export_fixtures(context): "Export fixtures" from dataent.utils.fixtures import export_fixtures for site in context.sites: try: dataent.init(site=site) dataent.connect() export_fixtures() finally: dataent.destroy()
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(dataent.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: dataent.flags.print_messages = verbose dataent.flags.in_test = True if not dataent.db: dataent.connect() # if not dataent.conf.get("db_name").startswith("test_"): # raise Exception, 'db_name must start with "test_"' # workaround! since there is no separate test db dataent.clear_cache() dataent.utils.scheduler.disable_scheduler() set_test_email_config() if not dataent.flags.skip_before_tests: if verbose: print('Running "before_tests" hooks') for fn in dataent.get_hooks("before_tests", app_name=app): dataent.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) dataent.db.commit() # workaround! since there is no separate test db dataent.clear_cache() return ret finally: if xmloutput_fh: xmloutput_fh.flush() xmloutput_fh.close()
def run_patch(context, module): "Run a particular patch" import dataent.modules.patch_handler for site in context.sites: dataent.init(site=site) try: dataent.connect() dataent.modules.patch_handler.run_single(module, force=context.force) finally: dataent.destroy()
def destroy_all_sessions(context, reason=None): "Clear sessions of all users (logs them out)" import dataent.sessions for site in context.sites: try: dataent.init(site=site) dataent.connect() dataent.sessions.clear_all_sessions(reason) dataent.db.commit() finally: dataent.destroy()
def _is_scheduler_enabled(): enable_scheduler = False try: dataent.connect() enable_scheduler = cint(dataent.db.get_single_value("System Settings", "enable_scheduler")) and True or False except: pass finally: dataent.db.close() return enable_scheduler
def add_system_manager(context, email, first_name, last_name, send_welcome_email, password): "Add a new system manager to a site" import dataent.utils.user for site in context.sites: dataent.connect(site=site) try: dataent.utils.user.add_system_manager(email, first_name, last_name, send_welcome_email, password) dataent.db.commit() finally: dataent.destroy()
def disable_scheduler(context): "Disable scheduler" import dataent.utils.scheduler for site in context.sites: try: dataent.init(site=site) dataent.connect() dataent.utils.scheduler.disable_scheduler() dataent.db.commit() print("Disabled for", site) finally: dataent.destroy()
def build(path): if not dataent.db: dataent.connect() try: return build_page(path) except dataent.DoesNotExistError: hooks = dataent.get_hooks() if hooks.website_catch_all: path = hooks.website_catch_all[0] return build_page(path) else: raise