-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdb_actions.py
306 lines (281 loc) · 10.2 KB
/
db_actions.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#
# Database Calls
# Hudl
#
# Created by Ethan Seyl 2016
#
import pymysql.cursors
import configparser
import logging
import os
class MyDB(object):
_db = None
_db_cur = None
_config = None
_working_dir = None
def __init__(self):
self._working_dir = os.path.dirname(__file__)
self._config = configparser.ConfigParser()
self._config.read(
'{}/config/DeviceNanny.ini'.format(self._working_dir))
self._db = pymysql.connect(
host=self._config['DATABASE']['host'],
user=self._config['DATABASE']['user'],
password=self._config['DATABASE']['password'],
db=self._config['DATABASE']['name'],
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
self._db_cur = self._db.cursor()
logging.debug("[db_actions][init] Connected to database successfully")
def db_fetch(self, string):
try:
cur = self._db.cursor()
cur.execute(string)
item = cur.fetchone()
return item
except Exception as e:
logging.error("[db_actions][db_fetch] Exception - {}".format(e))
finally:
cur.close()
def db_fetch_all(self, string):
try:
cur = self._db.cursor()
cur.execute(string)
item = cur.fetchall()
return item
except Exception as e:
logging.error(
"[db_actions][db_fetch_all] Exception - {}".format(e))
finally:
cur.close()
def db_commit(self, string):
try:
cur = self._db.cursor()
cur.execute(string)
self._db.commit()
except Exception as e:
logging.error("[db_actions][db_commit] Exception - {}".format(e))
finally:
cur.close()
def new_device_id(self):
"""
:return: Highest DeviceID in Devices table + 1
"""
try:
last_id_num = self.db_fetch(
"SELECT DeviceID from Devices where DeviceID=(SELECT max(DeviceID) FROM Devices)"
)
id_number = last_id_num.get('DeviceID') + 1
except:
id_number = '0'
logging.debug("[db_actions][new_device_id] {}".format(id_number))
return id_number
def device_ids(self):
"""
:return: All DeviceIDs from Devices table.
"""
devices = self.db_fetch_all("SELECT DeviceID from Devices")
return devices
def checked_out_by(self, device_id):
"""
:param device_id:
:return: UserID
"""
user_info = self.db_fetch(
"SELECT CheckedOutBy from Devices WHERE DeviceID = {}".format(
int(device_id)))
logging.debug(
"[db_actions][checked_out_by] DeviceID {} checked out by {}".
format(device_id, user_info.get("CheckedOutBy")))
return user_info.get("CheckedOutBy")
def get_device_name(self, location, port):
"""
:param port: USB port
:param location: Location
:return: DeviceName
"""
try:
device = self.db_fetch(
"SELECT DeviceName from Devices WHERE Port = '{}' AND Location = '{}'".
format(port, location))
return device.get("DeviceName")
except Exception as e:
logging.debug(
"[db_actions][get_device_name] Exception - {}".format(e))
def get_device_name_from_id(self, location, device_id):
"""
:param device_id:
:return: DeviceName
"""
logging.debug(
"[db_actions][get_device_name_from_id] Device ID = {}, Location = {}".
format(device_id, location))
try:
device = self.db_fetch(
"SELECT DeviceName from Devices WHERE DeviceID = '{}' AND Location = '{}'".
format(device_id, location))
logging.debug(
"[db_actions][get_device_name_from_id] Device Name: {}".format(
device.get("DeviceName")))
return device.get("DeviceName")
except Exception as e:
logging.error(
"[db_actions][get_device_name_from_id] Exception - {}".format(
e))
def get_device_id_from_port(self, location, port):
"""
:param port:
:param location:
:return: DeviceID
"""
device = self.db_fetch(
"SELECT DeviceID from Devices WHERE Location = '{}' AND Port = '{}'".
format(location, port))
try:
return device.get("DeviceID")
except AttributeError:
return None
def get_port_from_device_id(self, device_id):
"""
:param device_id:
:return: Port
"""
port = self.db_fetch(
"SELECT Port from Devices WHERE DeviceID = '{}'".format(device_id))
try:
return port.get("Port")
except:
logging.debug(
"[db_actions][get_port_from_device_id] No port registered for device {}".
format(device_id))
def get_device_id_from_serial(self, serial):
"""
:param serial:
:return: DeviceID
"""
device = self.db_fetch(
"SELECT DeviceID from Devices WHERE SerialUDID = '{}'".format(
serial))
try:
return device.get("DeviceID")
except AttributeError:
return None
def user_info(self, user_input):
"""
:param user_input: FirstName LastName OR UserID
:return: FirstName, LastName, SlackID, Location, UserID
"""
logging.debug("[db_actions][user_info] user_input = {}. Type = {}".
format(user_input, type(user_input)))
try:
int(user_input[0])
user_info = self.db_fetch(
"SELECT * from Users WHERE UserID = {}".format(user_input[0]))
logging.info(
"[db_actions][user_info] UserID input. Checked out by {}".
format(user_info))
return user_info
except:
try:
user_info = self.db_fetch(
"SELECT * from Users WHERE FirstName = '{}' AND LastName = '{}'".
format(str(user_input[0]), str(user_input[1])))
print("Your UserID is: {}".format(user_info.get("UserID")))
logging.info(
"[db_actions][user_info] User name input. Checked out by {}".
format(user_info))
return user_info
except:
return None
def update_time_reminded(self, device_name):
"""
Updates database with the last time a reminder was sent.
:param device_name:
"""
self.db_commit(
"UPDATE Devices set LastReminded = unix_timestamp() where DeviceName = '{}'".
format(device_name))
logging.info(
"[db_actions][update_time_reminded] LastReminded has been reset to current time."
)
def clear_port(self, device_id):
"""
Sets the port for a device to NULL.
:param device_id:
"""
self.db_commit("UPDATE Devices set Port = NULL where DeviceID = {}".
format(device_id))
def add_to_database(self, device_info):
"""
Adds info for a new device to the database.
:param device_info:
"""
self.db_commit(
"INSERT INTO Devices(DeviceName,Manufacturer,Model,Type,OS,Location,DeviceID,SerialUDID,Port,CheckedOutBy,"
"TimeCheckedOut,LastReminded) VALUES('{}','{}','{}','{}','{}','{}','{}','{}','{}','0','0','40000')".
format(device_info[0], device_info[1], device_info[2],
device_info[3], device_info[4].rstrip(), device_info[5],
device_info[6], device_info[7], device_info[8]))
def check_in(self, device_id, port):
"""
Updates database with check in info.
:param device_id:
:param port:
"""
try:
self.db_commit("UPDATE Devices set CheckedOutBy = '0',"
"Port = '{}' where DeviceID = {}".format(
port, device_id))
except Exception as e:
logging.debug("[db_actions][check_in] Exception - {}".format(e))
def check_out(self, user_id, device_id):
"""
Updates database with check out info.
:param user_id:
:param device_id:
"""
try:
self.db_commit(
"UPDATE Devices set CheckedOutBy = {}, Port = NULL,"
"TimeCheckedOut = unix_timestamp(), LastReminded = unix_timestamp() where DeviceID = {}".
format(user_id, device_id))
except Exception as e:
logging.debug(
"[db_actions][check_out] Exception in check_out - {}".format(
e))
def get_device_status(self, device_id):
"""
Returns all columns for a device.
:param device_id:
:return: DeviceName, CheckedOutBy, TimeCheckedOut, LastReminded
"""
device_status = self.db_fetch(
"SELECT DeviceName, CheckedOutBy, TimeCheckedOut, LastReminded,"
"Location from Devices where DeviceID = {}".format(device_id))
return device_status
def get_slack_id(self, user_id):
"""
:param user_id:
:return: SlackID for user
"""
slack_id = self.db_fetch(
"SELECT SlackID from Users where UserID = {}".format(user_id))
return slack_id
def get_registered_ports(self, location):
"""
:return: USB Ports for each device in database
"""
ports = self.db_fetch_all(
"SELECT Port from Devices where Port is not Null AND Location = '{}'".
format(location))
return ports
def get_serial_number_from_port(self, location, port):
"""
:param port:
:param location:
:return: Serial number
"""
serial = self.db_fetch(
"SELECT SerialUDID from Devices where Port = '{}' AND Location = '{}'".
format(port, location))
return serial.get("SerialUDID")