-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathpcd_add_m2n_data.py
More file actions
48 lines (36 loc) · 1.34 KB
/
pcd_add_m2n_data.py
File metadata and controls
48 lines (36 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# pip install pandas # if needed
import pandas as pd
from utils.session_manager import SessionManager
from utils.dynamics_config import get_config
# Parameters
PathToDotEnv = "env.example"
PathToCSV = 'data\M to N.csv'
EntityM = 'systemusers'
EntityN = 'teams'
MToNRelationship = 'teammembership_association'
# Column names in CSV must match EntityM and EntityN above
# Getting authenticated Requests object.
config = get_config(PathToDotEnv)
manager = SessionManager(config)
session = manager.get_authenticated_session()
# reading the CSV
df = pd.read_csv(PathToCSV)
successful_updates = 0
failures = 0
expected_updates = len(df)
for index, row in df.iterrows():
record_m = row[EntityM]
record_n = row[EntityN]
request_uri = f'{config.api_base_url}{EntityM}({record_m})/{MToNRelationship}/$ref'
odata_id = f'{config.api_base_url}{EntityN}({record_n})'
post_json = { "@odata.id": odata_id }
r = session.post(request_uri, json = post_json)
if r.status_code != 204:
failures += 1
raw = r.content.decode('utf-8')
print(f'Error linking {record_m} to {record_n}. Error {r.status_code}: \n{raw}\n')
else:
successful_updates += 1
if index % 10 == 0:
print(f"Processed: {index + 1}")
print(f'{successful_updates} UPDATES MADE OF {expected_updates} EXPECTED UPDATES.\n{failures} FAILURES.')