ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/ns_dev/Python/NinoCode/Tool_Box/RelativityLib.py
Revision: 579
Committed: Thu Mar 26 13:50:08 2015 UTC (11 years ago) by nino.borges
Content type: text/x-python
File size: 9640 byte(s)
Log Message:
update before leaving

File Contents

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