-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_accessory_assignments.py
110 lines (87 loc) · 3.9 KB
/
update_accessory_assignments.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import csv
import HTMLParser
import itertools
import json
import requests
import urllib3
from PyInquirer import prompt, Separator
# Hide SSL Warnings in Output
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# Template for Prompt
userSelectionTemplate = {
'message': 'Please select a user that will act as the assigner for these accessories (User ID - Name - Username):',
'name': 'assignedToUser',
'type': 'list'
}
# Used in sorting list of users
def extract_name(json):
return json['name']
def main():
print 'Reading in config file...'
with open('config.json') as config_file:
config = json.load(config_file)
filename = config['accessoriesFile']
apiToken = config['apiToken']
usersUrl = config['usersUrl']
accessoriesUrl = config['accessoriesUrl']
updatedAccessoriesName = config['updatedAccessoriesName']
print 'Config file read successfully'
# Headers for Authentication
headers = {
'accept': 'application/json',
'authorization': 'Bearer ' + apiToken,
'content-type': 'application/json'
}
# Ensure we get all manufacturers for de-dup checking
queryParams = {
"limit": "1000000"
}
# List of users
choiceListForUsers = []
# Look up userId by username
print 'Querying SnipeIT for available users...'
response = requests.get(usersUrl, headers=headers, params=queryParams, verify=False)
users = response.json().get('rows')
print 'Response Status: ' + str(requests.get(usersUrl, headers=headers, params=queryParams, verify=False))
# Load Accessories
print 'Loading Accessories'
response = requests.get(accessoriesUrl, headers=headers, params=queryParams, verify=False)
accessories = response.json().get('rows')
print 'Loading Accessories Complete'
if users:
users.sort(key=extract_name, reverse=False)
for row in users:
choiceListForUsers.append({'name': str(row['id']) + ' - ' + HTMLParser.HTMLParser().unescape(row['name']).encode('utf-8') + ' - ' + HTMLParser.HTMLParser().unescape(row['username']).encode('utf-8')})
print 'Query completed.'
userSelectionTemplate['choices'] = choiceListForUsers
# Prompt user to select assignedTo user
selection = prompt(userSelectionTemplate)
assignedToUser = selection['assignedToUser']
fields = assignedToUser.split('-')
userId = fields[0].strip()
with open(filename) as accessories_file, open(updatedAccessoriesName, 'w+') as updated_accessories_file:
accessoriesCsv = csv.DictReader(accessories_file, skipinitialspace=True)
accessoryId = None
assignedTo = None
foundAccessory = False
foundUser = False
assignedAccessories = []
for accessoryRow in accessoriesCsv:
# Grab Accessory's Id from Accessory's List
for accessory in accessories:
if accessory['name'].strip() == accessoryRow['Item Name'].strip():
accessoryId = accessory['id']
# Grab Assigned To User's Id from List
for user in users:
if user['name'].strip() == accessoryRow['Customer Name']:
assignedTo = user['id']
if accessoryId and assignedTo:
if [str(accessoryId), str(assignedTo)] not in assignedAccessories:
updated_accessories_file.write('INSERT INTO accessories_users (USER_ID, ACCESSORY_ID, ASSIGNED_TO, CREATED_AT) VALUES (%s, %s, %s, CURRENT_TIMESTAMP);\n' % (userId, accessoryId, assignedTo))
assignedAccessories.append([str(accessoryId), str(assignedTo)])
print str(assignedAccessories)
else:
print 'Response was either invalid or empty from server'
if __name__ == '__main__':
main()
print 'DONE'