{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Create Records\n",
    "Create records and delete (or withdraw) them in bulk."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Specify a table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import datetime\n",
    "from GRANTA_MIScriptingToolkit import granta as mpy\n",
    "\n",
    "mi = mpy.connect('http://localhost/mi_servicelayer', autologon=True)\n",
    "table = mi.get_db(db_key=\"MI_Training\").get_table(\"Files for Training\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create records\n",
    "Decide which folder the new records will be added to (you can use the table itself as a parent for a 'top-level' record)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "parent = table.search_for_records_by_name(\"Other\")[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create five new **Record** objects."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<Record long name:STK Example 6:Tue Mar 23 15:52:39 2021 - 0>,\n",
       " <Record long name:STK Example 6:Tue Mar 23 15:52:39 2021 - 1>,\n",
       " <Record long name:STK Example 6:Tue Mar 23 15:52:39 2021 - 2>,\n",
       " <Record long name:STK Example 6:Tue Mar 23 15:52:39 2021 - 3>,\n",
       " <Record long name:STK Example 6:Tue Mar 23 15:52:39 2021 - 4>]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now = datetime.now().strftime(\"%c\")\n",
    "recordNames = ['STK Example 6:{} - {}'.format(now, i) for i in range(5)]\n",
    "new_records = [table.create_record(n, parent=parent, subsets={\"All files\"}) for n in recordNames]\n",
    "new_records"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Write your changes to MI \n",
    "The new records are created on the server when update() is called."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "new records:\n",
      "http://localhost/mi/datasheet.aspx?dbKey=MI_Training&recordHistoryGuid=35419e3b-e159-4833-b928-a4369fbc8654\n",
      "http://localhost/mi/datasheet.aspx?dbKey=MI_Training&recordHistoryGuid=fd60c574-05ea-4101-8c6e-8c223e334933\n",
      "http://localhost/mi/datasheet.aspx?dbKey=MI_Training&recordHistoryGuid=6599ea26-6600-44a1-88b7-92151d1e93aa\n",
      "http://localhost/mi/datasheet.aspx?dbKey=MI_Training&recordHistoryGuid=688f5f86-5af2-42a8-8fcb-198c81f29560\n",
      "http://localhost/mi/datasheet.aspx?dbKey=MI_Training&recordHistoryGuid=28aa5e97-64ef-4991-b604-7ac8ae7b35ea\n"
     ]
    }
   ],
   "source": [
    "recs = mi.update(new_records)\n",
    "\n",
    "print(\"new records:\")\n",
    "for rec in recs:\n",
    "    print(rec.viewer_url)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Delete the records\n",
    "This method interacts directly with the server; updating is not necessary. If the table is version-controlled, the records are withdrawn instead."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "mi.bulk_delete_or_withdraw_records(recs)"
   ]
  }
 ],
 "metadata": {
  "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.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
