{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Edit Pseudo-attributes\n",
    "View the pseudo-attributes (properties) of a record, and edit a record's color."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Connect to MI\n",
    "Specify a database and table."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from GRANTA_MIScriptingToolkit import granta as mpy\n",
    "\n",
    "mi = mpy.connect('http://localhost/mi_servicelayer', autologon=True)\n",
    "db = mi.get_db(db_key='MI_Training')\n",
    "tab = db.get_table('Training Exercise for Import')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Find a record and view its pseudo-attributes\n",
    "Search for a record (use the first result returned)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Record long name:Ti (Titanium)>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "recs = tab.search_for_records_by_name('Ti')\n",
    "record = recs[0]\n",
    "record"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Examine the pseudo-attributes on the record."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "recordType = Record\n",
      "recordHistoryIdentity = 121491\n",
      "recordColor = Red\n",
      "recordVersionNumber = 0\n",
      "tableName = Training Exercise for Import\n",
      "writable = True\n",
      "parentShortName = Metal\n",
      "parentName = Metal\n",
      "parentRecordHistoryIdentity = 121213\n",
      "shortName = Ti\n",
      "modifiedDate = 2019-09-12 00:00:00\n",
      "createdDate = 2018-06-07 00:00:00\n",
      "releasedDate = 2019-09-12 00:00:00\n",
      "lastModifier = Granta Design 79\n",
      "creator = Granta Design 1\n",
      "subsets = {'Materials'}\n",
      "name = Ti (Titanium)\n"
     ]
    }
   ],
   "source": [
    "for name, pa in record.pseudo_attributes.items():\n",
    "    print('{} = {}'.format(name, str(pa.value)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Change the record color\n",
    "Pseudo-attribute values can only be changed via the properties on the **Record** class."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'White'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "record.color = 'White'\n",
    "record.color"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Write your changes to MI (pseudo-attributes do not need to be flagged for update using `set_attribute()`)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<Record long name:Ti (Titanium)>]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mi.update([record])"
   ]
  }
 ],
 "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
}
