From 218817753a2c0f846689467ea2bb67f4ab24e381 Mon Sep 17 00:00:00 2001 From: mdipierro Date: Fri, 26 Feb 2016 14:20:18 -0600 Subject: [PATCH] myconf.take, myconf.get --- applications/welcome/models/db.py | 20 ++++++++++++-------- applications/welcome/models/menu.py | 8 ++++---- applications/welcome/private/appconfig.ini | 17 ++++++++++++++--- gluon/contrib/appconfig.py | 21 +++++++++++++++++++++ gluon/tools.py | 11 +++++++---- 5 files changed, 58 insertions(+), 19 deletions(-) diff --git a/applications/welcome/models/db.py b/applications/welcome/models/db.py index 27dcdf14..075e7aca 100644 --- a/applications/welcome/models/db.py +++ b/applications/welcome/models/db.py @@ -14,10 +14,12 @@ from gluon.contrib.appconfig import AppConfig ## once in production, remove reload=True to gain full speed myconf = AppConfig(reload=True) - if not request.env.web2py_runtime_gae: ## if NOT running on Google App Engine use SQLite or other DB - db = DAL(myconf.take('db.uri'), pool_size=myconf.take('db.pool_size', cast=int), check_reserved=['all']) + db = DAL(myconf.get('db.uri'), + pool_size = myconf.get('db.pool_size'), + migrate_enabled = myconf.get('db.migrate'), + check_reserved = ['all']) else: ## connect to Google BigTable (optional 'google:datastore://namespace') db = DAL('google:datastore+ndb') @@ -32,8 +34,8 @@ else: ## none otherwise. a pattern can be 'controller/function.extension' response.generic_patterns = ['*'] if request.is_local else [] ## choose a style for forms -response.formstyle = myconf.take('forms.formstyle') # or 'bootstrap3_stacked' or 'bootstrap2' or other -response.form_label_separator = myconf.take('forms.separator') +response.formstyle = myconf.get('forms.formstyle') # or 'bootstrap3_stacked' or 'bootstrap2' or other +response.form_label_separator = myconf.get('forms.separator') ## (optional) optimize handling of static files @@ -53,7 +55,7 @@ response.form_label_separator = myconf.take('forms.separator') from gluon.tools import Auth, Service, PluginManager -auth = Auth(db) +auth = Auth(db, host=myconf.get('host.name')) service = Service() plugins = PluginManager() @@ -62,9 +64,11 @@ auth.define_tables(username=False, signature=False) ## configure email mail = auth.settings.mailer -mail.settings.server = 'logging' if request.is_local else myconf.take('smtp.server') -mail.settings.sender = myconf.take('smtp.sender') -mail.settings.login = myconf.take('smtp.login') +mail.settings.server = 'logging' if request.is_local else myconf.get('smtp.server') +mail.settings.sender = myconf.get('smtp.sender') +mail.settings.login = myconf.get('smtp.login') +mail.settings.tls = myconf.get('smtp.tls') +mail.settings.ssl = myconf.get('smtp.ssl') ## configure auth policy auth.settings.registration_requires_verification = False diff --git a/applications/welcome/models/menu.py b/applications/welcome/models/menu.py index b52cbcdf..6e5c521b 100644 --- a/applications/welcome/models/menu.py +++ b/applications/welcome/models/menu.py @@ -12,10 +12,10 @@ response.title = request.application.replace('_',' ').title() response.subtitle = '' ## read more at http://dev.w3.org/html5/markup/meta.name.html -response.meta.author = 'Your Name ' -response.meta.description = 'a cool new app' -response.meta.keywords = 'web2py, python, framework' -response.meta.generator = 'Web2py Web Framework' +response.meta.author = myconf.get('app.author') +response.meta.description = myconf.get('app.description') +response.meta.keywords = myconf.get('app.keywords') +response.meta.generator = myconf.get('app.generator') ## your http://google.com/analytics id response.google_analytics_id = None diff --git a/applications/welcome/private/appconfig.ini b/applications/welcome/private/appconfig.ini index f45efbfc..bf7412b9 100644 --- a/applications/welcome/private/appconfig.ini +++ b/applications/welcome/private/appconfig.ini @@ -1,17 +1,28 @@ ; App configuration +[app] +name = Welcome +author = Your Name +description = a cool new app +keywords = web2py, python, framework +generator = Web2py Web Framework + +; Host configuration +[host] +name = localhost ; db configuration [db] uri = sqlite://storage.sqlite -migrate = 1 -pool_size = 1 +migrate = true +pool_size = 10 ; ignored for sqlite ; smtp address and credentials [smtp] server = smtp.gmail.com:587 sender = you@gmail.com login = username:password - +tls = true +ssl = true ; form styling [forms] diff --git a/gluon/contrib/appconfig.py b/gluon/contrib/appconfig.py index c9768183..5bbd4829 100644 --- a/gluon/contrib/appconfig.py +++ b/gluon/contrib/appconfig.py @@ -58,6 +58,27 @@ class AppConfigDict(dict): dict.__init__(self, *args, **kwargs) self.int_cache = {} + def get(self, path, default=None): + try: + value = self.take(path).strip() + if value.lower() in ('none','null',''): + return None + elif value.lower() == 'true': + return True + elif value.lower() == 'false': + return False + elif value.isdigit() or (value[0]=='-' and value[1:].isdigit()): + return int(value) + elif ', ' in value: + return value.split(', ') + else: + try: + return float(value) + except: + return value + except: + return default + def take(self, path, cast=None): parts = path.split('.') if path in self.int_cache: diff --git a/gluon/tools.py b/gluon/tools.py index 63281234..4a608a05 100644 --- a/gluon/tools.py +++ b/gluon/tools.py @@ -1710,8 +1710,9 @@ class Auth(object): args = [] if vars is None: vars = {} + host = scheme and self.settings.host return URL(c=self.settings.controller, - f=f, args=args, vars=vars, scheme=scheme) + f=f, args=args, vars=vars, scheme=scheme, host=host) def here(self): return URL(args=current.request.args, vars=current.request.get_vars) @@ -1720,7 +1721,7 @@ class Auth(object): hmac_key=None, controller='default', function='user', cas_provider=None, signature=True, secure=False, csrf_prevention=True, propagate_extension=None, - url_index=None, jwt=None): + url_index=None, jwt=None, host=None): ## next two lines for backward compatibility if not db and environment and isinstance(environment, DAL): @@ -1763,9 +1764,10 @@ class Auth(object): # ## what happens after registration? settings = self.settings = Settings() - settings.update(Auth.default_settings) + settings.update(Auth.default_settings) + host = host or request.env.http_host, settings.update( - cas_domains=[request.env.http_host], + cas_domains=[host], enable_tokens=False, cas_provider=cas_provider, cas_actions=dict(login='login', @@ -1815,6 +1817,7 @@ class Auth(object): label_separator=current.response.form_label_separator, two_factor_methods = [], two_factor_onvalidation = [], + host = host, ) settings.lock_keys = True # ## these are messages that can be customized