| 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 |
## 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 |
self.UpdateFieldMatrix(self.currentItems[0]['__Fields'])
|
| 151 |
self.currentItemCount = len(self.currentItems)
|
| 152 |
return self.currentItems
|
| 153 |
|
| 154 |
def UpdateItemMatrix(self):
|
| 155 |
''' Updates the item matrix with ArtifactID:name and also puts all names in the itemlist'''
|
| 156 |
itemMatrix = {}
|
| 157 |
for i in self.rlResponse['Results']:
|
| 158 |
itemMatrix[i['Artifact ID']] = i['Relativity Text Identifier']
|
| 159 |
return itemMatrix
|
| 160 |
|
| 161 |
|
| 162 |
def UpdateTotalResultCount(self):
|
| 163 |
'''Returns the current totalResultCount'''
|
| 164 |
return self.rlResponse['TotalResultCount']
|
| 165 |
|
| 166 |
def UpdateCurrentResultCount(self):
|
| 167 |
'''Returns the current result count for the page that you are on'''
|
| 168 |
return self.rlResponse['ResultCount']
|
| 169 |
|
| 170 |
def UpdateFieldMatrix(self,rawFieldDict):
|
| 171 |
'''Returns the list of fields in the table object'''
|
| 172 |
fieldMatrix = {}
|
| 173 |
for i in rawFieldDict:
|
| 174 |
fieldMatrix[i['Name']] = i['Field Type']
|
| 175 |
self.fieldMatrix = fieldMatrix
|
| 176 |
|
| 177 |
|
| 178 |
def CreateNewItem(self, itemFieldMatrix):
|
| 179 |
'''Allows you to create a new item in the DO, by passing a matrix of fields and their values'''
|
| 180 |
itemFieldMatrix["Artifact Type Name"] = self.objectName
|
| 181 |
itemFieldMatrix["Parent Artifact"] = {"Artifact ID":self.parentID}
|
| 182 |
|
| 183 |
r = requests.post(self.rootSite+ '/Relativity.REST/Workspace/%s/%s'% (self.workSpaceDict[self.workSpace],self.objectName),
|
| 184 |
auth=requests.auth.HTTPBasicAuth(self.rootUserName,self.rootPassword),
|
| 185 |
headers={'X-CSRF-Header':"",'Content-Type':"application/json; charset=utf-8"},
|
| 186 |
data=json.dumps(itemFieldMatrix))
|
| 187 |
rlResponse = r.json()
|
| 188 |
rlResponseCode = r.status_code
|
| 189 |
print rlResponseCode
|
| 190 |
artID = rlResponse['Results'][0]['ArtifactID']
|
| 191 |
## Returning the artifact id?, not sure about this. maybe should return status and allow you to get at artifact...
|
| 192 |
return artID
|
| 193 |
#d = {
|
| 194 |
# "Name":"TEST","Artifact Type Name":"Tasks","Billing Time Used":5,"Parent Artifact":{"Artifact ID":1003663}
|
| 195 |
#}
|
| 196 |
#
|
| 197 |
#r = requests.post('https://dev.mcdermottdiscovery.com/Relativity.REST/Workspace/1016359/Tasks',
|
| 198 |
# auth=requests.auth.HTTPBasicAuth('eborges@mwe.com','MWEreldev2@'),
|
| 199 |
# headers={'X-CSRF-Header':"",'Content-Type':"application/json; charset=utf-8"},
|
| 200 |
# data=json.dumps(d))
|
| 201 |
|
| 202 |
|
| 203 |
def UpdateField(self,field):
|
| 204 |
|
| 205 |
d = {
|
| 206 |
"Artifact ID":1078252,"Artifact Type Name":"Tasks","Billing Time Used":5,"Parent Artifact":{"Artifact ID":1003663}
|
| 207 |
}
|
| 208 |
|
| 209 |
r = requests.put(self.rootSite+ '/Relativity.REST/Workspace/1016359/Tasks/1078252',
|
| 210 |
auth=requests.auth.HTTPBasicAuth(self.rootUserName,self.rootPassword),
|
| 211 |
headers={'X-CSRF-Header':"",'Content-Type':"application/json; charset=utf-8"},
|
| 212 |
data=json.dumps(d))
|
| 213 |
|
| 214 |
|