Reading Related Table Values for IUser¶
Reading Related Table Values for IUser¶
1import datetime
2def var_type(value):
3 """VBScript VarType equivalent - returns type name string."""
4 if value is None:
5 return 'null'
6 elif isinstance(value, bool):
7 return 'boolean'
8 elif isinstance(value, int):
9 return 'integer'
10 elif isinstance(value, float):
11 return 'double'
12 elif isinstance(value, str):
13 return 'string'
14 elif isinstance(value, (datetime.date, datetime.datetime)):
15 return 'date'
16 else:
17 return 'object'
18
19# This script shows how to access addition data related to a user.
20# This scripts requires a USERINFO table linked to the USERS table (USERINFO.ui_userkey -> USERS.u_key)
21
22
23ui = impact.gui
24ot = ui.output_toolbox
25
26db = impact.active_database
27ad = impact.active_drawing
28
29user_id = "ADMIN"
30
31
32_DESCRIBE_STATUS_MAP = {
33 ipDatabaseTableRowStatus.dtrsUnmodified: "Unmodified",
34 ipDatabaseTableRowStatus.dtrsAdded: "Added",
35 ipDatabaseTableRowStatus.dtrsModified: "Modified",
36 ipDatabaseTableRowStatus.dtrsDeleted: "Deleted",
37}
38
39def describe_status(status):
40 return _DESCRIBE_STATUS_MAP.get(status, None)
41
42
43def output_native_value(row, name):
44 v = row.load(name)
45
46 if (v is None or v == ""):
47 ot.add(" " + str(name) + ":")
48 else:
49 ot.add(" " + str(name) + ": VarType=" + str(var_type(v)) + ", Value=" + str(v))
50
51def describe_row(r_index, row):
52
53 # each row can be identified by a GUID - but it transient and is not streamed with the drawing
54 # opening the same drawing again with stored related table data WILL produce different GUID's
55 ot.add(" Row " + str(r_index) + ", GUID: " + str(row.guid) + ", Status: " + str(describe_status(row.status)))
56
57 row.values_as_variants = True
58
59 for f_index in range(1, row.field_count + 1):
60 output_native_value(row, row.field_name(f_index))
61
62def describe_table(table):
63 if table is None or (hasattr(table, "isNone") and table.isNone()):
64 ot.add("Unable to locate related table data for USERINFO table")
65 return
66
67 relationship = table.table_relationship
68
69 ot.add("Relationship: " + str(relationship.formatted_name))
70 ot.add(" Table: " + str(table.table.name))
71 ot.add(" Row Count: " + str(table.count))
72 ot.add(" Rows Needing Update: " + str(table.needs_updating_count))
73 ot.add(" Deleted Rows: " + str(table.deleted_count))
74
75 for r_index in range(1, table.count + 1):
76 describe_row(r_index, table.item(r_index))
77
78
79ot.clear()
80
81 # alternative syntax
82 # r_index = 1
83 # for each row in table
84 # DescribeRow r_index, row
85 # r_index = r_index + 1
86 # next
87
88user = db.users.item(user_id)
89
90if not user.isNone():
91 ot.add("Locating USERINFO table for User")
92
93 table_values = user.values.related_table_values
94
95 describe_table(table_values.item("USERINFO.UI_USERKEY->USERS.U_KEY"))
96
97else:
98 ot.add("Unable to locate IUser with ID " + str(user_id))