195 lines
6.3 KiB
Plaintext
195 lines
6.3 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"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": 2,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"url = \"https://hp.henrypump.cloud\"\n",
|
|
"username = \"nmelone@henry-pump.com\"\n",
|
|
"password = \"gzU6$26v42mU%3jDzTJf\"\n",
|
|
"userIdToCopy = \"\"\n",
|
|
"entity_group_id=\"b7a04da0-54d8-11ef-8d66-9bbb0351be2a\"\n",
|
|
"default_dashboard = \"a8ce80b0-54ee-11ef-aa15-a127638e3a77\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"data = [\n",
|
|
" \"Taylor Schafer - taylor@aermotorwindmill.com - 3257167602\",\n",
|
|
" \"Conner Scrivner - conner@aermotorwindmill.com - 4329349131\"\n",
|
|
" ]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"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": 5,
|
|
"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": 6,
|
|
"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 = '+1' + 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": 7,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"filtered_users = process_data(data, entity_group_id=entity_group_id,default_dashboard=default_dashboard)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"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
|
|
}
|