From e15dd6b2c06e34c66434a8862c1f014185ea2a62 Mon Sep 17 00:00:00 2001 From: TonySeek Date: Sun, 3 Nov 2013 17:53:49 +0800 Subject: [PATCH 1/3] Add support for plain text config. --- flask_oauthlib/client.py | 5 +++++ tests/test_client.py | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/flask_oauthlib/client.py b/flask_oauthlib/client.py index fc21d0c4..757cdda8 100644 --- a/flask_oauthlib/client.py +++ b/flask_oauthlib/client.py @@ -297,6 +297,11 @@ def _get_property(self, key, default=False): return default return attr app = self.oauth.app or current_app + # works with plain text config + config_key = "%s_%s" % (self.app_key, key.upper()) + if config_key in app.config: + return app.config[config_key] + # works with dict config config = app.config[self.app_key] if default is not False: return config.get(key, default) diff --git a/tests/test_client.py b/tests/test_client.py index 1c8468eb..db7e820f 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -114,6 +114,25 @@ def test_lazy_load(self): assert twitter.authorize_url == 'auth url' assert twitter.content_type is None + def test_lazy_load_with_plain_text_config(self): + oauth = OAuth() + twitter = oauth.remote_app('twitter', app_key='TWITTER') + + app = Flask(__name__) + app.config['TWITTER_CONSUMER_KEY'] = 'twitter key' + app.config['TWITTER_CONSUMER_SECRET'] = 'twitter secret' + app.config['TWITTER_REQUEST_TOKEN_URL'] = 'request url' + app.config['TWITTER_ACCESS_TOKEN_URL'] = 'token url' + app.config['TWITTER_AUTHORIZE_URL'] = 'auth url' + + oauth.init_app(app) + + assert twitter.consumer_key == 'twitter key' + assert twitter.consumer_secret == 'twitter secret' + assert twitter.request_token_url == 'request url' + assert twitter.access_token_url == 'token url' + assert twitter.authorize_url == 'auth url' + @patch(http_urlopen) def test_http_request(self, urlopen): urlopen.return_value = Response( From 76a1e07d4e20e576b1e82f99f04efc553a189016 Mon Sep 17 00:00:00 2001 From: TonySeek Date: Sun, 3 Nov 2013 17:59:43 +0800 Subject: [PATCH 2/3] add docs description for plain text config. --- docs/client.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/client.rst b/docs/client.rst index c1d1bfed..50f27bab 100644 --- a/docs/client.rst +++ b/docs/client.rst @@ -65,6 +65,11 @@ Flask config by the key ``TWITTER``, the configuration looks like:: oauth.init_app(app) +Or looks like that:: + + app.config['TWITTER_CONSUMER_KEY'] = 'a random string key' + app.config['TWITTER_CONSUMER_SECRET'] = 'a random string secret' + Twitter can get consumer key and secret from the Flask instance now. You can put all the configuration in ``app.config`` if you like, which From 36771a90f420cd324ea45c58814d3f4372f4fc23 Mon Sep 17 00:00:00 2001 From: TonySeek Date: Sun, 3 Nov 2013 18:35:40 +0800 Subject: [PATCH 3/3] resolve the missing default key problem. --- flask_oauthlib/client.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/flask_oauthlib/client.py b/flask_oauthlib/client.py index 757cdda8..6171228b 100644 --- a/flask_oauthlib/client.py +++ b/flask_oauthlib/client.py @@ -297,15 +297,18 @@ def _get_property(self, key, default=False): return default return attr app = self.oauth.app or current_app - # works with plain text config - config_key = "%s_%s" % (self.app_key, key.upper()) - if config_key in app.config: + if self.app_key in app.config: + # works with dict config + config = app.config[self.app_key] + if default is not False: + return config.get(key, default) + return config[key] + else: + # works with plain text config + config_key = "%s_%s" % (self.app_key, key.upper()) + if default is not False: + return app.config.get(config_key, default) return app.config[config_key] - # works with dict config - config = app.config[self.app_key] - if default is not False: - return config.get(key, default) - return config[key] def make_client(self, token=None): # request_token_url is for oauth1