Since the government is now officially acknowledging the existence of Unexplained Arial Phenomena (UFOs), it is creating a portal for citizens to report sightings of UAPs, and you have been hired to set up the backend. You will create a simple Flask app for the Unexplained Arial Phenomena Investigation Division (UAPID) to their specifications using your knowledge of the request-response cycle, GET HTTP requests, status codes, Flask, SQLAlchemy, dynamic routes, and request hooks.
- Initialize a new Flask app in the
app.py
file. - Create a route for the home page (
/
) that returns a welcome message in JSON format. Use the@app.route
decorator and themake_response
function from the flask module. The welcome message should be a string that says "The UAPID welcome our new extraterrestrial overlords!" and response should include a status code of 200.
- Configure the app to use a SQLite database.
- Create an instance of the SQLAlchemy class in
models.py
and import it intoapp.py
. - Initialize Flask-Migrate with the app and the database instance.
- Initialize the database with the app.
- Create a model called "Sighting" in
models.py
with the following columns:- id (Integer, primary key, autoincrement)
- date (String)
- time (String)
- location (String)
- shape_of_craft (String)
- approximate_size (Integer)
- approximate_speed (Integer)
- description (String)
- reporter (String)
- reporter_reliable_witness (Boolean)
- 5b. Optional: add a
__repr__
method to the Sighting model that returns a string representation of the model instance.
- Make the Sighting model inherit SerializerMixin.
- Create a migration and upgrade the database.
- Create at least 3 seed sightings in
seeds.py
and run the seeds file to populate the sightings table.
- Create a route (
/sightings
) that returns all sightings in the database in JSON format. - Use the
@app.route
decorator and themake_response
function from the flask module. - Use the Sighting model to query the database for all sightings.
- Create a list of dictionaries from the query results using the
to_dict
method from the SerializerMixin class in a list comprehension. - Return the list of dictionaries as a response in JSON format with a status code of 200.
- Create a route (
/sightings/<int:id>
) that returns a single sighting by its id in JSON format. - Use the
@app.route
decorator and themake_response
function from the flask module. - Use the Sighting model to query the database for a single sighting by its id.
- Return the sighting (converted to a dictionary) as a response in JSON format with a status code of 200.
- Create a route (
/sightings/location/<string:location>
) that returns all sightings in the database by location in JSON format. - Use the
@app.route
decorator and themake_response
function from the flask module. - Use the Sighting model to query the database for all sightings by location.
- Create a list of dictionaries from the query results using the
to_dict
method from the SerializerMixin class in a list comprehension. - Return the list of dictionaries as a response in JSON format with a status code of 200.
Use the @app.before_request
decorator to create a function that logs the total number of sightings in the database to the console before each request. The log should be in the format "Total sightings: ".
Create a route that returns all sightings in the database by location and date in JSON format. The route will be in the format /sightings/location/<string:location>/date/<string:date>
.