Target audience: Mayan EDMS and Python users
Used tools: Mayan EDMS and Python
What’s the purpose: Load user locale profile details in Mayan EDMS
This post will about configuration of ‘user locale profile details’ in Mayan EDMS In one of my previous post about Mayan EDMS I explained how to add users in ‘mass’ way – using Rest API and json file for user details.
But there is one other step to make for finishing user configuration – to edit ‘user locale profile details’ – this is choosing the language and time zone for every one user ! Normally this must be done by every user, when he logs for the first time – but if you have 100-200 users, you have to explain to every one what and how he must do.
So, more easier is to do this task yourself . But in database tables there is nothing about language and time zone of user’s profiles.
So, I have to remember my old motto : If there is no way, there must be other way !!!
And then I remember about selenium and web scraping – selenium is designed as web user interface automation tool mainly for testing – but we will forget for testing and will use it as automation tool for looping login is Mayan EDMS and config user locale profile details
in hand – info about username and password of every user will be read from array – of course, instead of array we can do this by reading text file with user data too.
And here is the code for this in Python :
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
## DEPRECATION in >= Selenium 4 !!!
#driver = webdriver.Firefox(executable_path=r'pathTo/geckodriver.exe')
s = Service('pathTo/geckodriver.exe')
driver = webdriver.Firefox(service=s)
driver.get('http://aaa.xxx.yyy.zzz:8000')
OPs = ["x00001","x00002","x00003"]
for OP in OPs:
print(OP)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='id_username']"))).send_keys(OP)
#password = driver.find_element(By.NAME,"id_password")
password = wait.until(EC.presence_of_element_located((By.XPATH, "//input[@id='id_password']")))
password.send_keys('your_password')
btnSbm = driver.find_element(By.NAME,'submit')
btnSbm.click()
# wait up to 10 secs for the url to change or else `TimeOutException` is raised.
wait = WebDriverWait(driver, 10)
driver.get('http://aaa.xxx.yyy.zzz:8000/locales/user/locale/edit/')
And this a variant with using JSON file with username data :
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import json
# Opening JSON file
f = open('pathTo/users11.json',encoding='utf-8')
# returns JSON object as a dictionary
data = json.load(f)
s = Service('pathTo/geckodriver.exe')
driver = webdriver.Firefox(service=s)
driver.get('http://aaa.xxx.yyy.zzz:8000')
for i in data['users_details']:
print(i['username'])
wait = WebDriverWait(driver, 10)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='id_username']"))).send_keys(i['username'])
#another way to find element and to send value
password = wait.until(EC.presence_of_element_located((By.XPATH, "//input[@id='id_password']")))
password.send_keys('your_password')
btnSbm = driver.find_element(By.NAME,'submit')
btnSbm.click()
# wait up to 10 secs for the url to change or else `TimeOutException` is raised.
wait = WebDriverWait(driver, 10)
driver.get('http://aaa.xxx.yyy.zzz:8000/locales/user/locale/edit/')