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

Redirect happens whenever user changes language #12768

Open
3 tasks done
a5net opened this issue Jan 26, 2021 · 12 comments · May be fixed by #31692
Open
3 tasks done

Redirect happens whenever user changes language #12768

a5net opened this issue Jan 26, 2021 · 12 comments · May be fixed by #31692
Labels
#bug:regression Bugs that are identified as regessions good first issue Good first issues for new contributors validation:validated A committer has validated / submitted the issue or it was reported by multiple users

Comments

@a5net
Copy link
Contributor

a5net commented Jan 26, 2021

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):

  • superset version: 0.999.0dev
  • python version: 3.7.4
  • node.js version: v14.11.0

Checklist

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

  • I have checked the superset logs for python stacktraces and included it here as text if there are any.
  • I have reproduced the issue with at least the latest released version of superset.
  • I have checked the issue tracker for the same issue and I haven't found one similar.
@a5net a5net added the #bug Bug report label Jan 26, 2021
@junlincc junlincc added #bug:regression Bugs that are identified as regessions and removed #bug Bug report labels Jan 26, 2021
@rusackas rusackas added the good first issue Good first issues for new contributors label Mar 5, 2021
@wangweinjcn
Copy link

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

@tianhe1986
Copy link
Contributor

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
  1. 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 zuzana-vej added the validation:required A committer should validate the issue label Mar 16, 2022
@zuzana-vej
Copy link
Contributor

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

@CFM90
Copy link

CFM90 commented Apr 13, 2022

This Bug continues to exist in V 1.4.2

@rusackas
Copy link
Member

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.

@rusackas rusackas added validation:validated A committer has validated / submitted the issue or it was reported by multiple users and removed validation:required A committer should validate the issue labels Jan 26, 2023
@superset-user
Copy link

Any update? Bug still exists in version 2.1.0.

@rusackas
Copy link
Member

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

@rusackas
Copy link
Member

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
Copy link
Member

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

@aprendizlaura
Copy link

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
Copy link

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__}"

@pomegranited
Copy link
Contributor

Hi @rusackas -- I've submitted a PR for this issue using @SBIN2010's suggested workaround. I tried a few ways of doing this, but that seemed to be the best available. Totally open to other suggestions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
#bug:regression Bugs that are identified as regessions good first issue Good first issues for new contributors validation:validated A committer has validated / submitted the issue or it was reported by multiple users
Projects
None yet
Development

Successfully merging a pull request may close this issue.