A REST API which allows users track their mood.
- Allows users to Login to their account, and privately submit their current mood to a persistent database. Users can view their mood entries over time.
- Users who submit at least 1 mood rating daily will maintain a "streak" (a measure of consecutive days in which they submitted to the app).
- The user's streak percentile compared to other users will be tracked, and if a user is in the 50% percentile, it will be retrievable
- The development server for the application is containerized using Docker
$ pip install -r requirements.txt
$ export FLASK_APP=mood_tracker.py
$ export FLASK_ENV=development
$ flask db init
$ flask db migrate
$ flask db upgrade
$ flask run
* Environment: development
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 277-572-647
Environment variables have been instantiated in mood_tracker.py
@app.shell_context_processor
def make_shell_context():
"""
Establishes necessary context to test server in the python shell.
Execute by typing `flask shell`
"""
return {"db": db, "User": User, "MoodEntry": MoodEntry}
A python shell can be run with these environment variables pre-imported by executing:
$ flask shell
$ pytest
============================= test session starts ==============================
platform darwin -- Python 3.7.2, pytest-4.6.2, py-1.8.0, pluggy-0.12.0
collected 21 items
tests/test_helpers.py ............ [ 57%]
tests/test_models.py ......... [100%]
========================== 21 passed in 0.51 seconds ===========================
--Docker can be installed from the Docker website.
$ docker build -t moodtracker:latest .
$ docker run --name moodtracker --rm -p 5000:5000 --rm moodtracker:latest
-- Follow the link for Auto generated Postman Docs
-- This confirms that the server is active.
http://localhost:5000/health
{
"msg": "Flask is up and running!"
}
-- This returns information about the active user.
http://localhost:5000/getuser
{
"msg": "<User DanYoung> is active.",
"user_id": "2"
}
-- This creates a new user and stores it in the database.
http://localhost:5000/mood
{
"username": "DanYoung",
"email": "[email protected]",
"password": "testpass1"
}
{
"User": {
"best_streak": 0,
"current_streak": 0,
"email": "[email protected]",
"id": 2,
"username": "DanYoung"
},
"msg": "Successfully created user with username DanYoung"
}
-- This logs the user out.
http://localhost:5000/logout
-- This route allows users to post a mood score (int between 1 and 10) to the database.
http://localhost:5000/mood
{
"mood_score": 2
}
{
"id": 10,
"mood_score": 2,
"timestamp": "Jun 08 2019 20:14:37",
"user_id": 2
}
-- This returns a list off all the mood scores posted by a user, as well as their best streak, current streak, and percentile rank compare to other users (if greater than 50).
http://localhost:5000/mood
{
"best_streak": 8,
"current_streak": 5,
"mood_entries": [
{
"id": 8,
"mood_score": 2,
"timestamp": "Jun 08 2019 20:14:37",
"user_id": 2
},
{
"id": 9,
"mood_score": 2,
"timestamp": "Jun 08 2019 20:14:37",
"user_id": 2
},
{
"id": 10,
"mood_score": 2,
"timestamp": "Jun 08 2019 20:14:37",
"user_id": 2
}
],
"percentile": 50
}