add to function
This commit is contained in:
59
app/AnimalShelter.py
Normal file
59
app/AnimalShelter.py
Normal file
@@ -0,0 +1,59 @@
|
||||
from pymongo import MongoClient
|
||||
from bson.objectid import ObjectId
|
||||
class AnimalShelter(object):
|
||||
""" CRUD operations for Animal collection in MongoDB """
|
||||
|
||||
def __init__(self):
|
||||
# Initializing the MongoClient. This helps to
|
||||
# access the MongoDB databases and collections.
|
||||
# This is hard-wired to use the aac database, the
|
||||
# animals collection, and the aac user.
|
||||
# Definitions of the connection string variables are
|
||||
# unique to the individual Apporto environment.
|
||||
#
|
||||
# You must edit the connection variables below to reflect
|
||||
# your own instance of MongoDB!
|
||||
#
|
||||
# Connection Variables
|
||||
#
|
||||
USER = 'aacuser'
|
||||
PASS = 'Ex9Ai3Y2eP'
|
||||
HOST = 'db'
|
||||
PORT = 27017
|
||||
DB = 'AAC'
|
||||
COL = 'animals'
|
||||
#
|
||||
# Initialize Connection
|
||||
#
|
||||
self.client = MongoClient('mongodb://%s:%s@%s:%d' % (USER,PASS,HOST,PORT))
|
||||
self.database = self.client['%s' % (DB)]
|
||||
self.collection = self.database['%s' % (COL)]
|
||||
|
||||
# Complete this create method to implement the C in CRUD.
|
||||
def create(self, *args, **kwargs):
|
||||
data = kwargs.get('data', None)
|
||||
if data is not None:
|
||||
try:
|
||||
self.database.animals.insert_one(data) # data should be dictionary
|
||||
except Exception as e:
|
||||
raise Exception(f'Error: {e}')
|
||||
else:
|
||||
raise Exception("Nothing to save, because data parameter is empty")
|
||||
|
||||
# Create method to implement the R in CRUD.
|
||||
def read(self, *args, **kwargs):
|
||||
criteria = kwargs.get('criteria', None)
|
||||
find_one = kwargs.get('once', False)
|
||||
|
||||
if find_one:
|
||||
try:
|
||||
data = self.collection.find_one(criteria)
|
||||
return data
|
||||
except Exception as e:
|
||||
raise Exception(f"Error: {e}")
|
||||
else:
|
||||
try:
|
||||
data = self.collection.find(criteria)
|
||||
return data
|
||||
except Exception as e:
|
||||
raise Exception(f"Error: {e}")
|
29
app/main.py
Normal file
29
app/main.py
Normal file
@@ -0,0 +1,29 @@
|
||||
from AnimalShelter import AnimalShelter
|
||||
test = AnimalShelter()
|
||||
|
||||
test_criteria = {"breed": "Pit Bull Mix"}
|
||||
test_data = test.read(criteria=test_criteria)
|
||||
|
||||
for object in test_data:
|
||||
print(object)
|
||||
|
||||
test_data = {
|
||||
'rec_num': 10001,
|
||||
'age_upon_outcome': '3 years',
|
||||
'animal_id': 'FA69420',
|
||||
'animal_type': 'Furry',
|
||||
'breed': 'Protogen',
|
||||
'color': 'Purple',
|
||||
'date_of_birth': '1999-12-25',
|
||||
'datetime': '2019-10-11 10:03:00',
|
||||
'monthyear': '2019-10-11T10:03:00',
|
||||
'name': '*Brex',
|
||||
'outcome_subtype': 'Foster',
|
||||
'outcome_type': 'Adoption',
|
||||
'sex_upon_outcome': 'Male i think',
|
||||
'location_lat': 30.7118766251975,
|
||||
'location_long': -97.2999365773623,
|
||||
'age_upon_outcome_in_weeks': 558.059821428571
|
||||
}
|
||||
|
||||
test.create(test_data)
|
1
app/requirements.txt
Normal file
1
app/requirements.txt
Normal file
@@ -0,0 +1 @@
|
||||
pymongo
|
Reference in New Issue
Block a user