forked from aweisenseesunbird/item-check
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitem_check.py
More file actions
148 lines (133 loc) · 5.07 KB
/
Copy pathitem_check.py
File metadata and controls
148 lines (133 loc) · 5.07 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from unittest import result
import requests
import pandas
import json
import streamlit as st
from io import BytesIO
# Disable the warnings
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# For debugging in vscode
debug = False
if debug:
st.write('Debug mode on, waiting for connection from remote debugger')
import ptvsd
ptvsd.enable_attach(address=('localhost', 5678))
ptvsd.wait_for_attach()
def get_locations():
headers = {
'Content-Type': 'application/json'
}
r = requests.get(f'https://{host}/api/v1/locations',
verify=False,
auth=(username, password),
headers=headers
)
locations = [location['id'] for location in r.json()['locations']]
return locations
def find_item(item_name, progress_window=False, percent_complete=False):
if progress_window and percent_complete:
update_progress_window(percent=percent_complete, window=progress_window)
headers = {
'Content-Type': 'application/json'
}
body = {
'columns':[
{'name':'tiName','filter':{'eq':item_name}}
],
'selectedColumns':[
{'name':'tiName'},
{'name':'cmbLocation'}
]
}
r = requests.post(f'https://{host}/api/v2/quicksearch/items?pageSize=0',
data=json.dumps(body),
verify=False,
auth=(username, password),
headers=headers
)
if r.status_code == 400:
return False
# result_len = r.json()['count']
results = r.json()['searchResults']['items']
# page=0
exact_match = [item['cmbLocation'] for item in results if item['tiName'] == item_name]
if len(exact_match) > 0:
return exact_match[0]
else:
return False
def st_main_input_window():
st.write("""
# Item checker
This is the Item Checker tool, please make your selections and we'll try to find your items in dcTrack!
""")
options = {}
options['host'] = st.text_input('dctrack host/ip',
key='host',
)
options['username'] = st.text_input('username',
key='username',
value='admin',
)
options['password'] = st.text_input('password',
key='password',
type='password',
value=password,
)
options['uploaded_file'] = st.file_uploader(label='incoming file',
type=['xlsx','csv'],
key='upload_file',
)
return options
def st_get_name_col():
selection = st.selectbox('Select the field which contains names to look up',
items.columns,
)
if st.button(f'Use {selection}'):
return selection
if __name__ == '__main__':
username = 'admin'
password = 'sunbird'
user_input = st_main_input_window()
if '' not in user_input.values() and None not in user_input.values():
file = user_input['uploaded_file']
if file.name.endswith('.xlsx'):
items = pandas.read_excel(file)
elif file.name.endswith('.csv'):
items = pandas.read_csv(file)
else:
st.error('File type must be xlsx or csv')
host = user_input['host']
username = user_input['username']
password = user_input['password']
name_column = st_get_name_col()
if name_column:
locations = get_locations()
lookup_items = items[name_column]
results = []
found = 0
not_found = 0
loading = st.progress(0)
loading_message = st.text(f'Found: {found} Not Found: {not_found}')
for count, item in enumerate(lookup_items):
percent_complete = (count + 1) / len(lookup_items)
loading_message.text(f'Found: {found} Not Found: {not_found}')
loading.progress(percent_complete)
search_result = find_item(item)
if search_result:
found+=1
else:
not_found+=1
results.append({'Name': item, 'Result': search_result})
output = pandas.DataFrame(results)
old_file = file.name.split('\\')[-1].split('.')[0]
new_file_name = f'{old_file}-results.xlsx'
new_file = BytesIO()
output.to_excel(new_file, index=False)
st.success(f'Done! The file was saved as {new_file_name}')
st.download_button(
label="Download results",
data=new_file.getvalue(),
file_name=new_file_name,
mime="application/vnd.ms-excel"
)