EducationSoftwareStrategy.com
StrategyCommunity

Knowledge Base

Product

Community

Knowledge Base

TopicsBrowse ArticlesDeveloper Zone

Product

Download SoftwareProduct DocumentationSecurity Hub

Education

Tutorial VideosSolution GalleryEducation courses

Community

GuidelinesGrandmastersEvents
x_social-icon_white.svglinkedin_social-icon_white.svg
Strategy logoCommunity

© Strategy Inc. All Rights Reserved.

LegalTerms of UsePrivacy Policy
  1. Home
  2. Topics

Script that reads user language preferences from Library and deploys them to Web (Command Manager scripts are generated)


Robert Prochowicz

Manager, Sales Engineering • MicroStrategy


With introduction of Library there is a need to synchronize user language preferences between Web and Library interfaces. This can go both ways, but this script concentrates on Library -> Web path. It is using mstrio Python library and Command Manager scripts. CM is being used since REST API work only in Library and can't modify Web related objects and settings.

With introduction of Library there is a need to synchronize user language preferences between Web and Library interfaces. This can go both ways, but this script concentrates on Library -> Web path. It is using mstrio Python library and Command Manager scripts. CM is being used since REST API work only in Library and can't modify Web related objects and settings.
This script does the following:

  • Connects to Strategy
  • Downloads all users
  • Extracts language preferences that was picked by the user in Library interface (users with 'default language' are treated separately). Alternatively, language preferences per user can be uploaded from a text file.
  • Creates ready to use Command Manager commands that can assign language preference in Web interface
  • At the end all the commands are exported as a text file that can be used directly in Command Manager.

Please note - I've added color formatting in the code below (http://hilite.me/ ). Code copied from here might not work due to indentation issues. In that case, use attached file with sample code. Also this CRM is adding ';' after URLs. 


# This script reads user language preferences from Library and deploys them to Web (Command Manager scripts are generated)
# 2023-07-24 Robert Prochowicz
# checked with Strategy 2021 u.10 and mstrio 11.3.10.101

import csv, json
import pandas as pd
import getpass
from mstrio.connection import Connection as mstrConnection
#from mstrio.server import Environment, Project
from mstrio.connection import get_connection
from mstrio.users_and_groups import list_users, User, UserGroup

def get_user_libr_lang(connection, uid):
    url_add=f"/api/users/{uid}/settings"
    url_add+="?fields=language"
    res=connection.get(url=connection.base_url+url_add)
    lang=res.json()['language']['value']
    return lang

def get_proj_default_lang(connection, pid): # returns default langages [metadata, data]
    url_add=f"/api/projects/{pid}/languages"
    res=connection.get(url=connection.base_url+url_add)
    return [res.json()['metadata']['default'], res.json()['data']['defaultLanguage']]

def get_languages(connection, print_flag):
    url_add=f"/api/languages"
    res=connection.get(url=connection.base_url+url_add)
    langs=res.json()['languages']
    if print_flag:
        for l in langs:
            print(l['id'],l['lcid'],l['name'])
    
    return {l['id']: l['lcid'] for l in langs}

base_url = 'http:/server.Strategy.com:8080/StrategyLibrary'
login_mode = 1
project_id = 'B7CA92F04B9FAE8D941C3E9B7E0CD754'
mstr_username= 'administrator'
mstr_password = ''
#mstr_password = getpass.getpass(prompt='Password ')

cm_lang, cm_nolang=[],[]


# ## In case you want to read langauge preferences from Library

# In[ ]:


mstr_connection = mstrConnection(base_url, mstr_username, mstr_password, project_id=project_id,
                                 login_mode=login_mode, verbose=False)

all_users = list_users(connection=mstr_connection)
lang_dict=get_languages(mstr_connection, True)
proj_default_lang=get_proj_default_lang(mstr_connection, project_id)
excluded_users=['Administrator']

for user in all_users:
    user_name, user_lang = user.username , get_user_libr_lang(mstr_connection, user.id)
    if user_lang and user_name not in excluded_users:
        user_lang=lang_dict[user_lang]
        cm=f'APPLY WEBPREFERENCES NAME "Language:"  VALUE "{user_lang}",  NAME "Language:[Metadata]"  VALUE "{user_lang}",   NAME "Language:[Intelligence Server]"  VALUE "{user_lang}"  FOR USER "{user_name}" TO ALLPROJECTS;'
        print(cm)
        cm_lang.append(cm)
    else:
        user_lang="NA"
        cm=f'APPLY WEBPREFERENCES NAME "Language:"  VALUE "{user_lang}",  NAME "Language:[Metadata]"  VALUE "{user_lang}",   NAME "Language:[Intelligence Server]"  VALUE "{user_lang}"  FOR USER "{user_name}" TO ALLPROJECTS;'
        #use lang values from proj_default_lang
        #print("EMPTY             ", cm)
        cm_nolang.append(cm)


# ## In case you have language preferences in a CSV file

# In[ ]:


# Your user_lang.csv looks like this:
# johnsmith, 1033
# briankelt, 1033

with open("user_lang.csv", encoding="utf8", newline='') as f:
    user_list = [x.strip().split(", ") for x in f if x]

for user in user_list:
    cm=f'APPLY WEBPREFERENCES NAME "Language:"  VALUE "{user[1]}",  NAME "Language:[Metadata]"  VALUE "{user[1]}",   NAME "Language:[Intelligence Server]"  VALUE "{user[1]}"  FOR USER "{user[0]}" TO ALLPROJECTS;'
    print(cm)
    cm_lang.append(cm)


# ## Export CM commands to text files

# In[ ]:


df = pd.DataFrame(cm_lang)
df.to_csv(f"cm_lang.csv", index=False, encoding='utf-8', sep="#", 
          quoting=csv.QUOTE_NONE, escapechar="\\", header=False)

df = pd.DataFrame(cm_nolang)
df.to_csv(f"cm_nolang.csv", index=False, encoding='utf-8', sep="#", 
          quoting=csv.QUOTE_NONE, escapechar="\\", header=False)


Comment

0 comments

Details

Example

Published:

October 20, 2023

Last Updated:

October 20, 2023