Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 8 additions & 1 deletion flask_appbuilder/babel/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from flask import abort, redirect, session
from flask import abort, redirect, request, session
from flask_babel import refresh

from ..baseviews import BaseView, expose
from ..urltools import Stack


class LocaleView(BaseView):
Expand All @@ -13,6 +14,12 @@ class LocaleView(BaseView):
def index(self, locale):
if locale not in self.appbuilder.bm.languages:
abort(404, description="Locale not supported.")

if request.referrer is not None:
page_history = Stack(session.get("page_history", []))
page_history.push(request.referrer)
session["page_history"] = page_history.to_json()

session["locale"] = locale
refresh()
self.update_redirect()
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The LocaleView.index is already calling update_redirect why updating it again? because we need to update it with the referrer?

Also would it make more sense to add the update_redirect to Superset?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The LocaleView.index is already calling update_redirect why updating it again? because we need to update it with the referrer?

Yes, updating it with the referrer before calling update_redirect would ensure it redirects back to the referrer page.

Also would it make more sense to add the update_redirect to Superset?

Surely you are right:) It is just a bit tough to add update_redirect in every related view, and the same case even with
build-in pages, not only Superset.

For example, with the URL "/login/", that is, login function at subclass of class AuthView, e.g class AuthDBView, it also does not call the update_redirect and would redirect to other page while changing locale.

Expand Down
15 changes: 15 additions & 0 deletions flask_appbuilder/tests/test_mvc.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ def test_babel_languages(self):
rv = client.get("/lang/pt")
self.assertEqual(rv.status_code, 302)

client.get("/")
rv = client.get("/lang/en", follow_redirects=True)
self.assertEqual(rv.status_code, 200)
data = rv.data.decode("utf-8")
self.assertIn('href="/lang/pt"', data)
self.assertIn("<h2><center>Welcome<center></h2>", data)

rv = client.get(
"/lang/pt", follow_redirects=True, headers={"Referer": "/users/list/"}
)
self.assertEqual(rv.status_code, 200)
data = rv.data.decode("utf-8")
self.assertIn('href="/lang/en"', data)
self.assertIn('<h4 class="panel-title">Lista de Utilizadores</h4>', data)


class ListFilterTestCase(BaseMVCTestCase):
def test_list_filter_invalid_object(self):
Expand Down