-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtesting_doggos.py
331 lines (275 loc) · 9.75 KB
/
testing_doggos.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
"""
This file contains the implementation of a Flask application for managing an animal shelter.
It defines the routes for handling HTTP requests,
as well as the models for animals, food, and shelters.
"""
import json
from socket import gethostname
from flask import Flask, request, jsonify, render_template
from flask_sqlalchemy import SQLAlchemy
import os
app = Flask(__name__)
# Configure your SQL database URI
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///animal_shelter.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class Animal(db.Model):
"""Model class for animals in the animal shelter."""
id = db.Column(db.Integer, primary_key=True)
food_id = db.Column(db.Integer, db.ForeignKey('food.id'), nullable=False)
shelter_id = db.Column(
db.Integer,
db.ForeignKey('shelter.id'),
nullable=False)
name = db.Column(db.String(50), nullable=False)
race = db.Column(db.String(50), nullable=False)
age = db.Column(db.Integer)
weight = db.Column(db.Float)
status = db.Column(db.String(50), nullable=False)
sex = db.Column(db.String(50), nullable=False)
def __init__(
self,
name,
race,
food_id,
shelter_id,
status,
sex,
age=None,
weight=None):
self.name = name
self.race = race
self.food_id = food_id
self.shelter_id = shelter_id
self.status = status
self.sex = sex
self.age = age
self.weight = weight
def to_dict(self):
"""Converts the Animal object to a dictionary."""
return {
'id': self.id,
'food_id': self.food_id,
'shelter_id': self.shelter_id,
'name': self.name,
'race': self.race,
'age': self.age,
'weight': self.weight,
'status': self.status,
'sex': self.sex
}
def update(self, data):
"""Updates the Animal object with new data."""
if isinstance(data, str):
data = json.loads(data)
self.name = data['name']
self.race = data['race']
self.food_id = data['food_id']
self.shelter_id = data['shelter_id']
self.status = data['status']
self.sex = data['sex']
self.age = data['age']
self.weight = data['weight']
class Food(db.Model):
"""Model class for animal food in the animal shelter."""
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)
amount = db.Column(db.Integer, nullable=False)
weight = db.Column(db.Float, nullable=False)
def __init__(self, name, amount, weight):
self.name = name
self.amount = amount
self.weight = weight
def to_dict(self):
"""Converts the Food object to a dictionary."""
return {
'id': self.id,
'name': self.name,
'amount': self.amount,
'weight': self.weight
}
def update(self, data):
"""Updates the Food object with new data."""
if isinstance(data, str):
data = json.loads(data)
self.name = data['name']
self.amount = int(data['amount'])
self.weight = float(data['weight'])
class Shelter(db.Model):
"""Model class for spaces in a shelter in the animal shelter."""
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)
amount = db.Column(db.Integer, nullable=False)
capacity = db.Column(db.Integer, nullable=False)
def __init__(self, name, amount, capacity):
self.name = name
self.amount = amount
self.capacity = capacity
def to_dict(self):
"""Converts the Shelter object to a dictionary."""
return {
'id': self.id,
'name': self.name,
'amount': self.amount,
'capacity': self.capacity
}
def update(self, data):
"""Updates the Shelter object with new data."""
if isinstance(data, str):
data = json.loads(data)
self.name = data['name']
self.amount = data['amount']
self.capacity = data['capacity']
def get_available_images():
image_folder = './static/images/Animals/'
available_images = set()
for filename in os.listdir(image_folder):
if filename.endswith('.jpeg'):
available_images.add(filename)
return available_images
@app.route('/')
def home():
'''Renders the home page. With the list of animals and available images.'''
animals = Animal.query.all()
available_images = get_available_images()
return render_template(
'index.html',
animals=animals,
available_images=available_images)
@app.route('/animals', methods=['GET', 'POST'])
def handle_animals():
"""Handles GET and POST requests for the animals endpoint."""
if request.method == 'POST':
data = request.get_json()
new_animal = Animal(name=data['name'],
race=data['race'],
food_id=data['food_id'],
shelter_id=data['shelter_id'],
status=data['status'],
sex=data['sex'],
age=data.get('age'),
weight=data.get('weight'))
db.session.add(new_animal)
db.session.commit()
return jsonify(new_animal.to_dict()), 201
if request.method == 'GET':
animals = Animal.query.all()
return jsonify([animal.to_dict() for animal in animals])
return '', 405
@app.route('/animals/<int:animal_id>', methods=['GET', 'PUT', 'DELETE'])
def handle_animal(animal_id):
"""Handles GET, PUT, and DELETE requests for a specific animal."""
animal = Animal.query.get_or_404(animal_id)
if request.method == 'GET':
return jsonify(animal.to_dict())
if request.method == 'PUT':
data = request.get_json()
animal.update(data)
db.session.commit()
return jsonify(animal.to_dict())
if request.method == 'DELETE':
db.session.delete(animal)
db.session.commit()
return '', 204
return '', 405
@app.route('/foods', methods=['GET', 'POST'])
def handle_foods():
"""Handles GET and POST requests for the foods endpoint."""
if request.method == 'POST':
data = request.get_json()
new_food = Food(name=data['name'],
amount=data['amount'],
weight=data['weight'])
db.session.add(new_food)
db.session.commit()
return jsonify(new_food.to_dict()), 201
if request.method == 'GET':
foods = Food.query.all()
return jsonify([food.to_dict() for food in foods])
return '', 405
@app.route('/foods/<int:food_id>', methods=['GET', 'PUT', 'DELETE'])
def handle_food(food_id):
"""Handles GET, PUT, and DELETE requests for a specific food."""
food = Food.query.get_or_404(food_id)
if request.method == 'GET':
return jsonify(food.to_dict())
if request.method == 'PUT':
data = request.get_json()
food.update(data)
db.session.commit()
return jsonify(food.to_dict())
if request.method == 'DELETE':
db.session.delete(food)
db.session.commit()
return '', 204
return '', 405
@app.route('/shelters', methods=['GET', 'POST'])
def handle_shelters():
"""Handles GET and POST requests for the shelters endpoint."""
if request.method == 'POST':
data = request.get_json()
new_shelter = Shelter(name=data['name'],
amount=data['amount'],
capacity=data['capacity'])
db.session.add(new_shelter)
db.session.commit()
return jsonify(new_shelter.to_dict()), 201
if request.method == 'GET':
shelters = Shelter.query.all()
return jsonify([shelter.to_dict() for shelter in shelters])
return '', 405
@app.route('/shelters/<int:shelter_id>', methods=['GET', 'PUT', 'DELETE'])
def handle_shelter(shelter_id):
"""Handles GET, PUT, and DELETE requests for a specific shelter."""
shelter = Shelter.query.get_or_404(shelter_id)
if request.method == 'GET':
return jsonify(shelter.to_dict())
if request.method == 'PUT':
data = request.get_json()
shelter.update(data)
db.session.commit()
return jsonify(shelter.to_dict())
if request.method == 'DELETE':
db.session.delete(shelter)
db.session.commit()
return '', 204
return '', 405
@app.route('/support_us')
def support_us():
"""Renders the support us page."""
return render_template('support_us.html')
@app.route('/manage_animals')
def manage_animals():
"""Renders the manage animals page."""
return render_template('manage_animals.html')
@app.route('/manage_foods')
def manage_foods():
"""Renders the manage foods page."""
return render_template('manage_foods.html')
@app.route('/manage_shelters')
def manage_shelters():
"""Renders the manage shelters page."""
return render_template('manage_shelters.html')
@app.route('/error')
def error():
"""Renders the error popup."""
return render_template('error.html')
@app.route('/info')
def info():
"""Renders the info popup."""
return render_template('info.html')
@app.route('/warn')
def warning():
"""Renders the warning popup."""
return render_template('warn.html')
@app.route('/manage_food')
def manage_food():
"""Returns the HTML content of the add food form."""
return render_template('manage_food.html')
if __name__ == '__main__':
with app.app_context():
db.create_all()
if 'liveconsole' not in gethostname():
app.run()
else:
app.run(debug=True)