A hosted version including swagger docs can be found here
Initiate a virtual environament and reference requirements.txt to install necessary project dependencies:
- Create virtual enivorment in the project root folder (/be_parkfinite): In your CLI, navigate to the project root folder and run:
-
python3 -m venv venv
- Activate your virtual environment: In your CLI run:
-
source venv/bin/activate
- Install project dependencies: In your CLI run:
-
pip install -r requirements.txt
- Run the app locally on http://127.0.0.1:8000: In your CLI run:
-
uvicorn main:app
- To execute all avaiable test suites: In your CLI run:
-
pytest
- Ensure your pythonpath is set correctly via an ABSOLTE FILE PATH TO THE PROJECT ROOT FOLDER (/be_parkfinite): In your CLI run:
-
echo $PYTHONPATH
- If there is no set pythonpath then a workaraound is to set it manually:
-
export PYTHONPATH="/path/to/poject/be_parkfinite:$PYTHONPATH"
- To avoid having to run this command every time you activate your venv: add a command to the relevant venv/bin/activate file for the operating system you are on.
In your CLI:
- To run main api test suite, for main.py endpoints:
-
pytest -m main
- To run database utilities test suite:
-
pytest -m db_utils
- To run testing utilities test suite:
-
use 'pytest -m test_utils'
--PLEASE ADD .ENV FILES TO GIT IGNORE TO KEEP THE DATABASES SECURE!!-- --PLEASE ADD ANY LOCALLY STORED SQLITE DATABASE TO GITIGNORE TOO!--
- To create and seed a locally stored SQLite development database add a .env.development file containing the line:
-
DATABASE_URL=sqlite:///../dev.db
-Remember to add this file to .gitignore-
- To seed to the hosted production database add a .env.production file containing the following line:
-
DATABASE_URL=postgresql://PATH/TO/PRODUCTION/DB/INCLUDING/PASSWORD/
In your CLI run:
-
ENV=development python3 seed.py
replace 'development' with 'production' to seed the hosted Postgres production db.
When seeding the database with user data for testing or development, we use pre-hashed passwords to speed up the process. These passwords have been pre-hashed using our utility function and are stored directly in the seed data.
If you need to generate a new pre-hashed password (for example, if you are setting up a new local repository or updating the seed data), follow these steps:
-
Open a Python shell within the project's virtual environment.
-
Run the following commands to generate the hash:
from api.utils.security_utils.password_utils import hash_password print(hash_password('your_password_here')
-
Replace the old pre-hashed password in yourr .env.pre_hashed_user_password environment file or seed script with the newly generated one:
PRE_HASHED_USER_PASSWORD=b'HASHEDPASSWORDHERE'
- Environment File: The pre-hashed password should be stored in an environment file named
.env.pre_hashed_user_passwords
(or another appropriately named.env
file depending on the environment). - Environment Variable: Ensure that the pre-hashed password is assigned to the
PRE_HASHED_USER_PASSWORD
environment variable. - Seeding Script: The seeding script will automatically use the
PRE_HASHED_USER_PASSWORD
environment variable to assign the hashed password to the user records during the seeding process.
In your seeding script, the hashed password is fetched from the environment variable like so:
from api.config.config import PRE_HASHED_USER_PASSWORD
def seed_users(session, users):
for user in users:
user.hashed_password = PRE_HASHED_USER_PASSWORD
session.add(user)
session.commit()
- Ensure that your
.env
files are properly git-ignored to avoid committing sensitive information to the repository. - Before pushing changes, verify that the environment and seed data are correctly set up and tested in your local environment.