Skip to content

Commit

Permalink
Merge pull request #3 from Esri/update_attach_keywords_update_05_25_21
Browse files Browse the repository at this point in the history
Updated directory cleanup for update attachment keywords script.
  • Loading branch information
Zachary-Sutherby authored May 25, 2021
2 parents 61547ee + 3d94f72 commit a338c5b
Showing 1 changed file with 65 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,21 +136,21 @@
"metadata": {},
"outputs": [],
"source": [
"def update_attachment(url, token, oid, attachment, attachID, keyword):\n",
" att_url = '{}/{}/updateAttachment'.format(url, oid)\n",
" start, extension = arcpy.os.path.splitext(attachment)\n",
"\n",
" jpg_list = ['.jpg', '.jpeg']\n",
" png_list = ['.png']\n",
" if extension in jpg_list:\n",
" files = {'attachment': (os.path.basename(attachment), open(attachment, 'rb'), 'image/jpeg')}\n",
" elif extension in png_list:\n",
" files = {'attachment': (os.path.basename(attachment), open(attachment, 'rb'), 'image/png')}\n",
" else:\n",
" files = {'attachment': (os.path.basename(attachment), open(attachment, 'rb'), 'application/octect-stream')}\n",
"\n",
" params = {'token': token,'f': 'json', 'attachmentId': attachID, 'keywords': keyword}\n",
" r = requests.post(att_url, params, files=files, verify=False)\n",
"def update_attachment(url, token, oid, attachment, attachID, keyword):\r\n",
" att_url = '{}/{}/updateAttachment'.format(url, oid)\r\n",
" start, extension = arcpy.os.path.splitext(attachment)\r\n",
"\r\n",
" jpg_list = ['.jpg', '.jpeg']\r\n",
" png_list = ['.png']\r\n",
" if extension in jpg_list:\r\n",
" files = {'attachment': (os.path.basename(attachment), open(attachment, 'rb'), 'image/jpeg')}\r\n",
" elif extension in png_list:\r\n",
" files = {'attachment': (os.path.basename(attachment), open(attachment, 'rb'), 'image/png')}\r\n",
" else:\r\n",
" files = {'attachment': (os.path.basename(attachment), open(attachment, 'rb'), 'application/octect-stream')}\r\n",
"\r\n",
" params = {'token': token,'f': 'json', 'attachmentId': attachID, 'keywords': keyword}\r\n",
" r = requests.post(att_url, params, files=files)\r\n",
" return r.json()"
]
},
Expand Down Expand Up @@ -240,55 +240,55 @@
}
],
"source": [
"def update_attachments():\n",
" with tempfile.TemporaryDirectory() as tmp:\n",
" tmp_path = tmp\n",
" for i in range(len(item_object.layers)):\n",
" layers = []\n",
" feature_layer = item_object.layers[i]\n",
" layers.append(feature_layer)\n",
" tables = item_object.tables[i]\n",
" layers.append(tables)\n",
" \n",
" for layer in layers: \n",
" url = layer.url\n",
"\n",
" # Skip layer if attachments are not enabled\n",
" if layer.properties.hasAttachments == True:\n",
" # Remove any characters from feature layer name that may cause problems and ensure it's unique\n",
" feature_layer_name = '{}-{}'.format(str(i), re.sub(r'[^A-Za-z0-9]+', '', layer.properties.name))\n",
" feature_layer_folder = tmp_path + feature_layer_name\n",
" \n",
" # Query to get list of object IDs in layer\n",
" feature_object_ids = layer.query(where='1=1', return_ids_only=True)\n",
" for j in range(len(feature_object_ids['objectIds'])):\n",
" current_oid = feature_object_ids['objectIds'][j]\n",
" current_oid_attachments = layer.attachments.get_list(oid=current_oid)\n",
"\n",
" if len(current_oid_attachments) > 0:\n",
" for k in range(len(current_oid_attachments)):\n",
" attachment_id = current_oid_attachments[k]['id']\n",
" attachment_name = current_oid_attachments[k]['name']\n",
" current_folder = os.path.join(feature_layer_folder, str(current_oid))\n",
" file_name = '{}-{}'.format(attachment_id, attachment_name)\n",
" current_attachment_path = layer.attachments.download(oid=current_oid,\n",
" attachment_id=attachment_id,\n",
" save_path=current_folder)\n",
" if len(attachment_keyword) > 0:\n",
" request = update_attachment(url, token, current_oid, current_attachment_path[0]\n",
" , attachment_id, attachment_keyword)\n",
" print(\"Completed updating attachment on feature layer\", feature_layer_name,\"with ID\", str(attachment_id), \"on ObjectID\", str(current_oid), \"\\n\", \"With the response of\", request)\n",
" else:\n",
" found_kw = findattachmentkeyword(attachment_name)\n",
" request = update_attachment(url, token, current_oid, current_attachment_path[0]\n",
" , attachment_id, found_kw)\n",
" print(\"Completed updating attachment on feature layer\", feature_layer_name,\"with ID\", str(attachment_id), \"on ObjectID\", str(current_oid), \"\\n\", \"With the response of\", request)\n",
" \n",
" os.remove(current_attachment_path[0])\n",
" os.rmdir(current_folder)\n",
" os.rmdir(feature_layer_folder)\n",
" \n",
"\n",
"def update_attachments():\r\n",
" with tempfile.TemporaryDirectory() as tmp:\r\n",
" tmp_path = tmp\r\n",
" for i in range(len(item_object.layers)):\r\n",
" layers = []\r\n",
" feature_layer = item_object.layers[i]\r\n",
" layers.append(feature_layer)\r\n",
" tables = item_object.tables[i]\r\n",
" layers.append(tables)\r\n",
" \r\n",
" for layer in layers: \r\n",
" url = layer.url\r\n",
"\r\n",
" # Skip layer if attachments are not enabled\r\n",
" if layer.properties.hasAttachments == True:\r\n",
" # Remove any characters from feature layer name that may cause problems and ensure it's unique\r\n",
" feature_layer_name = '{}-{}'.format(str(i), re.sub(r'[^A-Za-z0-9]+', '', layer.properties.name))\r\n",
" feature_layer_folder = tmp_path + feature_layer_name\r\n",
" \r\n",
" # Query to get list of object IDs in layer\r\n",
" feature_object_ids = layer.query(where='1=1', return_ids_only=True)\r\n",
" for j in range(len(feature_object_ids['objectIds'])):\r\n",
" current_oid = feature_object_ids['objectIds'][j]\r\n",
" current_oid_attachments = layer.attachments.get_list(oid=current_oid)\r\n",
"\r\n",
" if len(current_oid_attachments) > 0:\r\n",
" for k in range(len(current_oid_attachments)):\r\n",
" attachment_id = current_oid_attachments[k]['id']\r\n",
" attachment_name = current_oid_attachments[k]['name']\r\n",
" current_folder = os.path.join(feature_layer_folder, str(current_oid))\r\n",
" file_name = '{}-{}'.format(attachment_id, attachment_name)\r\n",
" current_attachment_path = layer.attachments.download(oid=current_oid,\r\n",
" attachment_id=attachment_id,\r\n",
" save_path=current_folder)\r\n",
" if len(attachment_keyword) > 0:\r\n",
" request = update_attachment(url, token, current_oid, current_attachment_path[0]\r\n",
" , attachment_id, attachment_keyword)\r\n",
" print(\"Completed updating attachment on feature layer\", feature_layer_name,\"with ID\", str(attachment_id), \"on ObjectID\", str(current_oid), \"\\n\", \"With the response of\", request)\r\n",
" else:\r\n",
" found_kw = findattachmentkeyword(attachment_name)\r\n",
" request = update_attachment(url, token, current_oid, current_attachment_path[0]\r\n",
" , attachment_id, found_kw)\r\n",
" print(\"Completed updating attachment on feature layer\", feature_layer_name,\"with ID\", str(attachment_id), \"on ObjectID\", str(current_oid), \"\\n\", \"With the response of\", request)\r\n",
" \r\n",
" os.remove(current_attachment_path[0])\r\n",
" os.rmdir(current_folder)\r\n",
" os.rmdir(feature_layer_folder)\r\n",
" \r\n",
"\r\n",
"update = update_attachments()"
]
}
Expand All @@ -314,4 +314,4 @@
},
"nbformat": 4,
"nbformat_minor": 4
}
}

0 comments on commit a338c5b

Please sign in to comment.