Target audience: Mayan EDMS and Python users
Used tools: Mayan EDMS and Python
What’s the purpose: Load user’s data in Mayan EDMS
This post will be for mass loading of user’s data in Mayan EDMS. Usually, we enter user’s data one by one, from program’s menu, filling user’s data in user’s entry form. But if you have user’s data of 200-300 users, written in a text/CSV/Excel file, it will be more convenient to enter them at once !
Good – but how to do this? Entering directly in database is not give a desire result – new user can login but nothing from the program can be seen in the browser. The is no information about structure of database, connections between data tables and so on ! Then, what’s remain to do? To enter data of every user, one by one, on condition, that we have a file for all users?
Have to read documentation again – but the are no info about such a problem. But, reading doc again, I find out that program has web API. I try it and it’s worked. But only for one user, of course ! But Mayan EDMS is made by Django/Python – so if I could make a Python script for reading user’s data from text file and add them to database, using web API, then I’ll be able to load all data at once in a database !
First, as web API uses input data in JSON string, our text file, with user’s data must be converted in JSON format – there are many ways for doing this, but I did it using Excel, with some long formulas, which converted Excel columns with user’s data in cells to one other column which contained JSON data – but this will be another post with subject Text/CSV file to JSON !
The Python script proved not so complex. Here is this little utility:
# Python app to read json file and load data DB thгough web API
import requests
import json
from requests.auth import HTTPBasicAuth
# Opening JSON file
f = open('D:/users.json',encoding='utf-8')
# returns JSON object as a dictionary
data = json.load(f)
# iterating through the JSON list
for i in data['users_details']:
res=requests.post(url='http://xxx.xxx.xxx.xxx:8000/api/v4/users/',
auth=('username','password'), data=i).json()
#print on the screen only for checking
print(res['username'],res['last_name'])
f.close()
Here D:/users.json is a json file with user’s data, xxx.xxx.xxx.xxx is an IP address of the server