Files
ThingsBoard/Code Snippets/addNewUsers.ipynb
2024-07-31 14:02:59 -05:00

198 lines
6.5 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"from tb_rest_client.rest_client_pe import *\n",
"from tb_rest_client.rest import ApiException\n",
"from tb_rest_client.api_client import *\n",
"import re, ast, json\n",
"from uuid import uuid4"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"url = \"https://www.enxlekkocloud.com\"\n",
"username = \"nico.a.melone@gmail.com\"\n",
"password = \"9EE#mqb*b6bXV9hJrPYGm&w3q5Y@3acumvvb5isQ\"\n",
"userIdToCopy = \"\"\n",
"entity_group_id=\"616d62f0-3300-11ef-9c57-29fbfd438c8b\"\n",
"default_dashboard = \"c157d8a0-32f9-11ef-9c57-29fbfd438c8b\""
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"data = [\n",
" \"Michael Montgomery - mmontgomery@apollopetro.com\",\n",
" \"Jerry Pourciau - jpourciau@apollopetro.com\",\n",
" \"Dimitri Menutis - dmenutis@apollopetro.com\",\n",
" \"Chris Jean - cjean@apollopetro.com\",\n",
" \"Josh Spence - jspence@apollopetro.com\"\n",
" ]"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"def checkUserExists(userEmail, rest_client):\n",
" resp = rest_client.get_user_users(page_size=100, page=0,text_search=userEmail)\n",
" resp = resp.to_dict()\n",
" if resp[\"total_elements\"] > 0:\n",
" return True\n",
" return False"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"def copyUser(userIdToCopy, rest_client):\n",
" resp = rest_client.get_user_by_id(userIdToCopy)\n",
" resp = resp.to_dict()\n",
" del resp[\"id\"]\n",
" del resp[\"tenant_id\"]\n",
" del resp[\"created_time\"]\n",
" return resp\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"def process_data(data,entity_group_id,default_dashboard):\n",
" result = []\n",
" for item in data:\n",
" parts = item.split(' - ')\n",
" first_last_name = parts[0].split()\n",
" first_name = first_last_name[0]\n",
" last_name = ' '.join(first_last_name[1:])\n",
" email = parts[1]\n",
" phone = ''\n",
" if len(parts) > 2:\n",
" phone = '+' + re.sub(r'\\D', '', parts[2])\n",
" \n",
" owner_id = {\n",
" \"id\": entity_group_id,\n",
" \"entity_type\": \"CUSTOMER\"\n",
" }\n",
" if default_dashboard:\n",
" additionalInfo = {\n",
" \"description\": \"\",\n",
" \"defaultDashboardId\": default_dashboard,\n",
" \"defaultDashboardFullscreen\": False,\n",
" \"homeDashboardId\": default_dashboard,\n",
" \"homeDashboardHideToolbar\": False,\n",
" \"userCredentialsEnabled\": True\n",
" }\n",
" else:\n",
" additionalInfo = {}\n",
" \n",
" result.append({\n",
" \"email\": email,\n",
" \"authority\": \"CUSTOMER_USER\",\n",
" \"firstName\": first_name,\n",
" \"lastName\": last_name,\n",
" \"phone\": phone,\n",
" \"additionalInfo\": additionalInfo,\n",
" \"ownerId\": owner_id\n",
" })\n",
" \n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"filtered_users = process_data(data, entity_group_id=entity_group_id,default_dashboard=default_dashboard)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"with RestClientPE(base_url=url) as rest_client:\n",
" try:\n",
" rest_client.login(username=username, password=password)\n",
" if userIdToCopy:\n",
" templateUser = copyUser(userIdToCopy=userIdToCopy, rest_client=rest_client)\n",
" templateUser[\"additionalInfo\"] = ast.literal_eval(templateUser['additional_info'].replace(\"'\", '\"'))\n",
" del templateUser[\"additional_info\"]\n",
" else:\n",
" templateUser = {\n",
" \"email\": \"user@example.com\",\n",
" \"authority\": \"CUSTOMER_USER\",\n",
" \"firstName\": \"John\",\n",
" \"lastName\": \"Doe\",\n",
" \"phone\": \"38012345123\",\n",
" \"additionalInfo\": {},\n",
" \"ownerId\": {\n",
" \"id\": \"efe3a0d0-bb6b-11ec-9326-ad8278896f52\",\n",
" \"entityType\": \"CUSTOMER\"\n",
" }\n",
" }\n",
" for user in filtered_users:\n",
" if not checkUserExists(user[\"email\"], rest_client=rest_client):\n",
" \n",
" \"\"\"\n",
" templateUser[\"email\"] = user[\"email\"]\n",
" templateUser[\"name\"] = user[\"email\"]\n",
" templateUser[\"firstName\"] = user[\"information\"][\"first\"]\n",
" templateUser[\"lastName\"] = user[\"information\"][\"last\"]\n",
" if user[\"phone\"]:\n",
" templateUser[\"phone\"] = \"+1\" + \"\".join(user[\"phone\"].split(\"-\"))\n",
" else:\n",
" templateUser[\"phone\"] = \"\"\n",
" \"\"\"\n",
" #print(json.dumps(user, indent=4))\n",
" rest_client.save_user(send_activation_mail=True, body=user, entity_group_id=entity_group_id)\n",
" except ApiException as e:\n",
" print(e)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "thingsboard",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}