def get_meteor_ddp_default_conn_url(): common_config = frappe.get_file_json("common_site_config.json") site_path = frappe.get_site_path() site_config = frappe.get_file_json( os.path.join(site_path, "site_config.json")) ddp = site_config.get("DDP_DEFAULT_CONNECTION_URL") or common_config.get( "DDP_DEFAULT_CONNECTION_URL") or "http://localhost:3000/" return ddp
def get_reaction_webapp_path(): common_config = frappe.get_file_json("common_site_config.json") site_path = frappe.get_site_path() site_config = frappe.get_file_json( os.path.join(site_path, "site_config.json")) meteor_app = site_config.get("reaction_webapp_path") or common_config.get( "reaction_webapp_path") or "" return meteor_app
def get_reaction_server_url(): common_config = frappe.get_file_json("common_site_config.json") site_path = frappe.get_site_path() site_config = frappe.get_file_json( os.path.join(site_path, "site_config.json")) meteor_server = site_config.get("reaction_server") or common_config.get( "reaction_server") or "http://localhost:3000" return meteor_server
def get_meteor_root_url(): common_config = frappe.get_file_json("common_site_config.json") site_path = frappe.get_site_path() site_config = frappe.get_file_json( os.path.join(site_path, "site_config.json")) root_url = site_config.get("ROOT_URL") or common_config.get( "ROOT_URL") or "http://localhost:3000" return root_url
def get_meteor_root_url_paht_prefix(): common_config = frappe.get_file_json("common_site_config.json") site_path = frappe.get_site_path() site_config = frappe.get_file_json( os.path.join(site_path, "site_config.json")) root_url_prefix = site_config.get( "ROOT_URL_PATH_PREFIX") or common_config.get( "ROOT_URL_PATH_PREFIX") or "" return root_url_prefix
def get_fluorine_conf(name): path_reactivity = get_path_reactivity() common_site_config = os.path.join(path_reactivity, "common_site_config.json") if os.path.exists(common_site_config): f = frappe.get_file_json(common_site_config) return f.get(name, None) return None
def get_site_db_name(site_name): config={} site_path= '/home/frappe/frappe-bench/sites/'+site_name site_config=os.path.join(site_path, "site_config.json") config.update(get_file_json(site_config)) frappe.local.conf= _dict(config) return frappe.local.conf.db_name
def get_db_config(database): config = {} db_config = os.path.join(frappe.local.site_path, ".".join([database, 'json'])) if os.path.exists(db_config): config.update(frappe.get_file_json(db_config)) return config
def set_meteor_config(): common_config = frappe.get_file_json("common_site_config.json") site_path = frappe.get_site_path() site_config = frappe.get_file_json( os.path.join(site_path, "site_config.json")) meteor_config["DDP_DEFAULT_CONNECTION_URL"] = site_config.get( "DDP_DEFAULT_CONNECTION_URL") or common_config.get( "DDP_DEFAULT_CONNECTION_URL") or "http://localhost:3000/" meteor_config["ROOT_URL_PATH_PREFIX"] = site_config.get( "ROOT_URL_PATH_PREFIX") or common_config.get( "ROOT_URL_PATH_PREFIX") or "" root_url = site_config.get("ROOT_URL") or common_config.get( "ROOT_URL") or "http://localhost:3000" if (not root_url.endswith("/")): root_url = root_url + "/" meteor_config["ROOT_URL"] = root_url
def copy_site_config(self): site_config_backup_path = os.path.join(get_backup_path(), "{}-site_config_backup.json".format(self.todays_date)) site_config_path = os.path.join(frappe.get_site_path(), "site_config.json") site_config = {} if os.path.exists(site_config_path): site_config.update(frappe.get_file_json(site_config_path)) with open(site_config_backup_path, "w") as f: f.write(json.dumps(site_config, indent=2)) f.flush() self.site_config_backup_path = site_config_backup_path
def get_google_auth_flow(): from oauth2client.client import flow_from_clientsecrets google_config_path = frappe.get_site_path("google_config.json") google_config = frappe.get_file_json(google_config_path) flow = flow_from_clientsecrets( google_config_path, scope=[ 'https://www.googleapis.com/auth/userinfo.profile', 'https://www.googleapis.com/auth/userinfo.email' ], redirect_uri=google_config.get("web").get("redirect_uris")[0]) return flow
def validate(self): json_data = frappe.get_file_json(frappe.local.site_path + self.cf_upload_gstr_2b_data) return_period = json_data['data']['rtnprd'] existing_doc = frappe.db.get_value('CD GSTR 2B Data Upload Tool', {'cf_return_period': return_period}, 'name') if existing_doc and not existing_doc == self.name: frappe.throw( _(f'Unable to proceed. Already another document {comma_and("""<a href="#Form/CD GSTR 2B Data Upload Tool/{0}">{1}</a>""".format(existing_doc, existing_doc))} uploaded for the return period {frappe.bold(return_period)}.' )) if not json_data['data']['gstin'] == self.cf_company_gstin: frappe.throw( _(f'Invalid JSON. Company GSTIN mismatched with uploaded 2B data.' ))
def get_default_listview_fields(doctype): meta = frappe.get_meta(doctype) path = frappe.get_module_path(frappe.scrub(meta.module), "doctype", frappe.scrub(meta.name), frappe.scrub(meta.name) + ".json") doctype_json = frappe.get_file_json(path) fields = [ f.get("fieldname") for f in doctype_json.get("fields") if f.get("in_list_view") ] if meta.title_field and not meta.title_field.strip() in fields: fields.append(meta.title_field.strip()) return {doctype: fields}
def update_site_files(): common_config = frappe.get_file_json("common_site_config.json") #site_path = frappe.get_site_path() #site_config = frappe.get_file_json(os.path.join(site_path, "site_config.json")) common_config["DDP_DEFAULT_CONNECTION_URL"] = "http://localhost:3000/" common_config["ROOT_URL_PATH_PREFIX"] = "" common_config["MONGOWEB"] = { "host": "localhost", "port": "3001" } common_config["ROOT_URL"] = "http://localhost:3000" common_config["reaction_webapp_path"] = "../apps/reaction_ecommerce/reaction_ecommerce/www/webreaction" common_config["reaction_server"] = "http://localhost:3000" with open("common_site_config.json", 'w') as txtfile: txtfile.write(frappe.as_json(common_config))
def rename_inside_controller(self, new, old, new_path): for fname in ('{}.js', '{}.py', '{}_list.js', '{}_calendar.js', 'test_{}.py', 'test_{}.js'): fname = os.path.join(new_path, fname.format(frappe.scrub(new))) if os.path.exists(fname): with open(fname, 'r') as f: code = f.read() with open(fname, 'w') as f: file_content = code.replace(old, new) # replace str with full str (js controllers) file_content = file_content.replace(frappe.scrub(old), frappe.scrub(new)) # replace str with _ (py imports) file_content = file_content.replace(old.replace(' ', ''), new.replace(' ', '')) # replace str (py controllers) f.write(file_content) # updating json file with new name doctype_json_path = os.path.join(new_path, '{}.json'.format(frappe.scrub(new))) current_data = frappe.get_file_json(doctype_json_path) current_data['name'] = new with open(doctype_json_path, 'w') as f: json.dump(current_data, f, indent=1)
def after_insert(self): enqueued_jobs = [d.get("job_name") for d in get_info()] json_data = frappe.get_file_json(frappe.local.site_path + self.cf_upload_gstr_2b_data) if self.name in enqueued_jobs: frappe.msgprint( _("Create GSTR 2B entries already in progress. Please wait for sometime." )) else: enqueue(create_gstr2b_entries, queue="default", timeout=6000, event='create_gstr2b_entries', json_data=json_data, doc=self, job_name=self.name) frappe.msgprint( _("Create GSTR 2B entries job added to the queue. Please check after sometime." ))
def update_password(sites, user_password, user_email): import getpass from frappe.utils.password import update_password as upd_pwd for site in sites: try: frappe.init(site=site) while not user_password: user_password = getpass.getpass( "Administrator's password for {0}: ".format(site)) frappe.connect() default_email = get_admin_email() if not user_email: user_email = raw_input( "Administrator's email for {0}, default email: ({1}) ". format(site, default_email)) # set password has meteor.js send to the server; as SHA256. from reaction_ecommerce.utils.users import hashpw pwd_hash = hashpw(user_password) upd_pwd('Administrator', pwd_hash) site_path = frappe.get_site_path() site_config = frappe.get_file_json( os.path.join(site_path, "site_config.json")) site_config["admin_password"] = pwd_hash site_config["admin_email"] = user_email or default_email if user_email and default_email != user_email: set_admin_email(user_email) frappe.db.commit() #write to file. with open(os.path.join(site_path, "site_config.json"), 'w') as txtfile: txtfile.write(frappe.as_json(site_config)) update_reaction_settings(user_email or default_email, user_password) user_password = None finally: frappe.destroy()
def get_default_listview_filters(doctype): meta = frappe.get_meta(doctype) table_fields = meta.get_table_fields() fields = {} for meta in [meta] + table_fields: if meta.doctype == "DocField": meta = frappe.get_meta(meta.options) path = frappe.get_module_path(frappe.scrub(meta.module), "doctype", frappe.scrub(meta.name), frappe.scrub(meta.name) + ".json") doctype_json = frappe.get_file_json(path) fields[meta.name] = [ f.get("fieldname") for f in doctype_json.get("fields") if f.get("in_standard_filter") ] return fields
def update_translations(lang, translated_data, app, is_file=True): """Update translations from a source and target file for a given language. :param lang: Language code (e.g. `en`). :param untranslated_file: File path with the messages in English. :param translated_file: File path with messages in language to be updated. :param app: Select untranslated strings for a particular app""" clear_cache() full_dict = load_lang(lang, [app]) if full_dict: def restore_newlines(s): return (s.replace("|||||", "\\\n").replace( "| | | | |", "\\\n").replace("||||", "\\n").replace( "| | | |", "\\n").replace("|||", "\n").replace("| | |", "\n")) translation_dict = defaultdict(dict) for k in full_dict: for m in full_dict[k]: translation_dict[k][m] = full_dict[restore_newlines(k)][ restore_newlines(m)] if is_file: new_translations = frappe._dict( frappe.get_file_json(translated_data)) else: new_translations = translated_data for k in new_translations: for m in new_translations[k]: if new_translations[k][m] != "": translation_dict[k][restore_newlines( m)] = restore_newlines(new_translations[k][m]) write_translations_file(app, lang, translation_dict)
def test_sync_field_order(self): from frappe.modules.import_file import get_file_path import os # create test doctype test_doctype = frappe.get_doc({ "doctype": "DocType", "module": "Core", "fields": [{ "label": "Field 1", "fieldname": "field_1", "fieldtype": "Data" }, { "label": "Field 2", "fieldname": "field_2", "fieldtype": "Data" }, { "label": "Field 3", "fieldname": "field_3", "fieldtype": "Data" }, { "label": "Field 4", "fieldname": "field_4", "fieldtype": "Data" }], "permissions": [{ "role": "System Manager", "read": 1 }], "name": "Test Field Order DocType", "__islocal": 1 }) path = get_file_path(test_doctype.module, test_doctype.doctype, test_doctype.name) initial_fields_order = ['field_1', 'field_2', 'field_3', 'field_4'] frappe.delete_doc_if_exists("DocType", "Test Field Order DocType") if os.path.isfile(path): os.remove(path) try: frappe.flags.allow_doctype_export = 1 test_doctype.save() # assert that field_order list is being created with the default order test_doctype_json = frappe.get_file_json(path) self.assertTrue(test_doctype_json.get("field_order")) self.assertEqual(len(test_doctype_json['fields']), len(test_doctype_json['field_order'])) self.assertListEqual( [f['fieldname'] for f in test_doctype_json['fields']], test_doctype_json['field_order']) self.assertListEqual( [f['fieldname'] for f in test_doctype_json['fields']], initial_fields_order) self.assertListEqual(test_doctype_json['field_order'], initial_fields_order) # remove field_order to test reload_doc/sync/migrate is backwards compatible without field_order del test_doctype_json['field_order'] with open(path, 'w+') as txtfile: txtfile.write(frappe.as_json(test_doctype_json)) # assert that field_order is actually removed from the json file test_doctype_json = frappe.get_file_json(path) self.assertFalse(test_doctype_json.get("field_order")) # make sure that migrate/sync is backwards compatible without field_order frappe.reload_doctype(test_doctype.name, force=True) test_doctype.reload() # assert that field_order list is being created with the default order again test_doctype.save() test_doctype_json = frappe.get_file_json(path) self.assertTrue(test_doctype_json.get("field_order")) self.assertEqual(len(test_doctype_json['fields']), len(test_doctype_json['field_order'])) self.assertListEqual( [f['fieldname'] for f in test_doctype_json['fields']], test_doctype_json['field_order']) self.assertListEqual( [f['fieldname'] for f in test_doctype_json['fields']], initial_fields_order) self.assertListEqual(test_doctype_json['field_order'], initial_fields_order) # reorder fields: swap row 1 and 3 test_doctype.fields[0], test_doctype.fields[ 2] = test_doctype.fields[2], test_doctype.fields[0] for i, f in enumerate(test_doctype.fields): f.idx = i + 1 # assert that reordering fields only affects `field_order` rather than `fields` attr test_doctype.save() test_doctype_json = frappe.get_file_json(path) self.assertListEqual( [f['fieldname'] for f in test_doctype_json['fields']], initial_fields_order) self.assertListEqual(test_doctype_json['field_order'], ['field_3', 'field_2', 'field_1', 'field_4']) # reorder `field_order` in the json file: swap row 2 and 4 test_doctype_json['field_order'][1], test_doctype_json[ 'field_order'][3] = test_doctype_json['field_order'][ 3], test_doctype_json['field_order'][1] with open(path, 'w+') as txtfile: txtfile.write(frappe.as_json(test_doctype_json)) # assert that reordering `field_order` from json file is reflected in DocType upon migrate/sync frappe.reload_doctype(test_doctype.name, force=True) test_doctype.reload() self.assertListEqual([f.fieldname for f in test_doctype.fields], ['field_3', 'field_4', 'field_1', 'field_2']) # insert row in the middle and remove first row (field 3) test_doctype.append("fields", { "label": "Field 5", "fieldname": "field_5", "fieldtype": "Data" }) test_doctype.fields[4], test_doctype.fields[ 3] = test_doctype.fields[3], test_doctype.fields[4] test_doctype.fields[3], test_doctype.fields[ 2] = test_doctype.fields[2], test_doctype.fields[3] test_doctype.remove(test_doctype.fields[0]) for i, f in enumerate(test_doctype.fields): f.idx = i + 1 test_doctype.save() test_doctype_json = frappe.get_file_json(path) self.assertListEqual( [f['fieldname'] for f in test_doctype_json['fields']], ['field_1', 'field_2', 'field_4', 'field_5']) self.assertListEqual(test_doctype_json['field_order'], ['field_4', 'field_5', 'field_1', 'field_2']) except: raise finally: frappe.flags.allow_doctype_export = 0
def before_save(self): json_data = frappe.get_file_json(frappe.local.site_path + self.cf_upload_gstr_2b_data) self.cf_return_period = json_data['data']['rtnprd']
def get_meteor_manifest(path): program_file = frappe.get_file_json(path) if (program_file): return program_file.get("manifest") return []
def get_fluorine_server_conf(): path_reactivity = get_path_reactivity() program_conf = os.path.join(path_reactivity, "program.json") if os.path.exists(program_conf): return frappe.get_file_json(program_conf) return None