-
Notifications
You must be signed in to change notification settings - Fork 0
/
animalshelter.py
385 lines (298 loc) · 14.2 KB
/
animalshelter.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon 2023-9-18 20:54:13
Last updated Sun 2023-12-03
version: 1.1
author: David France
email: david.france@snhu.edu
Purpose: Created to connect MongoDB database to Dash App dashboard,
specifically the Austin Animal Shelter database.
"""
from pymongo import MongoClient
from bson.objectid import ObjectId
from collections.abc import Mapping
import hashlib
from datetime import datetime
class AnimalShelter(object):
"""CRUD operations for MongoDB database """
def __init__(self, database, collection):
"""Connect to specified database and collection"""
DB = database
COL = collection
self.client = MongoClient()
self.database = self.client['%s' % (DB)]
self.collection = self.database['%s' % (COL)]
def insert(self, data: dict) -> str:
"""Insert new document from dashboard fields"""
# Check that data is in proper format
if not isinstance(data, Mapping):
raise Exception("Error - invalid data format")
# Document is not yet complete, add calculated fields
data = self.__add_fields(data)
# Check if animal to insert is a rescue type
if 'animal_type' in data and data['animal_type'] == 'Dog':
data = self.__check_rescue_type(data)
insert_result = self.collection.insert_one(data)
if insert_result.acknowledged:
return f"New animal inserted with ID - {data['animal_id']}"
return 'ERROR - animal not added'
def insert_backup(self, data: dict) -> bool:
"""Insert complete document for backup purposes"""
# Check that data is in proper format
if not isinstance(data, Mapping):
raise Exception("Error - invalid data format")
insert_result = self.collection.insert_one(data)
return insert_result.acknowledged
def find(self, data: dict) -> list:
""""Find a document"""
# Check that data is in proper format
if not isinstance(data, Mapping):
raise Exception("Error - invalid data format")
# Not all fields need to be returned to dashboard
cursor = self.collection.find(data, {'_id': 0, 'datetime': 0, 'monthyear': 0, 'rescue_type': 0})
return self.__cursor_to_dict(cursor)
def update(self, doc: dict, values: dict) -> int:
"""Update one or many documents"""
# Check that data is in proper format
if not isinstance(doc, Mapping) or not isinstance(values, Mapping):
raise Exception("Error - invalid data format")
# If DOB is changed, ages need to be updated
if 'date_of_birth' in values:
values['age_upon_outcome'] = self.__age(values['date_of_birth'])
values['age_upon_outcome_in_weeks'] = self.__age_in_weeks(values['date_of_birth'])
# If individual animal is being updated, its id will be in one or both doc and values variables
# If id is in values that means the id is being updated, so that is handled first
if 'animal_id' in values:
result = self.collection.update_one(doc, {'$set': values}) # Update animal
animal = self.find({'animal_id': values['animal_id']})[0] # Return updated animal
if animal['animal_type'] == 'Dog': # If animal is a dog, update rescue_type
animal = self.__check_rescue_type(animal)
self.collection.update_one({'animal_id': values['animal_id']}, {'$set': {'rescue_type': animal['rescue_type']}})
elif 'animal_id' in doc:
result = self.collection.update_one(doc, {'$set': values}) # Update animal
animal = self.find({'animal_id': doc['animal_id']})[0] # Return updated animal
if animal['animal_type'] == 'Dog': # If animal is a dog, update rescue_type
animal = self.__check_rescue_type(animal)
self.collection.update_one({'animal_id': doc['animal_id']}, {'$set': {'rescue_type': animal['rescue_type']}})
else:
result = self.collection.update_many(doc, {'$set': values})
# Return all animals that have been updated
animals = self.find(values)
# For each updated animal, if it's a dog then update rescue_type
for animal in animals:
if animal['animal_type'] == 'Dog':
animal = self.__check_rescue_type(animal)
self.collection.update_one({'animal_id': animal['animal_id']}, {'$set': {'rescue_type': animal['rescue_type']}})
return result.modified_count
def delete(self, data: dict, many: bool = False) -> int:
"""Delete one or many documents"""
# Check that data is in proper format
if not isinstance(data, Mapping):
raise Exception("Error - invalid data format")
if not many:
result = self.collection.delete_one(data)
else:
result = self.collection.delete_many(data)
return result.deleted_count
def check_user(self, username: str, password: str) -> bool:
"""Determine if username/password pair is valid"""
# Create hash functions for each variable
user_hash = hashlib.sha256()
pword_hash = hashlib.sha256()
# Get salt value for username
salt = self.__get_salt(username)
username += salt
password += salt
# Hash the input username/password pair
user_hash.update(username.encode('utf8'))
pword_hash.update(password.encode('utf8'))
# If username/password pair exists, it will return a cursor
result = self.collection.find({user_hash.hexdigest(): pword_hash.hexdigest()})
return len(self.__cursor_to_dict(result)) > 0
def check_permission(self, username: str, password: str) -> bool:
"""Check if user has read/write or read-only permission"""
# Get salt value for username
salt = self.__get_salt(username)
read_write = 'read/write' + salt
tf = 'True' + salt
# Create hash functions for each variable
user_hash = hashlib.sha256()
pword_hash = hashlib.sha256()
rw_hash = hashlib.sha256()
tf_hash = hashlib.sha256()
# Encode each variable using the hash function
user_hash.update((username+salt).encode('utf8'))
pword_hash.update((password+salt).encode('utf8'))
rw_hash.update(read_write.encode('utf8'))
tf_hash.update(tf.encode('utf8'))
# Get document for username/password pair
result = self.collection.find({user_hash.hexdigest(): pword_hash.hexdigest()})
# Insert key/value pairs from result to compare read/write permission
document = {}
for doc in result:
for key, value in doc.items():
document[str(key)] = str(value)
# Return if value for key 'read/write' is 'True'
return document[rw_hash.hexdigest()] == tf_hash.hexdigest()
# Return the salt value for a given user
def __get_salt(self, user):
username = hashlib.sha256()
username.update(user.encode('utf8'))
# Connect to salt database
salt_client = MongoClient()
salt_db = self.client['%s' % ('Other')]
result = self.__cursor_to_dict(salt_db.misc.find({'user': username.hexdigest()}))
# If user doesn't exist, return
if len(result) < 1:
return 'none'
return result[0]['salt']
def name_validation(self, name: str) -> bool:
"""Validate name for data input"""
if len(name) > 15:
return False
characters = ['*', '-', '.', ' ']
for char in name:
if not char.isalpha() and char not in characters:
return False
return True
def check_id(self, id: str) -> bool:
"""Validate ID for data update"""
try:
animal = self.find({'animal_id': id})[0]
return True
except:
return False
# Check if animal fits into a rescue_type, return updated dictionary ready to insert
def __check_rescue_type(self, dict):
# Animal can fit more than one rescue type, or be updated to fit none, each type gets inserted into array
rescue_types = []
water_breeds = ['Chesa Bay Retr Mix',
'Labrador Retriever Mix',
'Newfoundland']
mountain_breeds = ['German Shepherd',
'Alaskan Malamute',
'Old English Sheepdog',
'Siberian Husky',
'Rottweiler']
disaster_breeds = ['Doberman Pinsch',
'German Shepherd',
'Golden Retriever',
'Bloodhound',
'Rottweiler']
# Append array if animal fits water type
if dict['breed'] in water_breeds and dict['sex_upon_outcome'] == 'Intact Female' and dict['age_upon_outcome_in_weeks'] <= 156:
rescue_types.append('water')
# Append array if animal fits mountain/wilderness type
if dict['breed'] in mountain_breeds and dict['sex_upon_outcome'] == 'Intact Male' and dict['age_upon_outcome_in_weeks'] <= 156:
rescue_types.append('mountain_wilderness')
# Append array if animal fits disaster/tracking type
if dict['breed'] in disaster_breeds and dict['sex_upon_outcome'] == 'Intact Male' and dict['age_upon_outcome_in_weeks'] <= 300:
rescue_types.append('disaster_tracking')
# Append dictionary to insert into database. Empty dictionaries are still inserted,
# which will take away categories from animals that have been updated to no
# longer fit into a rescue category
dict['rescue_type'] = rescue_types
return dict
# Calculate and add fields to animal being inserted
# Order matters to keep the data structure in line with dashboard data table
def __add_fields(self, data):
new_data = {}
new_data['row'] = self.__next_row()
new_data['age_upon_outcome'] = self.__age(data['date_of_birth'])
new_data['animal_id'] = self.__next_id()
if 'animal_type' in data:
new_data['animal_type'] = data['animal_type']
else:
new_data['animal_type'] = ''
if 'breed' in data:
new_data['breed'] = data['breed']
else:
new_data['breed'] = ''
if 'color' in data:
new_data['color'] = data['color']
else:
new_data['color'] = ''
if 'date_of_birth' in data:
new_data['date_of_birth'] = data['date_of_birth']
new_data['datetime'] = datetime.now().strftime("%Y-%m-%d %H:%M")
new_data['monthyear'] = datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
else:
new_data['date_of_birth'] = ''
new_data['datetime'] = ''
new_data['monthyear'] = ''
if 'name' in data:
new_data['name'] = data['name']
else:
new_data['name'] = ''
if 'outcome_subtype' in data:
new_data['outcome_subtype'] = data['outcome_subtype']
else:
new_data['outcome_subtype'] = ''
if 'outcome_type' in data:
new_data['outcome_type'] = data['outcome_type']
else:
new_data['outcome_type'] = ''
if 'sex_upon_outcome' in data:
new_data['sex_upon_outcome'] = data['sex_upon_outcome']
else:
new_data['sex_upon_outcome'] = ''
if 'location_lat' in data:
new_data['location_lat'] = data['location_lat']
new_data['location_long'] = data['location_long']
else:
new_data['location_lat'] = 0.0
new_data['location_long'] = 0.0
if 'date_of_birth' in data:
new_data['age_upon_outcome_in_weeks'] = self.__age_in_weeks(data['date_of_birth'])
else:
new_data['age_upon_outcome_in_weeks'] = 0.0
return new_data
# Return next animal_id. Used when creating new animals.
def __next_id(self):
last_id = self.collection.find_one(sort=[('animal_id', -1)], projection=['animal_id'])
id = int(last_id['animal_id'][1:])
id += 1
return f"A{id}"
# Return next 'row' number. Used when creating new animals.
def __next_row(self):
last_row = self.collection.find_one(sort=[('row', -1)], projection=['row'])
next_row = last_row['row'] + 1
return next_row
# Return age string from birthdate
def __age(self, date) -> str:
dob = datetime.strptime(date, "%Y-%m-%d")
days = (datetime.now() - dob).days
s = ''
if days < 7:
if days > 1:
s = 's'
return f'{days} day{s}'
elif days < 30:
weeks = days // 7
if weeks > 1:
s = 's'
return f'{weeks} week{s}'
elif days < 365:
months = days // 30
if months > 1:
s = 's'
return f'{months} month{s}'
else:
years = days // 365
if years > 1:
s = 's'
return f'{years} year{s}'
# Return age in weeks from birthdate
def __age_in_weeks(self, date) -> float:
dob = datetime.strptime(date, "%Y-%m-%d")
days = (datetime.now() - dob).days
return days / 7
# Private function to convert cursor to list of dictionaries
# Source: https://stacktuts.com/how-to-convert-a-pymongo-cursor-cursor-into-a-dict-in-python
def __cursor_to_dict(self, cursor):
result = []
for doc in cursor:
dictionary = dict(doc)
result.append(dictionary)
return result