apache / superset

Apache Superset is a Data Visualization and Data Exploration Platform
https://superset.apache.org/
Apache License 2.0
62.78k stars 13.87k forks source link

Redirect happens whenever user changes language #12768

Open a5net opened 3 years ago

a5net commented 3 years ago

When the user presses on change language a redirect to /users/list happens. I think, in terms of UX, it should remain on the same page as before changing the language.

Expected results

Remain on the same page, but translated into a different language.

Actual results

Redirects to users/list.

Screenshots

gif-gif

How to reproduce the bug

  1. Enable languages
  2. Open any page
  3. Change the language

Environment

(please complete the following information):

Checklist

Make sure to follow these steps before submitting your issue - thank you!

wangweinjcn commented 3 years ago

I have same problem, how to fix it? thank you!

tianhe1986 commented 3 years ago

I encountered the same problem and solved it with a monkey patch. It is effective but not elegant enough. I also submit a PR to Flask-AppBuilder as a improvement.

It is because not all pages call update_redirect which changing the page history stack, turning out it will jump to other page at the top of the page history stack after switching language.

What I did

  1. Add a monkey patch function in class SupersetAppInitializer at superset/app.py.

    def monkey_patch(self) -> None:
        @expose("/<string:locale>")
        def patch_flask_locale_index(self, locale):
            from flask import abort, redirect, session, request
            from flask_babel import refresh
            from flask_appbuilder.urltools import Stack
            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()
            return redirect(self.get_redirect())
    
        from flask_appbuilder.babel.views import LocaleView
        LocaleView.index = patch_flask_locale_index
  2. Call the function in configure_fab function in class SupersetAppInitializer before init appbuilder, that is

    def configure_fab(self) -> None:
        if self.config["SILENCE_FAB"]:
            logging.getLogger("flask_appbuilder").setLevel(logging.ERROR)
    
        custom_sm = self.config["CUSTOM_SECURITY_MANAGER"] or SupersetSecurityManager
        if not issubclass(custom_sm, SupersetSecurityManager):
            raise Exception(
                """Your CUSTOM_SECURITY_MANAGER must now extend SupersetSecurityManager,
                 not FAB's security manager.
                 See [4565] in UPDATING.md"""
            )
    
        self.monkey_patch()
        appbuilder.indexview = SupersetIndexView
        appbuilder.base_template = "superset/base.html"
        appbuilder.security_manager_class = custom_sm
        appbuilder.init_app(self.flask_app, db.session)

That's it.

zuzana-vej commented 2 years ago

Can you please confirm you are still facing this bug / close it not --thanks!

CFM90 commented 2 years ago

This Bug continues to exist in V 1.4.2

rusackas commented 1 year ago

I just tried this on master, and it still exists. I'll assign this to a couple people that might know a way to resolve this, but I think in general, I'll leave this open and in the unfortunate ol' "PRs welcome" status.

superset-user commented 11 months ago

Any update? Bug still exists in version 2.1.0.

rusackas commented 9 months ago

Probably still an issue... I don't think I've seen anyone working on it, strangely.

rusackas commented 5 months ago

This issue is at risk of being closed as stale, though the behavior still exists as of 4.0.1. All the links just point to their relative language URL (e.g. http://localhost:8088/lang/zh_TW) which by default navigates to the Welcome page.

@tianhe1986 would you want to open a PR making a solution along the lines you suggest more official,

@dpgaspar do you know of any better/easier way to keep the user on their current page/URL when changing language?

rusackas commented 4 months ago

This was just reported again on the linked issue. Not so stale anymore :)

aprendizlaura commented 1 month ago

I have the same problem, but when I change the language, it doesn't redirect me to the user section; instead, it takes me to the homepage.

SBIN2010 commented 1 month ago

I suggest this option, it works well for me Add it to config.py:

class SupersetIndexView(IndexView):   
    @expose("/lang/<string:locale>")
    def patch_flask_locale(self, locale):
        from flask import redirect, request, session
        referrer = request.headers.get("Referer")
        session["locale"] = locale
        return redirect(referrer)

FAB_INDEX_VIEW = f"{SupersetIndexView.__module__}.{SupersetIndexView.__name__}"