25.10.2017 14:52, Ashesh Vashi wrote:
On Tue, Oct 24, 2017 at 9:11 PM, Alexander Lakhin <exclus...@gmail.com <mailto:exclus...@gmail.com>> wrote:

    24.10.2017 18:11, Ashesh Vashi wrote:
    Looks like - I missed to logout, and test.

    Please find the updated patch.
    Now the error is gone but I get no right locale.
    The issue is with Desktop mode (see "if config.SERVER_MODE is
    False:" in web/pgadmin/__init__.py).

Did you restart the pgAdmin4/reload after changing the language?
Yes, I restarted it.
The issue is with the current_user.
To reproduce it you can perform rm ~/.pgadmin/sessions/* and then restart pgAdmin4/reload the web interface. As I understand, the modules are registered (and get_locales() first called) before user_login is performed in before_request.
So on very first call to get_locale() you can't use the current_user object.
Please look at v4 of the patch.

Best regards,
------
Alexander Lakhin
Postgres Professional: http://www.postgrespro.com
The Russian Postgres Company

diff --git a/web/pgadmin/__init__.py b/web/pgadmin/__init__.py
index 975659a4..e5e19ec5 100644
--- a/web/pgadmin/__init__.py
+++ b/web/pgadmin/__init__.py
@@ -17,7 +17,7 @@ from importlib import import_module
 from flask import Flask, abort, request, current_app, session, url_for
 from flask_babel import Babel, gettext
 from flask_login import user_logged_in, user_logged_out
-from flask_security import Security, SQLAlchemyUserDatastore
+from flask_security import Security, SQLAlchemyUserDatastore, current_user
 from flask_mail import Mail
 from flask_security.utils import login_user
 from werkzeug.datastructures import ImmutableDict
@@ -242,13 +242,17 @@ def create_app(app_name=None):
         language = 'en'
         if config.SERVER_MODE is False:
             # Get the user language preference from the miscellaneous module
-            misc_preference = Preferences.module('miscellaneous', False)
-            if misc_preference:
-                user_languages = misc_preference.preference(
-                    'user_language'
-                )
-                if user_languages:
-                    language = user_languages.get() or language
+            if current_user.is_authenticated:
+                user_id = current_user.id
+            else:
+                user = user_datastore.get_user(config.DESKTOP_USER)
+                if user is not None:
+                    user_id = user.id
+            user_language = Preferences.raw_value(
+                'miscellaneous', 'user_language', None, user_id
+            )
+            if user_language is not None:
+                language = user_language
         else:
             # If language is available in get request then return the same
             # otherwise check the session or cookie
diff --git a/web/pgadmin/utils/preferences.py b/web/pgadmin/utils/preferences.py
index c7127c95..4a6c01b6 100644
--- a/web/pgadmin/utils/preferences.py
+++ b/web/pgadmin/utils/preferences.py
@@ -473,6 +473,41 @@ class Preferences(object):
             options, help_str, category_label
         )
 
+    @staticmethod
+    def raw_value(_module, _preference, _category=None, _user_id=None):
+        # Find the entry for this module in the configuration database.
+        module = ModulePrefTable.query.filter_by(name=_module).first()
+
+        if module is None:
+            return None
+
+        if _category is None:
+            _category = _module
+
+        if _user_id is None:
+            _user_id = getattr(current_user, 'id', None)
+            if _user_id is None:
+                return None
+
+        cat = PrefCategoryTbl.query.filter_by(mid=module.id).filter_by(name=_category).first()
+
+        if cat is None:
+            return None
+
+        pref = PrefTable.query.filter_by(name=_preference).filter_by(cid=cat.id).first()
+
+        if pref is None:
+            return None
+
+        user_pref  = UserPrefTable.query.filter_by(
+            pid=pref.id
+        ).filter_by(uid=_user_id).first()
+
+        if user_pref is not None:
+            return user_pref.value
+
+        return None
+
     @classmethod
     def module(cls, name, create=True):
         """

Reply via email to