Flask API Using MethodView
For this assignment you will extend a Flask REST API implementation that uses a new Flask feature, the MethodView
class.
The file dogs_api_views.py
has starter code which implements the same API as the starter code for the last assignment, except it has added support to delete a dog. Watch this video for my discussion of this code.
NOTE: There is a typo in the post()
method of BreedsView
. It should be request.form['breed']
, not request.form['name']
.
The provided dog_db.py
file is the same as the one provided for the last assignment, except it has a delete_dog()
method for deleting a dog:
def delete_dog(self, dog_id):
"""
Delete the dog with the given primary key.
:param dog_id: primary key of the dog
"""
cur = self.conn.cursor()
query = 'DELETE FROM dog WHERE dog_id = ?'
cur.execute(query, (dog_id,))
self.conn.commit()
For this assignment I am asking you to again add support for owners to this new codebase, and also add support for deleting breeds and owners. I recommend copying over your dog_db.py
from the last assignment to the repository for this assignment, and adding the delete_dog()
method shown above to your code. Then you can add delete_breed()
and delete_owner()
.
Note that you may have to delete dogs when you delete breeds and owners. If you are deleting an owner by ID, you must first delete all of the dogs that have that owner_id
, to maintain the foreign key constraint.
In dogs_api_views.py
, adapt the code that you wrote for owners in dogs_api.py
for the last assignment so that it uses a MethodView
class instead of decorated functions. Then, add delete()
methods for both the breed and the owner MethodView
classes.
Requirements Summary
Here is a more condensed list of what I am asking you to do:
- Add methods to the
DogDatabase
class to support owners, and to support deleting dogs, owners, and breeds. Feel free to re-use code that you created for the last assignment, and to use mydelete_dog()
meethod. - Add a
MethodView
class todogs_api_views.py
for owners requests. Adapt your Flask code that deals with owners from the last assignment to use withMethodView
. Add support for deleting owners, and deleting breeds.
Push your code back to git-keeper to submit.