diff --git a/core/page/data_import_tool/data_import_tool.py b/core/page/data_import_tool/data_import_tool.py index 2de6dae09e..21d17c872a 100644 --- a/core/page/data_import_tool/data_import_tool.py +++ b/core/page/data_import_tool/data_import_tool.py @@ -220,7 +220,6 @@ def upload(rows = None, submit_after_import=None, ignore_encoding_errors=False, webnotes.mute_emails = True webnotes.check_admin_or_system_manager() # extra input params - import json params = json.loads(webnotes.form_dict.get("params") or '{}') if params.get("_submit"): @@ -498,3 +497,20 @@ def export_json(doctype, name, path): del d["name"] d["__islocal"] = 1 outfile.write(json.dumps(doclist, default=json_handler, indent=1, sort_keys=True)) + +def import_doclist(path): + import os + if os.path.isdir(path): + files = [os.path.join(path, f) for f in os.listdir(path)] + else: + files = [path] + + for f in files: + if f.endswith(".json"): + with open(f, "r") as infile: + b = webnotes.bean(json.loads(infile.read())).insert_or_update() + print "Imported: " + b.doc.doctype + " / " + b.doc.name + webnotes.conn.commit() + if f.endswith(".csv"): + import_file_by_path(f, ignore_links=True) + webnotes.conn.commit() \ No newline at end of file diff --git a/webnotes/__init__.py b/webnotes/__init__.py index 6ba2f2a771..f14b6563bb 100644 --- a/webnotes/__init__.py +++ b/webnotes/__init__.py @@ -39,6 +39,8 @@ def _(msg): """translate object in current lang, if exists""" if hasattr(local, 'translations'): return local.translations.get(lang, {}).get(msg, msg) + + return msg def set_user_lang(user, user_language=None): from webnotes.translate import get_lang_dict diff --git a/webnotes/install_lib/install.py b/webnotes/install_lib/install.py index 2e090d9b31..6b9e7c55c8 100755 --- a/webnotes/install_lib/install.py +++ b/webnotes/install_lib/install.py @@ -19,21 +19,17 @@ class Installer: make_conf(db_name, site=site) self.site = site - self.make_connection(root_login, root_password, site) + self.make_connection(root_login, root_password) webnotes.local.conn = self.conn webnotes.local.session = webnotes._dict({'user':'Administrator'}) self.dbman = DbManager(self.conn) - def make_connection(self, root_login, root_password, site): + def make_connection(self, root_login, root_password): if root_login: if not root_password: - try: - webnotes.init(site=site) - root_password = webnotes.conf.get("root_password") or None - except ImportError: - pass + root_password = webnotes.conf.get("root_password") or None if not root_password: root_password = getpass.getpass("MySQL root password: ") @@ -183,7 +179,11 @@ class Installer: def make_conf(db_name=None, db_password=None, site=None, site_config=None): try: import conf + webnotes.init(site=site) + if not site and webnotes.conf.site: + site = webnotes.conf.site + if site: # conf exists and site is specified, create site_config.json make_site_config(site, db_name, db_password, site_config) diff --git a/webnotes/sessions.py b/webnotes/sessions.py index f541d54fc0..b3dfab409f 100644 --- a/webnotes/sessions.py +++ b/webnotes/sessions.py @@ -29,8 +29,13 @@ def clear_cache(user=None): if user: cache.delete_value("bootinfo:" + user) - if webnotes.session and webnotes.session.sid: - cache.delete_value("session:" + webnotes.session.sid) + if webnotes.session: + if user==webnotes.session.user and webnotes.session.sid: + cache.delete_value("session:" + webnotes.session.sid) + else: + for sid in webnotes.conn.sql_list("""select sid from tabSessions + where user=%s""", user): + cache.delete_value("session:" + sid) else: for sess in webnotes.conn.sql("""select user, sid from tabSessions""", as_dict=1): cache.delete_value("sesssion:" + sess.sid) diff --git a/webnotes/utils/scheduler.py b/webnotes/utils/scheduler.py index d98564c52a..b17c5ec7f3 100644 --- a/webnotes/utils/scheduler.py +++ b/webnotes/utils/scheduler.py @@ -16,7 +16,7 @@ on the need. """ import webnotes -def execute(): +def execute(site=None): """ execute jobs this method triggers the other scheduler events @@ -29,7 +29,7 @@ def execute(): format = '%Y-%m-%d %H:%M:%S' if not webnotes.conn: - webnotes.connect() + webnotes.connect(site=site) out = [] diff --git a/wnf.py b/wnf.py index 7467a48fde..f453af08c9 100755 --- a/wnf.py +++ b/wnf.py @@ -12,21 +12,25 @@ if __name__=="__main__": import webnotes def main(): - def run(args): - for fn, opts in args.items(): - if (opts or isinstance(opts, list)) and globals().get(fn): - return globals().get(fn)(opts, args) - - webnotes.destroy() - parsed_args = webnotes._dict(vars(setup_parser())) + fn = get_function(parsed_args) if parsed_args.get("site")=="all": for site in get_sites(): args = parsed_args.copy() args["site"] = site - run(args) + run(fn, args) else: - run(parsed_args) + run(fn, parsed_args) + +def run(fn, args): + out = globals().get(fn)(args.get(fn), args) + webnotes.destroy() + return out + +def get_function(args): + for fn, opts in args.items(): + if (opts or isinstance(opts, list)) and globals().get(fn): + return fn def get_sites(): pass @@ -102,9 +106,9 @@ def setup_utilities(parser): help="Reset permissions for all doctypes") # scheduler - parser.add_argument("--scheduler", "--run_scheduler", default=False, action="store_true", + parser.add_argument("--run_scheduler", default=False, action="store_true", help="Trigger scheduler") - parser.add_argument("--scheduler_event", "--run_scheduler_event", nargs=1, + parser.add_argument("--run_scheduler_event", nargs=1, metavar="all | daily | weekly | monthly", help="Run a scheduler event") @@ -253,6 +257,87 @@ def make_conf(opts, args): from webnotes.install_lib.install import make_conf make_conf(*opts, site=args.site) +# clear +def clear_cache(opts, args): + import webnotes.sessions + webnotes.connect(site=args.site) + webnotes.sessions.clear_cache() + +def clear_web(opts, args): + import webnotes.webutils + webnotes.connect(site=args.site) + webnotes.webutils.clear_cache() + +def reset_perms(opts, args): + webnotes.connect(site=args.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) + +# scheduler +def run_scheduler(opts, args): + import webnotes.utils.scheduler + webnotes.connect(site=args.site) + print webnotes.utils.scheduler.execute() + +def run_scheduler_event(opts, args): + import webnotes.utils.scheduler + webnotes.connect(site=args.site) + print webnotes.utils.scheduler.trigger("execute_" + opts[0]) + +# replace +def replace(opts, args): + print opts + replace_code('.', *opts, force=args.force) + +# import/export +def export_doc(opts, args): + import webnotes.modules + webnotes.connect(site=args.site) + webnotes.modules.export_doc(*opts) + +def export_doclist(opts, args): + from core.page.data_import_tool import data_import_tool + webnotes.connect(site=args.site) + data_import_tool.export_json(*opts) + +def export_csv(opts, args): + from core.page.data_import_tool import data_import_tool + webnotes.connect(site=args.site) + data_import_tool.export_csv(*opts) + +def import_doclist(opts, args): + from core.page.data_import_tool import data_import_tool + webnotes.connect(site=args.site) + data_import_tool.import_doclist(*opts) + +# translation +def build_message_files(opts, args): + import webnotes.translate + webnotes.connect(site=args.site) + webnotes.translate.build_message_files() + +def export_messages(opts, args): + import webnotes.translate + webnotes.connect(site=args.site) + webnotes.translate.export_messages(*opts) + +def import_messages(opts, args): + import webnotes.translate + webnotes.connect(site=args.site) + webnotes.translate.import_messages(*opts) + +def google_translate(opts, args): + import webnotes.translate + webnotes.connect(site=args.site) + webnotes.translate.google_translate(*opts) + +def translate(opts, args): + import webnotes.translate + webnotes.connect(site=args.site) + webnotes.translate.translate(*opts) + # git def git(opts, args=None): cmd = opts