ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/ns_dev/Python/NinoCode/Tool_Box/RelativityLib.py
Revision: 605
Committed: Wed Jun 15 15:53:03 2016 UTC (9 years, 9 months ago) by nino.borges
Content type: text/x-python
File size: 9816 byte(s)
Log Message:

File Contents

# Content
1 """
2
3 Relativity Library
4
5 Created by
6 Emanuel Borges
7 05.22.2014
8
9 This class will be my main Relativity library
10 Should DocumentTable and COTable be their own classes that subclass RelativityConnection?
11
12
13 TODO:
14 Make it so taht you van overide the site, username and pw. Right now it's created before you can overide.
15
16 """
17
18 import requests,json
19
20 class Relativity_Connection(object):
21 """An Advanced Relativity Connection Library and Toolkit"""
22 version = '0.0.1'
23
24 ## Prod
25 rootSite = 'https://relativity5.advanceddiscovery.com'
26 rootUserName = 'emanuel.borges@advanceddiscovery.com'
27 rootPassword = 'M@y_1255'
28
29
30 ## ## Dev
31 ## #rootSite = 'http://dev.mcdermottdiscovery.com'
32 ## #rootUserName = 'eborges@mwe.com'
33 ## #rootPassword = 'MWEreldev2@'
34 ##
35 ## ## Prod
36 ## rootSite = 'https://www.mcdermottdiscovery.com'
37 ## rootUserName = 'eborges@mwe.com'
38 ## rootPassword = 'QEst3kU2!'
39
40 def __init__(self, workSpace = None):
41 self.UpdateWorkspaceDict()
42 self.workSpace = workSpace
43 self.availableTables = None
44
45 def UpdateWorkspaceDict(self):
46 """Updates the instance varible workSpaceDict with a {workspaceName:ID} dictionary"""
47 workSpaceDict = {}
48 r = requests.get(self.rootSite+'/Relativity.REST/Relativity/Workspace',auth=requests.auth.HTTPBasicAuth(self.rootUserName,self.rootPassword), headers={'X-CSRF-Header':""})
49 rawJson = r.json()
50 rawResults = rawJson['Results']
51 for i in rawResults:
52 workSpaceDict[i['Relativity Text Identifier']] = i['Artifact ID']
53
54 self.workSpaceDict = workSpaceDict
55
56 def GetAvilableTables(self):
57 """updates the list of available tables"""
58 pass
59
60 def LoadWorkspace(self, workspaceName):
61 r = requests.get(self.rootSite+ '/Relativity.REST/Relativity/Workspace/%s'% self.workSpaceDict[workSpaceName],auth=requests.auth.HTTPBasicAuth(self.rootUserName,self.rootPassword), headers={'X-CSRF-Header':""})
62 self.workSpace = workSpaceName
63
64
65
66 class DocumentTable(Relativity_Connection):
67
68 def __init__ (self,workSpace):
69 self.workSpace = workSpace
70 super(DocumentTable,self).__init__(workSpace)
71 r = requests.get(self.rootSite+ '/Relativity.REST/Relativity/Workspace/%s/Document'% self.workSpaceDict[self.workSpace],auth=requests.auth.HTTPBasicAuth(self.rootUserName,self.rootPassword), headers={'X-CSRF-Header':""})
72 self.availableTables = r
73 #return r.__dict__
74
75 def UpdateField(self,field):
76 d = {
77 "Artifact ID":1078252,"Artifact Type Name":"Tasks","Billing Time Used":5
78 }
79
80 r = requests.put('https://dev.mcdermottdiscovery.com/Relativity.REST/Workspace/1016359/Tasks/1078252',
81 auth=requests.auth.HTTPBasicAuth('eborges@mwe.com','MWEreldev2@'),
82 headers={'X-CSRF-Header':"",'Content-Type':"application/json; charset=utf-8"},
83 data=json.dumps(d))
84
85
86
87
88 class CustomObjectTable(Relativity_Connection):
89 '''You can just insts this object if you already know the workspace # and objectName. Otherwise insts a sep rl object
90 to gather the workspace number and objectName'''
91 def __init__ (self, workSpace, objectName):
92 self.workSpace = workSpace
93 super(CustomObjectTable,self).__init__(workSpace)
94 r = requests.get(self.rootSite+ '/Relativity.REST/Workspace/%s/%s'% (self.workSpaceDict[self.workSpace],objectName),
95 auth=requests.auth.HTTPBasicAuth(self.rootUserName,self.rootPassword),
96 headers={'X-CSRF-Header':""})
97
98 self.rlResponse = r.json()
99
100 rlResponseCode = r.status_code
101 print rlResponseCode
102
103 ## This is the ID of the workspace, which is needed when you update or create new items
104 ## This will only work if there are already items in this table, got to fix this...
105 self.parentID = self.rlResponse['Results'][0]['Parent Artifact']['Artifact ID']
106
107 ## You wont get the field list until after you return 1 item from the RDO
108 self.fieldMatrix = None
109
110 ## Count of the results both in total and on this page.
111 self.totalResultCount = self.UpdateTotalResultCount()
112 self.pageResultCount = self.UpdateCurrentResultCount()
113
114 ## The matrix if returned items with artifactID:name. Cant do other way around, since name can dupe.
115 self.itemMatrix = self.UpdateItemMatrix()
116
117
118 ## The objectName stays the same, since you need a new instance for each table.
119 self.objectName = objectName
120
121 ## Loading the item will update the field list and make that the active item
122 self.currentItems = None
123
124 ## You may load 1 item using single item or query and get multiple items.
125 self.currentItemCount = None
126
127
128
129 def LoadSingleItem(self, itemID):
130 '''loads a requested item into the instance and updates the field matrix'''
131 r = requests.get(self.rootSite+ '/Relativity.REST/Workspace/%s/%s/%s'% (self.workSpaceDict[self.workSpace],self.objectName,itemID),
132 auth=requests.auth.HTTPBasicAuth(self.rootUserName,self.rootPassword),
133 headers={'X-CSRF-Header':""})
134 rlResponse = r.json()
135 rlResponseCode = r.status_code
136 print rlResponseCode
137 self.currentItems = [rlResponse,]
138 self.UpdateFieldMatrix(self.currentItems[0]['__Fields'])
139 self.currentItemCount = len(self.currentItems)
140
141 def QueryForItems(self,searchString):
142 '''takes a search string and brings back a matrix containing only those items that matched the search'''
143 payload = {
144 "condition":searchString,
145 "fields":["*"]
146 }
147 ## Note that it will give you a max of 100 here because I'm setting the page size.
148 r = requests.post(self.rootSite+ '/Relativity.REST/Workspace/%s/%s/QueryResult?pagesize=100'% (self.workSpaceDict[self.workSpace],self.objectName),
149 auth=requests.auth.HTTPBasicAuth(self.rootUserName,self.rootPassword),
150 headers={'X-CSRF-Header':"",'Content-Type':"application/json; charset=utf-8"},
151 data=json.dumps(payload))
152 rlResponse = r.json()
153 rlResponseCode = r.status_code
154 print rlResponseCode
155 self.currentItems = rlResponse['Results']
156 #print self.currentItems
157 if self.currentItems:
158 self.UpdateFieldMatrix(self.currentItems[0]['__Fields'])
159 self.currentItemCount = len(self.currentItems)
160
161 return self.currentItems
162
163 def UpdateItemMatrix(self):
164 ''' Updates the item matrix with ArtifactID:name and also puts all names in the itemlist'''
165 itemMatrix = {}
166 for i in self.rlResponse['Results']:
167 itemMatrix[i['Artifact ID']] = i['Relativity Text Identifier']
168 return itemMatrix
169
170
171 def UpdateTotalResultCount(self):
172 '''Returns the current totalResultCount'''
173 return self.rlResponse['TotalResultCount']
174
175 def UpdateCurrentResultCount(self):
176 '''Returns the current result count for the page that you are on'''
177 return self.rlResponse['ResultCount']
178
179 def UpdateFieldMatrix(self,rawFieldDict):
180 '''Returns the list of fields in the table object'''
181 fieldMatrix = {}
182 for i in rawFieldDict:
183 fieldMatrix[i['Name']] = i['Field Type']
184 self.fieldMatrix = fieldMatrix
185
186
187 def CreateNewItem(self, itemFieldMatrix):
188 '''Allows you to create a new item in the DO, by passing a matrix of fields and their values'''
189 itemFieldMatrix["Artifact Type Name"] = self.objectName
190 itemFieldMatrix["Parent Artifact"] = {"Artifact ID":self.parentID}
191
192 r = requests.post(self.rootSite+ '/Relativity.REST/Workspace/%s/%s'% (self.workSpaceDict[self.workSpace],self.objectName),
193 auth=requests.auth.HTTPBasicAuth(self.rootUserName,self.rootPassword),
194 headers={'X-CSRF-Header':"",'Content-Type':"application/json; charset=utf-8"},
195 data=json.dumps(itemFieldMatrix))
196 rlResponse = r.json()
197 rlResponseCode = r.status_code
198 print rlResponseCode
199 artID = rlResponse['Results'][0]['ArtifactID']
200 ## Returning the artifact id?, not sure about this. maybe should return status and allow you to get at artifact...
201 return artID
202 #d = {
203 # "Name":"TEST","Artifact Type Name":"Tasks","Billing Time Used":5,"Parent Artifact":{"Artifact ID":1003663}
204 #}
205 #
206 #r = requests.post('https://dev.mcdermottdiscovery.com/Relativity.REST/Workspace/1016359/Tasks',
207 # auth=requests.auth.HTTPBasicAuth('eborges@mwe.com','MWEreldev2@'),
208 # headers={'X-CSRF-Header':"",'Content-Type':"application/json; charset=utf-8"},
209 # data=json.dumps(d))
210
211
212 def UpdateField(self,field):
213
214 d = {
215 "Artifact ID":1078252,"Artifact Type Name":"Tasks","Billing Time Used":5,"Parent Artifact":{"Artifact ID":1003663}
216 }
217
218 r = requests.put(self.rootSite+ '/Relativity.REST/Workspace/1016359/Tasks/1078252',
219 auth=requests.auth.HTTPBasicAuth(self.rootUserName,self.rootPassword),
220 headers={'X-CSRF-Header':"",'Content-Type':"application/json; charset=utf-8"},
221 data=json.dumps(d))
222
223