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