Feature Request: Session Defaults persist between logins

Hello,

As of now session defaults always reset after relogging in. There’s no way to set a default company for a user permanently except setting permission and restricting the user to that company. This restriction not only leads to some bugs (such as some reports not working, etc) but also restricts the user if the user may want to view reports in other companies. Permanent session defaults would help fix the above limitations.

Thanks

Session Defaults was introduced so that users who have access to multiple companies or departments or locations can navigate smoothly between the multiple options. If you want to permanently setup, then user permissions is the best way to go ahead. Perhaps you can try ignoring permission of Company on specific documents/reports…this will not cause any user permission notifications even if user is restricted to 1 company.

is there any way to change the session defaults programatically ?

i.e. give a link or some sort of option in the dekstop or website to click on a company(a link ) and that company gets set as the default for that session.

2 Likes

hi @jaichavan
while we set the permission to be default and for all doctypes … when we go to sales invoices for eg, it didn’t set on the filter and even if we create a new invoice the default values for the parent company is present and prefilled on the invoice…
using ERPNext: v12.14.0

I think there should be an option to set the default settings on the user page itself or to enable to pop the session defaults directly after the user login and save it’s value to user preferences…:thinking::thinking:

1 Like

You can use Session Defaults setting.

For more details: Session Defaults

Session defaults reset every time and need the user to set it on every login… are there something persistent?

I think, this needs to be there on the user page like @michaeleino said. This will help a lot.

1 Like

Did you ever find a solution? I do agree that the system should remember the session defaults after logout/login…

So this is to everyone who needs session settings to be persistent.

Assuming you have your own app

  • create a new doctype, e.g. “Persistent Session Setting”, with fields User (Link to User), Setting Key (Link to DocType) and Setting Value (Dynamic Link)

  • in your hooks.py, include an app include js like

    app_include_js = ["/assets/supercool/js/persistent_defaults.js"]
    
  • create the above file in public/js and add this code:

      $(document).on('app_ready', function() {
          if (document.referrer.endsWith("/login")) {
              // app ready after login, let's rumble
              console.log("let's rumble");
              // get persistent session settings
              frappe.call({
                  'method': "frappe.client.get_list",
                  'args': {
                      'doctype': "Persistent Session Setting",
                      'filters': {'user': frappe.session.user},
                      'fields': ["setting_key", "setting_value"]
                  },
                  'callback': function(response) {
                      if (response.message) {
                          response.message.forEach(function (setting) {
                              var key = setting.setting_key.toLowerCase().replaceAll(" ", "_");
                              frappe.defaults.set_user_default_local(key, setting.setting_value);
                          });
                      }
                  }
              });
          }
      });
    

Then, for each user create a start-up config. And it will be loaded at each login :wink:

5 Likes

well, your idea is very cool, and inspired me :smiley:
I have managed to use the same concept with some modification

  • used the already mapped user<>company from the employee doc, instead of creating/maintaining another doctype.
$(document).on('app_ready', function() {
      if (document.referrer.endsWith("/")) {
          // app ready after login, let's rumble
          console.log("let's set the session defaults");
          // get persistent session settings
          frappe.call({
              'method': "frappe.client.get_list",
              'args': {
                  'doctype': "Employee",
                  'filters': [[ "Employee","user_id","=",frappe.session.user ]],
                  'fields': ["company"]
              },
              'callback': function(response) {
                //console.log(response.message.length)
                  if (response.message > "0") {
                      response.message.forEach(function (setting) {
                          console.log(setting.company);
                          // var key = setting.Key1.toLowerCase().replaceAll(" ", "_");
                          frappe.defaults.set_user_default_local("company", setting.company);
                      });
                  }
                  else{ // fallback to popup the setup_session_defaults
                      frappe.ui.toolbar.setup_session_defaults();
                  }
              }
          });
      }
  });

however i still have some issues:

  • it really works on reports default filters, but when I click on the UI setting>user defaults i found it is not set.

  • referrer /login will not work when is user coming from /#login so used / instead.
    however it didn’t work if the user click any link to open in a new window :frowning:

Currently I’m trying to set the defaults using the api
/api/method/frappe.core.doctype.session_default_settings.session_default_settings.get_session_default_values

many thanks for the code idea :slight_smile:

well… here is a working workaround for this…

this file to be loaded on by, this to clear the cookie flag on logout:
web_include_js = "/assets/customstyle/js/clear_persistent_defaults.js"

//check if logged out and set the my_session_default_settings=no
if (document.cookie.split(‘;’).some((item) => item.includes(‘my_session_default_settings=yes’ && ‘system_user=no’ && ‘user_id=Guest’))) {
document.cookie = “my_session_default_settings=no;SameSite=Lax”;
console.log(“my_session_default_settings=no”);
}

and this is the logic: (app_include_js = “/assets/js/persistent_defaults.js”)

$(document).on(‘app_ready’, function() {
// $(document).on(‘startup’, function() {

if (!document.cookie.split(‘;’).some((item) => item.includes(‘my_session_default_settings=yes’))) {
// if (document.referrer.endsWith(“/”)) {
console.log(“let’s set the session defaults”);
// get persistent session settings
frappe.call({
‘method’: “frappe.client.get_list”,
‘args’: {
‘doctype’: “Employee”,
‘filters’: [
[“Employee”, “user_id”, “=”, frappe.session.user]
],
‘fields’: [“company”]
},
‘callback’: function(response) {
//console.log(response.message.length)
if (response.message > “0”) {
//foreach here if there is more than 1 seting to be set
response.message.forEach(function(setting) {
console.log(setting.company);
// var key = setting.Key1.toLowerCase().replaceAll(" ", “_”);
// frappe.defaults.set_user_default_local(“company”, setting.company);
frappe.call({
method: ‘frappe.core.doctype.session_default_settings.session_default_settings.set_session_default_values’,
args: {
default_values: {
“company”: setting.company
},
},
callback: function(data) {
if (data.message == “success”) {
frappe.show_alert({
‘message’: __(‘Session Defaults Saved’),
‘indicator’: ‘green’
});
frappe.ui.toolbar.clear_cache();
//set my_session_default_settings=yes
document.cookie = “my_session_default_settings=yes;SameSite=Lax”;
} else {
frappe.show_alert({
‘message’: __(‘An error occurred while setting Session Defaults’),
‘indicator’: ‘red’
});
}
}
});
});
} else { // fallback to popup the setup_session_defaults
frappe.ui.toolbar.setup_session_defaults();
//set my_session_default_settings=yes, will not ask for session defaults again even if user didn’t save it.
document.cookie = “my_session_default_settings=yes;SameSite=Lax”;
}
}
});
} else {
console.log(“Session defaults is exist!”);
}
});

working as expected, and still checking if there are issues!

any code suggestions?

For this, I just disable the on_login hooks

How? and Why ??

Just commented-out “on_login” line from frappe/hooks.py
reason is the simplest way but need to re-apply it whenever upgrade version.