Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[babel] Fix, crash with empty LANGUAGES config key #1147

Merged
merged 7 commits into from
Sep 26, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[babel] Fix API crash on non existing config key
  • Loading branch information
dpgaspar committed Sep 26, 2019
commit 6844e204db82bb39ed434bdd0fb9ae1ef024ed91
12 changes: 10 additions & 2 deletions flask_appbuilder/babel/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def __init__(self, appbuilder):
super(BabelManager, self).__init__(appbuilder)
app = appbuilder.get_app
app.config.setdefault("BABEL_DEFAULT_LOCALE", "en")
app.config.setdefault("LANGUAGES", {"en": {"flag": "us", "name": "English"}})
if not app.config.get("LANGUAGES"):
app.config["LANGUAGES"] = {"en": {"flag": "us", "name": "English"}}
appbuilder_parent_dir = os.path.join(
os.path.dirname(os.path.abspath(__file__)), os.pardir
)
Expand Down Expand Up @@ -44,12 +45,19 @@ def register_views(self):
def babel_default_locale(self):
return self.appbuilder.get_app.config["BABEL_DEFAULT_LOCALE"]

@property
def languages(self):
return self.appbuilder.get_app.config["LANGUAGES"]

def get_locale(self):
if has_request_context():
# locale selector for API searches for request args
for arg, value in request.args.items():
if arg == "_l_":
return value
if value in self.languages:
return value
else:
return self.babel_default_locale
locale = session.get("locale")
if locale:
return locale
Expand Down
8 changes: 4 additions & 4 deletions flask_appbuilder/templates/appbuilder/navbar_right.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@
{% if not locale %}
{% set locale = 'en' %}
{% endif %}
{% if languages.keys()|length > 1 %}
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="javascript:void(0)">
<div class="f16"><i class="flag {{languages[locale].get('flag')}}"></i><b class="caret"></b>
</div>
</a>
{% if languages.keys()|length > 1 %}
<ul class="dropdown-menu">
<li class="dropdown">
{% for lang in languages %}
{% if lang != locale %}
<a tabindex="-1" href="{{appbuilder.get_url_for_locale(lang)}}">
<div class="f16"><i class="flag {{languages[lang].get('flag')}}"></i> - {{languages[lang].get('name')}}
</div></a>
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
</li>
</ul>
{% endif %}
</li>
{% endif %}
{% endmacro %}


Expand Down
20 changes: 20 additions & 0 deletions flask_appbuilder/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,26 @@ def tearDown(self):
self.app = None
self.db = None

def test_babel(self):
"""
REST Api: Test babel simple test
"""
client = self.app.test_client()
rv = self._login(client, USERNAME_ADMIN, PASSWORD_ADMIN)
uri = "api/v1/model1api/?_l_=en"
rv = client.get(uri)
self.assertEqual(rv.status_code, 200)

def test_babel_wrong_language(self):
"""
REST Api: Test babel with a wrong languge
"""
client = self.app.test_client()
rv = self._login(client, USERNAME_ADMIN, PASSWORD_ADMIN)
uri = "api/v1/model1api/?_l_=xx"
rv = client.get(uri)
self.assertEqual(rv.status_code, 200)

def test_auth_login(self):
"""
REST Api: Test auth login
Expand Down