246 lines
9.3 KiB
Plaintext
246 lines
9.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 = \"e23d1840-7e8c-11ee-b973-b3cdc1f2a19a\"\n",
|
|
"entity_group_id=\"4d43b400-b25d-11ef-861c-8dbe77c636e1\"\n",
|
|
"default_dashboard = \"b10eeab0-b274-11ef-861c-8dbe77c636e1\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"data = [\n",
|
|
" \"Ben Brewster - ben@rrigwater.com - 325-260-3330\",\n",
|
|
" \"Mark Vandresar - mark@rrigenergy.com - 814-462-8899\",\n",
|
|
" \"Zack Roybal - zack@rrigenergy.com - 432-813-5307\",\n",
|
|
" \"Corey Leedy - corey@rrigenergy.com - 325-864-3272\",\n",
|
|
" \"Parker Handlin - parker@rrigenergy.com - 817-201-1558\",\n",
|
|
" \"Chris Johnson - chris@rrigenergy.com - 601-831-0401\",\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": [
|
|
{
|
|
"ename": "AttributeError",
|
|
"evalue": "'dict' object has no attribute 'replace'",
|
|
"output_type": "error",
|
|
"traceback": [
|
|
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
|
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
|
|
"Input \u001b[0;32mIn [8]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m userIdToCopy:\n\u001b[1;32m 5\u001b[0m templateUser \u001b[38;5;241m=\u001b[39m copyUser(userIdToCopy\u001b[38;5;241m=\u001b[39muserIdToCopy, rest_client\u001b[38;5;241m=\u001b[39mrest_client)\n\u001b[0;32m----> 6\u001b[0m templateUser[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124madditionalInfo\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m ast\u001b[38;5;241m.\u001b[39mliteral_eval(\u001b[43mtemplateUser\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43madditional_info\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m]\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mreplace\u001b[49m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m'\u001b[39m))\n\u001b[1;32m 7\u001b[0m \u001b[38;5;28;01mdel\u001b[39;00m templateUser[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124madditional_info\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n\u001b[1;32m 8\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n",
|
|
"\u001b[0;31mAttributeError\u001b[0m: 'dict' object has no attribute 'replace'"
|
|
]
|
|
}
|
|
],
|
|
"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)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 11,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{'customer_id': {'id': '4d4058a0-b25d-11ef-861c-8dbe77c636e1',\n",
|
|
" 'entity_type': 'CUSTOMER'},\n",
|
|
" 'email': 'n_melone@hotmail.com',\n",
|
|
" 'authority': 'CUSTOMER_USER',\n",
|
|
" 'first_name': 'N',\n",
|
|
" 'last_name': 'Melone',\n",
|
|
" 'phone': '+15732019537',\n",
|
|
" 'name': 'n_melone@hotmail.com',\n",
|
|
" 'additional_info': {'description': '',\n",
|
|
" 'defaultDashboardId': 'b10eeab0-b274-11ef-861c-8dbe77c636e1',\n",
|
|
" 'defaultDashboardFullscreen': False,\n",
|
|
" 'homeDashboardId': 'b10eeab0-b274-11ef-861c-8dbe77c636e1',\n",
|
|
" 'homeDashboardHideToolbar': False,\n",
|
|
" 'userCredentialsEnabled': True,\n",
|
|
" 'failedLoginAttempts': 2,\n",
|
|
" 'lastLoginTs': 1706115931097}}"
|
|
]
|
|
},
|
|
"execution_count": 11,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"templateUser"
|
|
]
|
|
}
|
|
],
|
|
"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
|
|
}
|