1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 from importLib import Import
20
21
22
23
24
26
28 return tuple((k, self[k]) for k in sorted(self))
29
31 return hash(self.__key())
32
35
36
38
39 - def __init__(self, batch, backend, master_label, create_orgs=False):
44
46 org_map = self.backend.lookupOrgMap(self.master_label)
47 self.mn_to_mi = org_map['master-name-to-master-id']
48 self.mi_to_li = org_map['master-id-to-local-id']
49
51 try:
52
53 if not self.backend.lookupMaster(self.master_label):
54 self.backend.createMaster(self.master_label)
55
56
57
58 missing_master_orgs = []
59 for org in self.batch:
60 if org['name'] not in list(self.mn_to_mi.keys()):
61 missing_master_orgs.append(org)
62 if len(missing_master_orgs) > 0:
63 self.backend.createMasterOrgs(self.master_label,
64 missing_master_orgs)
65
66
67
68
69
70 if self.create_orgs:
71 orgs_to_create = []
72 orgs_to_link = []
73 update_master_orgs = []
74 for org in self.batch:
75 if (org['id'] not in list(self.mi_to_li.keys())
76 or not self.mi_to_li[org['id']]):
77 local_id = self.backend.lookupOrg(org['name'])
78 if local_id:
79 orgs_to_link.append({
80 'master_id': org['id'],
81 'local_id': local_id})
82 else:
83 orgs_to_create.append(org['name'])
84 if len(orgs_to_create) > 0:
85 new_org_map = self.backend.createOrgs(orgs_to_create)
86 for org in orgs_to_create:
87 update_master_orgs.append({
88 'master_id': self.mn_to_mi[org],
89 'local_id': new_org_map[org]})
90 update_master_orgs += orgs_to_link
91 if len(update_master_orgs) > 0:
92 self.backend.updateMasterOrgs(update_master_orgs)
93
94
95 self._create_maps()
96
97
98
99
100
101 trusts_to_create = set([])
102 for org in self.batch:
103 for trust in org['org_trust_ids']:
104 if (org['id'] in list(self.mi_to_li.keys())
105 and trust['org_id'] in list(self.mi_to_li.keys())):
106 my_org_id = self.mi_to_li[org['id']]
107 self.backend.clearOrgTrusts(my_org_id)
108 my_trust_id = self.mi_to_li[trust['org_id']]
109 trusts_to_create.add(hashabledict({
110 'org_id': my_org_id,
111 'trust': my_trust_id}))
112
113
114 trusts_to_create.add(hashabledict({
115 'org_id': my_trust_id,
116 'trust': my_org_id}))
117
118 if len(trusts_to_create) > 0:
119 self.backend.createOrgTrusts(trusts_to_create)
120 except:
121 self.backend.rollback()
122 raise
123 self.backend.commit()
124