This commit is contained in:
Rushabh Mehta 2013-09-24 18:38:10 +05:30
commit 49b1d17777
27 changed files with 139 additions and 90 deletions

View file

@ -10,6 +10,7 @@ db_password = '%(db_password)s'
# user attachments stored in
files_path = 'public/files'
public_path = 'public'
# max file attachment size (default 1MB)
max_file_size = 1000000

View file

@ -77,7 +77,7 @@ class DocType:
make_module_and_roles(self.doclist)
from webnotes import conf
if (not webnotes.in_import) and getattr(conf, 'developer_mode', 0):
if (not webnotes.in_import) and conf.get('developer_mode') or 0:
self.export_doc()
self.make_controller_template()

View file

@ -9,6 +9,7 @@ naming for same name files: file.gif, file-1.gif, file-2.gif etc
"""
import webnotes, webnotes.utils, os
from webnotes import conf
class DocType():
def __init__(self, d, dl):
@ -28,7 +29,7 @@ class DocType():
def on_trash(self):
if self.doc.file_name and webnotes.conn.sql("""select count(*) from `tabFile Data`
where file_name=%s""", self.doc.file_name)[0][0]==1:
path = webnotes.utils.get_path("public", "files", self.doc.file_name)
path = webnotes.utils.get_storage_path(conf.files_path, self.doc.file_name)
if os.path.exists(path):
os.remove(path)
@ -38,4 +39,4 @@ class DocType():
"write", self.doc.attached_to_name):
webnotes.msgprint(webnotes._("No permission to write / remove."),
raise_exception=True)

View file

@ -2,7 +2,8 @@
# MIT License. See license.txt
from __future__ import unicode_literals
import webnotes, conf, os
import webnotes, os
from webnotes import conf
import webnotes.utils
from webnotes.utils import get_base_path
from webnotes.modules import get_doc_path
@ -29,7 +30,7 @@ class DocType:
def export_doc(self):
# export
if self.doc.standard == 'Yes' and getattr(conf, 'developer_mode', 0) == 1:
if self.doc.standard == 'Yes' and (conf.get('developer_mode') or 0) == 1:
from webnotes.modules.export_file import export_to_files
export_to_files(record_list=[['Print Format', self.doc.name]],
record_module=self.doc.module)

View file

@ -54,7 +54,7 @@ class DocType:
"""don't allow more than max users if set in conf"""
from webnotes import conf
# check only when enabling a user
if hasattr(conf, 'max_users') and self.doc.enabled and \
if 'max_users' in conf and self.doc.enabled and \
self.doc.name not in ["Administrator", "Guest"] and \
cstr(self.doc.user_type).strip() in ("", "System User"):
active_users = webnotes.conn.sql("""select count(*) from tabProfile

View file

@ -2,8 +2,8 @@
# MIT License. See license.txt
from __future__ import unicode_literals
import webnotes, conf
from webnotes import _
import webnotes
from webnotes import conf, _
class DocType:
def __init__(self, doc, doclist):
@ -30,7 +30,7 @@ class DocType:
def export_doc(self):
# export
if self.doc.is_standard == 'Yes' and getattr(conf, 'developer_mode', 0) == 1:
if self.doc.is_standard == 'Yes' and (conf.get('developer_mode') or 0) == 1:
from webnotes.modules.export_file import export_to_files
export_to_files(record_list=[['Report', self.doc.name]],
record_module=webnotes.conn.get_value("DocType", self.doc.ref_doctype, "module"))

View file

@ -10,7 +10,7 @@ from webnotes import _
@webnotes.whitelist(allow_roles=["System Manager", "Administrator"])
def update_this_app():
from webnotes import conf
if hasattr(conf, "expires_on"):
if "expires_on" in conf:
return _("This feature is only applicable to self hosted instances")
from webnotes.utils import execute_in_shell, cstr, get_base_path

View file

@ -8,6 +8,7 @@ globals attached to webnotes module
from __future__ import unicode_literals
from werkzeug.local import Local
from werkzeug.exceptions import NotFound
import os
import json
@ -32,7 +33,7 @@ class _dict(dict):
return _dict(super(_dict, self).copy())
def __getattr__(self, key):
return webnotes.local.get("key", None)
return local.get("key", None)
def _(msg):
"""translate object in current lang, if exists"""
@ -73,6 +74,9 @@ message_log = local("message_log")
lang = local("lang")
def init(site=None):
if getattr(local, "initialised", None):
return
local.error_log = []
local.message_log = []
local.debug_log = []
@ -80,6 +84,14 @@ def init(site=None):
local.lang = "en"
local.request_method = request.method if request else None
local.conf = get_conf(site)
local.initialised = True
def destroy():
"""closes connection and releases werkzeug local"""
webnotes.conn.close()
from werkzeug.local import release_local
release_local(local)
_memc = None
mute_emails = False
@ -125,7 +137,7 @@ def errprint(msg):
def log(msg):
if not request:
import conf
if getattr(conf, "logging", False):
if conf.get("logging") or False:
print repr(msg)
from utils import cstr
@ -183,8 +195,9 @@ def remove_file(path):
if e.args[0]!=2:
raise e
def connect(db_name=None, password=None):
def connect(db_name=None, password=None, site=None):
import webnotes.db
init(site=site)
local.conn = webnotes.db.Database(user=db_name, password=password)
local.session = _dict({'user':'Administrator'})
local.response = _dict()
@ -203,12 +216,10 @@ logger = None
def get_db_password(db_name):
"""get db password from conf"""
import conf
if hasattr(conf, 'get_db_password'):
if 'get_db_password' in conf:
return conf.get_db_password(db_name)
elif hasattr(conf, 'db_password'):
elif 'db_password' in conf:
return conf.db_password
else:
@ -521,13 +532,21 @@ def get_config():
return _config
def get_conf(site):
# TODO Should be heavily cached!
import conf
from webnotes.utils import get_storage_base_path
conf = _dict(conf.__dict__)
if conf.sites_dir and site:
with open(os.path.join(get_storage_base_path(conf.sites_dir, site), 'site_config.json'), 'r') as f:
site_config = json.load(f)
site_config.update(conf.__dict__)
return _dict(site_config)
conf_path = os.path.join(get_storage_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 = json.load(f)
site_config.update(conf)
site_config['site'] = site
return _dict(site_config)
else:
raise NotFound()
else:
return conf

View file

@ -8,6 +8,7 @@ sys.path.insert(0, 'lib')
from werkzeug.wrappers import Request, Response
from werkzeug.local import LocalManager
from werkzeug.wsgi import SharedDataMiddleware
from werkzeug.exceptions import HTTPException
from webnotes import get_config
import mimetypes
@ -22,22 +23,29 @@ local_manager = LocalManager([webnotes.local])
def application(request):
webnotes.local.request = request
webnotes.init(site=request.host)
webnotes.local.form_dict = webnotes._dict({ k:v[0] if isinstance(v, (list, tuple)) else v \
for k, v in (request.form or request.args).iteritems() })
webnotes.local._response = Response()
try:
webnotes.http_request = webnotes.auth.HTTPRequest()
except webnotes.AuthenticationError, e:
pass
if webnotes.form_dict.cmd:
webnotes.handler.handle()
else:
webnotes.webutils.render(webnotes.request.path[1:])
webnotes.init(site=request.host)
webnotes.local.form_dict = webnotes._dict({ k:v[0] if isinstance(v, (list, tuple)) else v \
for k, v in (request.form or request.args).iteritems() })
webnotes.local._response = Response()
try:
webnotes.http_request = webnotes.auth.HTTPRequest()
except webnotes.AuthenticationError, e:
pass
if webnotes.form_dict.cmd:
webnotes.handler.handle()
else:
webnotes.webutils.render(webnotes.request.path[1:])
if webnotes.conn:
webnotes.conn.close()
except HTTPException, e:
return e
return webnotes._response

View file

@ -71,7 +71,7 @@ def get_bootinfo():
def load_conf_settings(bootinfo):
from webnotes import conf
for key in ['developer_mode']:
if hasattr(conf, key): bootinfo[key] = getattr(conf, key)
if key in conf: bootinfo[key] = conf.get(key)
def add_allowed_pages(bootinfo):
bootinfo.page_info = dict(webnotes.conn.sql("""select distinct parent, modified from `tabPage Role`

View file

@ -84,7 +84,7 @@ class Database:
webnotes.errprint(query % values)
except TypeError:
webnotes.errprint([query, values])
if getattr(conf, "logging", False)==2:
if (conf.get("logging") or False)==2:
webnotes.log("<<<< query")
webnotes.log(query)
webnotes.log("with values:")
@ -97,7 +97,7 @@ class Database:
if debug:
self.explain_query(query)
webnotes.errprint(query)
if getattr(conf, "logging", False)==2:
if (conf.get("logging") or False)==2:
webnotes.log("<<<< query")
webnotes.log(query)
webnotes.log(">>>>")

View file

@ -175,7 +175,8 @@ def print_raw():
def make_logs():
"""make strings for msgprint and errprint"""
import json, conf
import json
from webnotes import conf
from webnotes.utils import cstr
if webnotes.error_log:
# webnotes.response['exc'] = json.dumps("\n".join([cstr(d) for d in webnotes.error_log]))
@ -184,7 +185,7 @@ def make_logs():
if webnotes.message_log:
webnotes.response['_server_messages'] = json.dumps([cstr(d) for d in webnotes.message_log])
if webnotes.debug_log and getattr(conf, "logging", False):
if webnotes.debug_log and conf.get("logging") or False:
webnotes.response['_debug_messages'] = json.dumps(webnotes.debug_log)
def print_zip(response):

View file

@ -7,7 +7,8 @@
from __future__ import unicode_literals
import os, sys, json
import webnotes, conf
import webnotes
from webnotes import conf
import webnotes.db
import getpass
from webnotes.model.db_schema import DbManager
@ -17,15 +18,16 @@ class Installer:
def __init__(self, root_login, root_password=None):
if root_login:
if not root_password:
root_password = getattr(conf, "root_password", None)
root_password = conf.get("root_password") or None
if not root_password:
root_password = getpass.getpass("MySQL root password: ")
self.root_password = root_password
self.conn = webnotes.db.Database(user=root_login, password=root_password)
webnotes.conn=self.conn
webnotes.session= webnotes._dict({'user':'Administrator'})
webnotes.local.conn = self.conn
webnotes.local.session = webnotes._dict({'user':'Administrator'})
self.dbman = DbManager(self.conn)
def import_from_db(self, target, source_path='', password = 'admin', verbose=0):
@ -73,6 +75,8 @@ class Installer:
# update admin password
self.create_auth_table()
self.update_admin_password(password)
self.conn.close()
return target
@ -100,7 +104,7 @@ class Installer:
def update_admin_password(self, password):
from webnotes.auth import _update_password
webnotes.conn.begin()
_update_password("Administrator", getattr(conf, "admin_password", password))
_update_password("Administrator", conf.get("admin_password") or password)
webnotes.conn.commit()
@ -123,8 +127,8 @@ class Installer:
webnotes.conn.begin()
for d in install_docs:
doc = webnotes.doc(fielddata=d)
doc.insert()
bean = webnotes.bean(d)
bean.insert()
webnotes.conn.commit()
def set_all_patches_as_completed(self):

View file

@ -14,7 +14,7 @@ class MClient(memcache.Client):
self.set(self.n(key), val)
def get_value(self, key, builder=None):
if builder and getattr(conf, "auto_cache_clear", False):
if builder and conf.get("auto_cache_clear") or False:
return builder()
val = self.get(self.n(key))

View file

@ -18,10 +18,10 @@ import webnotes
import webnotes.model
import webnotes.model.doc
import webnotes.model.doclist
from webnotes.utils import cint
from webnotes.utils import cint, get_base_path
doctype_cache = webnotes.local('doctype_doctype_cache')
docfield_types = webnotes.local('doctype_doctype_cache')
docfield_types = webnotes.local('doctype_docfield_types')
# doctype_cache = {}
# docfield_types = None
@ -272,7 +272,8 @@ def add_code(doctype, doclist):
def add_embedded_js(doc):
"""embed all require files"""
import re, os, conf
import re, os
from webnotes import conf
# custom script
custom = webnotes.conn.get_value("Custom Script", {"dt": doc.name,
@ -280,7 +281,7 @@ def add_embedded_js(doc):
doc.fields['__js'] = ((doc.fields.get('__js') or '') + '\n' + custom).encode("utf-8")
def _sub(match):
fpath = os.path.join(os.path.dirname(conf.__file__), \
fpath = os.path.join(get_base_path(), \
re.search('["\'][^"\']*["\']', match.group(0)).group(0)[1:-1])
if os.path.exists(fpath):
with open(fpath, 'r') as f:

View file

@ -10,6 +10,7 @@ import webnotes
import os
import conf
from webnotes.modules.import_file import import_file
from webntoes.utils import get_base_path
def sync_all(force=0):
sync_for("lib", force)
@ -17,7 +18,7 @@ def sync_all(force=0):
webnotes.clear_cache()
def sync_for(folder, force=0, sync_everything = False):
return walk_and_sync(os.path.join(os.path.dirname(os.path.abspath(conf.__file__)),
return walk_and_sync(os.path.join(get_base_path(),
folder), force, sync_everything)
def walk_and_sync(start_path, force=0, sync_everything = False):
@ -46,4 +47,4 @@ def walk_and_sync(start_path, force=0, sync_everything = False):
webnotes.conn.commit()
return modules
return modules

View file

@ -5,7 +5,8 @@ from __future__ import unicode_literals
"""
Utilities for using modules
"""
import webnotes, os, conf
import webnotes, os
from webnotes import conf
lower_case_files_for = ['DocType', 'Page', 'Report',
"Workflow", 'Module Def', 'Desktop Item', 'Workflow State', 'Workflow Action']
@ -25,7 +26,7 @@ def get_module_path(module):
"""Returns path of the given module"""
m = scrub(module)
app_path = os.path.dirname(conf.__file__)
app_path = webnotes.utils.get_base_path()
if m in ('core', 'website'):
return os.path.join(app_path, 'lib', m)
@ -49,4 +50,4 @@ def export_doc(doctype, name, module=None):
write_document_file(webnotes.model.doc.get(doctype, name), module)
def get_doctype_module(doctype):
return webnotes.conn.get_value('DocType', doctype, 'module')
return webnotes.conn.get_value('DocType', doctype, 'module')

View file

@ -14,7 +14,8 @@ def insert_test_data(doctype, sort_fn=None):
def get_test_doclist(doctype, name=None):
"""get test doclist, collection of doclists"""
import os, conf, webnotes
import os, webnotes
from webnotes import conf
from webnotes.modules.utils import peval_doclist
from webnotes.modules import scrub

View file

@ -363,7 +363,8 @@ def google_translate(lang, infile, outfile):
"""translate objects using Google API. Add you own API key for translation"""
data = get_all_messages_from_file(infile)
import requests, conf
import requests
from webnotes import conf
old_translations = {}

View file

@ -810,18 +810,26 @@ def filter_strip_join(some_list, sep):
"""given a list, filter None values, strip spaces and join"""
return (cstr(sep)).join((cstr(a).strip() for a in filter(None, some_list)))
def get_path(*path):
def get_path(base=None, *path):
if not base:
base = get_base_path()
import os
return os.path.join(get_base_path(), *path)
return os.path.join(base, *path)
def get_base_path():
import conf
import os
return os.path.dirname(os.path.abspath(conf.__file__))
def get_storage_base_path(sites_dir, hostname):
def get_storage_base_path(sites_dir=None, hostname=None):
import os
if not sites_dir and not hostname:
sites_dir = conf.sites_dir
hostname = conf.site
return os.path.join(sites_dir, hostname)
def get_storage_path(*path):
return get_path(base=get_storage_base_path(), *path)
def get_url(uri=None):
url = get_request_site_address()

View file

@ -17,7 +17,7 @@ from datetime import datetime
#Global constants
verbose = 0
import conf
from webnotes import conf
#-------------------------------------------------------------------------------
class BackupGenerator:
"""
@ -70,8 +70,7 @@ class BackupGenerator:
self.backup_path_db = this_file_path
def zip_files(self):
# TODO use get_storage_base_path
files_path = os.path.join(os.path.dirname(os.path.abspath(conf.__file__)), 'public', 'files')
files_path = webnotes.utils.get_storage_path(conf.files_path)
cmd_string = """tar -cf %s %s""" % (self.backup_path_files, files_path)
err, out = webnotes.utils.execute_in_shell(cmd_string)
@ -185,10 +184,9 @@ backup_path = None
def get_backup_path():
global backup_path
if not backup_path:
import os, conf
import os
# TODO Use get_storage_base_path
backup_path = os.path.join(os.path.dirname(os.path.abspath(conf.__file__)),
'public', 'backups')
backup_path = webnotes.utils.get_storage_path(conf.backup_path)
return backup_path
#-------------------------------------------------------------------------------

View file

@ -2,7 +2,8 @@
# MIT License. See license.txt
from __future__ import unicode_literals
import webnotes, conf
import webnotes
from webnotes import conf
def sendmail_md(recipients, sender=None, msg=None, subject=None):
"""send markdown email"""

View file

@ -27,7 +27,7 @@ def send(recipients=None, sender=None, doctype='Profile', email_field='email',
if hasattr(startup, 'get_monthly_bulk_mail_limit'):
monthly_bulk_mail_limit = startup.get_monthly_bulk_mail_limit()
else:
monthly_bulk_mail_limit = getattr(conf, 'monthly_bulk_mail_limit', 500)
monthly_bulk_mail_limit = conf.get('monthly_bulk_mail_limit') or 500
if this_month + len(recipients) > monthly_bulk_mail_limit:
webnotes.msgprint("""Monthly Bulk Mail Limit (%s) Crossed""" % monthly_bulk_mail_limit,
@ -111,14 +111,15 @@ def unsubscribe():
def flush(from_test=False):
"""flush email queue, every time: called from scheduler"""
import webnotes, conf
import webnotes
from webnotes import conf
from webnotes.utils.email_lib.smtp import SMTPServer, get_email
smptserver = SMTPServer()
auto_commit = not from_test
if webnotes.mute_emails or getattr(conf, "mute_emails", False):
if webnotes.mute_emails or conf.get("mute_emails") or False:
webnotes.msgprint("Emails are muted")
from_test = True

View file

@ -171,11 +171,11 @@ class EMail:
if not self.sender:
self.sender = webnotes.conn.get_value('Email Settings', None,
'auto_email_id') or getattr(conf, 'auto_email_id', None)
'auto_email_id') or conf.get('auto_email_id') or None
if not self.sender:
webnotes.msgprint("""Please specify 'Auto Email Id' \
in Setup > Email Settings""")
if not hasattr(conf, "expires_on"):
if not "expires_on" in conf:
webnotes.msgprint("""Alternatively, \
you can also specify 'auto_email_id' in conf.py""")
raise webnotes.ValidationError
@ -205,7 +205,7 @@ class EMail:
def send(self, as_bulk=False):
"""send the message or add it to Outbox Email"""
if webnotes.mute_emails or getattr(conf, "mute_emails", False):
if webnotes.mute_emails or conf.get("mute_emails") or False:
webnotes.msgprint("Emails are muted")
return
@ -254,11 +254,11 @@ class SMTPServer:
self.password = es.mail_password
self.always_use_login_id_as_sender = es.always_use_login_id_as_sender
else:
self.server = getattr(conf, "mail_server", "")
self.port = getattr(conf, "mail_port", None)
self.use_ssl = cint(getattr(conf, "use_ssl", 0))
self.login = getattr(conf, "mail_login", "")
self.password = getattr(conf, "mail_password", "")
self.server = conf.get("mail_server") or ""
self.port = conf.get("mail_port") or None
self.use_ssl = cint(conf.get("use_ssl") or 0)
self.login = conf.get("mail_login") or ""
self.password = conf.get("mail_password") or ""
@property
def sess(self):

View file

@ -3,9 +3,10 @@
from __future__ import unicode_literals
import webnotes
import os, conf
from webnotes.utils import cstr, get_path, cint
import os
from webnotes.utils import cstr, cint, get_storage_path
from webnotes import _
from webnotes import conf
class MaxFileSizeReachedError(webnotes.ValidationError): pass
@ -64,7 +65,7 @@ def get_uploaded_content():
def save_file(fname, content, dt, dn):
import filecmp
from webnotes.model.code import load_doctype_module
files_path = get_path("public", "files")
files_path = get_storage_path(conf.public_path)
module = load_doctype_module(dt, webnotes.conn.get_value("DocType", dt, "module"))
if hasattr(module, "attachments_folder"):
@ -102,7 +103,7 @@ def save_file(fname, content, dt, dn):
f = webnotes.bean({
"doctype": "File Data",
"file_name": os.path.relpath(os.path.join(files_path, fname), get_path("public")),
"file_name": os.path.relpath(os.path.join(files_path, fname), get_storage_path(conf.public_path)),
"attached_to_doctype": dt,
"attached_to_name": dn,
"file_size": file_size
@ -139,7 +140,7 @@ def scrub_file_name(fname):
return fname
def check_max_file_size(content):
max_file_size = getattr(conf, 'max_file_size', 1000000)
max_file_size = conf.get('max_file_size') or 1000000
file_size = len(content)
if file_size > max_file_size:
@ -183,7 +184,7 @@ def get_file(fname):
# read the file
import os
files_path = get_path("public", "files")
files_path = get_storage_path(conf.files_path)
file_path = os.path.join(files_path, file_name)
if not os.path.exists(file_path):
# check in folders

View file

@ -28,7 +28,7 @@ def render_page(page_name):
page_name = scrub_page_name(page_name)
html = ''
if not (hasattr(conf, 'auto_cache_clear') and conf.auto_cache_clear or 0):
if not ('auto_cache_clear') and conf.auto_cache_clear or 0 in conf:
html = webnotes.cache().get_value("page:" + page_name)
from_cache = True

View file

@ -125,5 +125,5 @@ login.show_forgot_password = function() {
login.set_message = function(message, color) {
wn.msgprint(message);
return;
$('#login_message').html(message).toggle(true);
//$('#login_message').html(message).toggle(true);
}