From 18f2629eab6134e92839e9fba8ab58898971c13c Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Tue, 8 Oct 2013 18:33:34 +0530 Subject: [PATCH] [minor] add system manager, webnotes.conn.count --- webnotes/__init__.py | 24 ++++++++++++++++-------- webnotes/db.py | 10 ++++++++++ webnotes/profile.py | 17 +++++++++++++++++ webnotes/utils/__init__.py | 6 ++++++ wnf.py | 18 +----------------- 5 files changed, 50 insertions(+), 25 deletions(-) diff --git a/webnotes/__init__.py b/webnotes/__init__.py index f14b6563bb..3bb4ee984a 100644 --- a/webnotes/__init__.py +++ b/webnotes/__init__.py @@ -557,15 +557,23 @@ def get_conf(site): site_config = _dict({}) conf = site_config.update(conf.__dict__) if conf.sites_dir and site: - conf_path = os.path.join(get_site_base_path(sites_dir=conf.sites_dir, hostname=site), 'site_config.json') - if os.path.exists(conf_path): - with open(conf_path, 'r') as f: - site_config.update(json.load(f)) - site_config['site'] = site - return site_config - - else: + out = get_site_config(conf.sites_dir, site) + if not out: raise NotFound() + + site_config.update(out) + site_config['site'] = site + return site_config else: return conf + +def get_site_config(sites_dir, site): + conf_path = get_conf_path(sites_dir, site) + if os.path.exists(conf_path): + with open(conf_path, 'r') as f: + return json.load(f) + +def get_conf_path(sites_dir, site): + return os.path.join(get_site_base_path(sites_dir=sites_dir, + hostname=site), 'site_config.json') \ No newline at end of file diff --git a/webnotes/db.py b/webnotes/db.py index bb73040767..09fa079109 100644 --- a/webnotes/db.py +++ b/webnotes/db.py @@ -467,6 +467,16 @@ class Database: except: return None + def count(self, dt, filters=None): + if filters: + conditions, filters = self.build_conditions(filters) + return webnotes.conn.sql("""select count(*) + from `tab%s` where %s""" % (dt, conditions), filters)[0][0] + else: + return webnotes.conn.sql("""select count(*) + from `tab%s`""" % (dt,))[0][0] + + def get_table_columns(self, doctype): return [r[0] for r in self.sql("DESC `tab%s`" % doctype)] diff --git a/webnotes/profile.py b/webnotes/profile.py index 81848ab988..e4042bd16f 100644 --- a/webnotes/profile.py +++ b/webnotes/profile.py @@ -179,3 +179,20 @@ def add_role(profile, role): "role": role }) profile_wrapper.save() + +def add_system_manager(email, first_name=None, last_name=None): + # add profile + profile = webnotes.new_bean("Profile") + profile.doc.fields.update({ + "name": email, + "email": email, + "enabled": 1, + "first_name": first_name or email, + "last_name": last_name + }) + profile.insert() + + # add roles + roles = webnotes.conn.sql_list("""select name from `tabRole` + where name not in ("Administrator", "Guest", "All")""") + profile.make_controller().add_roles(*roles) \ No newline at end of file diff --git a/webnotes/utils/__init__.py b/webnotes/utils/__init__.py index 02a0cfacb8..fe1c6c1d08 100644 --- a/webnotes/utils/__init__.py +++ b/webnotes/utils/__init__.py @@ -838,6 +838,12 @@ def get_site_base_path(sites_dir=None, hostname=None): def get_site_path(*path): return get_path(base=get_site_base_path(), *path) +def get_files_path(): + return get_site_path(webnotes.conf.files_path) + +def get_backups_path(): + return get_site_path(webnotes.conf.backup_path) + def get_url(uri=None): url = get_request_site_address() if not url or "localhost" in url: diff --git a/wnf.py b/wnf.py index 75eaf1146d..493b8aa4e5 100755 --- a/wnf.py +++ b/wnf.py @@ -219,23 +219,7 @@ def install_fixtures(site=None): @cmd def add_system_manager(email, first_name=None, last_name=None, site=None): webnotes.connect(site=site) - - # add profile - profile = webnotes.new_bean("Profile") - profile.doc.fields.update({ - "name": email, - "email": email, - "enabled": 1, - "first_name": first_name or email, - "last_name": last_name - }) - profile.insert() - - # add roles - roles = webnotes.conn.sql_list("""select name from `tabRole` - where name not in ("Administrator", "Guest", "All")""") - profile.make_controller().add_roles(*roles) - + webnotes.profile.add_system_manager(email, first_name, last_name) webnotes.conn.commit() @cmd