{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Administration: Prepare portal items for notebooks\n",
    "\n",
    ">* 👟 Ready To Run!\n",
    "* 🔒 Requires Administrator Privileges\n",
    "* ⌨️ Administration\n",
    "\n",
    "Some of the sample notebooks shipped with your Organization reference hosted feature layers and other items. This notebook needs to be run by an Administrator to set up an Organization with all those features and items."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import logging\n",
    "log = logging.getLogger()\n",
    "\n",
    "from IPython.display import display, HTML\n",
    "\n",
    "from arcgis.gis import GIS\n",
    "\n",
    "gis = GIS(\"home\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['shapefiles', 'portal_sd_items', 'csvs', 'arcgis_server_sd_items']\n"
     ]
    }
   ],
   "source": [
    "publish_data_dir_root = \"/arcgis/samplesdata/files_to_publish/\"\n",
    "print(os.listdir(publish_data_dir_root))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_absolute_file_paths(directory):\n",
    "    for dirpath, _ ,filenames in os.walk(directory):\n",
    "        for f in filenames:\n",
    "            yield os.path.abspath(os.path.join(dirpath, f))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run the below cell to publish all CSV files in the `files_to_publish` dir:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "csv_file_dir = os.path.join(publish_data_dir_root, \"csvs\")\n",
    "\n",
    "for csv_file_path in get_absolute_file_paths(csv_file_dir):\n",
    "    csv_file_path = os.path.join(publish_data_dir_root,\n",
    "                                 current_csv_file)\n",
    "    try:\n",
    "        item = gis.content.add({}, csv_file_path)\n",
    "        published_item = item.publish()\n",
    "        published_item.share(everyone=True)\n",
    "        display(published_item)\n",
    "    except Exception as e:\n",
    "        log.warning(f\"Could not publish {csv_file_path}. \"\\\n",
    "                    f\"Exception thrown: {e}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run the below cell to publish all zipped shapefiles in the `files_to_publish` dir:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <b>Samples Data Item 1</b>\n",
       "                        <br/>Last Modified: October 12, 2017\n",
       "                        <br/>0 comments, 0 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"Slovenia Upravne Enote Boundaries 2015\" type:Feature Layer Collection owner:esri_livingatlas>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <b>Samples Data Item 2</b>\n",
       "                        <br/>Last Modified: October 12, 2017\n",
       "                        <br/>0 comments, 0 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"2017 USA Grocery Store Market Opportunity\" type:Map Image Layer owner:esri_livingatlas>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <b>Samples Data Item 3</b>\n",
       "                        <br/>Last Modified: October 12, 2017\n",
       "                        <br/>0 comments, 0 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"World Regions\" type:Feature Layer Collection owner:esri_livingatlas>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <b>Samples Data Item 4</b>\n",
       "                        <br/>Last Modified: October 12, 2017\n",
       "                        <br/>0 comments, 0 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"MODIS True Color - Aqua Surface Reflectance\" type:Imagery Layer owner:esri_livingatlas>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "shapefile_dir = os.path.join(publish_data_dir_root, \"shapefiles\")\n",
    "\n",
    "for shapefile_path in get_absolute_file_paths(shapefile_dir):\n",
    "    try:\n",
    "        item = gis.content.add({}, shapefile_path)\n",
    "        published_item = item.publish()\n",
    "        published_item.share(everyone=True)\n",
    "        display(published_item)\n",
    "    except Exception as e:\n",
    "        log.warning(f\"Could not publish {shapefile_path}. \"\\\n",
    "                    f\"Exception thrown: {e}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run the below cell to publish all Portal `.sd` files in the `files_to_publish` dir:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <b>Samples Data Item 5</b>\n",
       "                        <br/>Last Modified: October 12, 2017\n",
       "                        <br/>0 comments, 0 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"Slovenia Upravne Enote Boundaries 2015\" type:Feature Layer Collection owner:esri_livingatlas>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "portal_sd_file_dir = os.path.join(publish_data_dir_root,\n",
    "                                  \"portal_sd_items\")\n",
    "\n",
    "for portal_sd_path in get_absolute_file_paths(portal_sd_file_dir):\n",
    "    try:\n",
    "        item = gis.content.add({}, portal_sd_path)\n",
    "        published_item = item.publish()\n",
    "        published_item.share(everyone=True)\n",
    "        display(published_item)\n",
    "    except Exception as e:\n",
    "        log.warning(f\"Could not publish {shapefile_path}.\"\\\n",
    "                    f\"Exception thrown: {e}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run the below cell to define two helper functions used by the remainder of cells in this notebook."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "def prompt_user_for_arcgis_server(gis=gis):\n",
    "    \"\"\"Asks users to pick an ArcGIS Server from gis.admin.servers.\n",
    "    Returns the `arcgis.gis.server.Server` instance chosen\"\"\"\n",
    "    def prompt_user_yes_no(prompt):\n",
    "        \"\"\"Returns a bool of if user answered 'y' or 'n'\"\"\"\n",
    "        answer = \"\"\n",
    "        while (\"y\" not in answer.lower()) and \\\n",
    "              (\"n\" not in answer.lower()):\n",
    "            answer = str(input(prompt))\n",
    "        return \"y\" in answer.lower()\n",
    "      \n",
    "    for server in gis.admin.servers.list():\n",
    "        if prompt_user_yes_no(f\"Select server {server.url} ? (y/n)\\n\"):\n",
    "            return server\n",
    "    raise Exception(\"You must specify an ArcGIS Server to publish to.\")\n",
    "\n",
    "def attempt_server_sd_publish(server, server_sd_file_dir, gis=gis):\n",
    "    \"\"\"Given an `arcgis.gis.server.Server` instance and a folder of\n",
    "    SD files, attempt to publish all .sd files in that folder\"\"\"\n",
    "    for sd_file_path in get_absolute_file_paths(server_sd_file_dir):\n",
    "        try:\n",
    "            num_retries = 3 # there are intermittent failures\n",
    "            for i in range(num_retries):\n",
    "                try:\n",
    "                    display(HTML(f\"Attempting to publish {sd_file_path}\"\\\n",
    "                                 \", please wait...\"))\n",
    "                    result = server.publish_sd(sd_file_path)\n",
    "                    display(HTML(f\"Result: {result}\"))\n",
    "                    break\n",
    "                except Exception as e:\n",
    "                    if i+1 == num_retries:\n",
    "                        raise Exception(\n",
    "                            f\"Direct to ArcGIS Server publishing \"\\\n",
    "                            f\"failed after {num_retries} attempts. \"\\\n",
    "                            f\"Exception thrown: {e}\")\n",
    "        except Exception as e:\n",
    "            display(HTML(\"<br>\"))\n",
    "            log.warning(f\"Could not publish .sd file: {e}\")\n",
    "\n",
    "    display(HTML(\"<h1>Done publishing these .sds</h1><br>-----\"))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run the below cell to publish all Image Server ArcGIS Server `.sd` files in the `files_to_publish` dir. Note that there is an interactive prompt for you to pick the Image Server to publish `.sd` files to, so you must interact with the below cell."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<h2>Please select an ArcGIS Image Server to publish all .sd files to:</h2>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Select server https://example.com/server/admin ? (y/n)\n",
      "n\n",
      "Select server https://example.com/ra/admin ? (y/n)\n",
      "y\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "Attempting to publish /arcgis/home/Sonoma_NLCD2011.sd, please wait..."
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "Result: True"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "Attempting to publish  /arcgis/home/After_L8.sd, please wait..."
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "Result: True"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "Attempting to publish /arcgis/home/Sonoma_DEM.sd, please wait..."
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "Result: True"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "Attempting to publish /arcgis/home/Before_L8.sd, please wait..."
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "Result: True"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "Attempting to publish  /arcgis/home/Sonoma_Basins.sd, please wait..."
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "Result: True"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<h1>Done publishing these .sds</h1><br>-----"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "image_server_sd_file_dir = os.path.join(publish_data_dir_root,\n",
    "                                        \"arcgis_server_sd_items\",\n",
    "                                        \"image_server_sds\")\n",
    "display(HTML(\"<h2>Please select an ArcGIS Image Server to \"\\\n",
    "             \"publish all .sd files to:</h2>\"))\n",
    "image_server = prompt_user_for_arcgis_server()\n",
    "attempt_server_sd_publish(image_server, image_server_sd_file_dir)"
   ]
  }
 ],
 "metadata": {
  "esriNotebookRuntime": {
   "notebookRuntimeName": "ArcGIS Notebook Python 3 Standard",
   "notebookRuntimeVersion": "10.7.1"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "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.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
