This is a simple blog API with AI moderation features, written in python with Django and Django Ninja.
Clone the repo, and cd into it:
git clone https://github.com/SherlockH0/Blog-API.git
cd Blog-APIThen, you can either run the application in testing mode using docker compose, or run the application in development mode.
Make sure you have Docker installed on your system.
Create local settings files:
mkdir -p local
cp blogapi/project/settings/templates/settings.prod.py ./local/settings.prod.pyAnd set a google AI API key in it (you can omit this step, but the AI features won't work):
# local/settings.prod.py
- GOOGLE_AI_API_KEY = NotImplemented
+ GOOGLE_AI_API_KEY = "djjnJIJIJhkh"Run the application:
docker compose upApplication is now available on localhost:8000 (for more routes see API Docs)
Make sure you have Docker and Python ^3.12 installed on your system. I'm also using Poetry as a dependency manager, so if you have it installed on your system, it would be a plus.
Create local settings files:
mkdir -p local
cp blogapi/project/settings/templates/settings.dev.py ./local/settings.dev.py
cp blogapi/project/settings/templates/settings.unittest.py ./local/settings.unittest.pyAnd add set a google AI API key in both of them (.dev file is used for development, and .unittest is used for running tests) (you can omit this step, but the AI features won't work):
# /local/settings.dev.py or /local/settings.unittest.py
- GOOGLE_AI_API_KEY = NotImplemented
+ GOOGLE_AI_API_KEY = "djjnJIJIJhkh"Create and activate virtual environment:
python -m venv venv
# Linux, MacOS, Windows (WSL)
source venv/bin/activate
# Windows
venv\Scripts\activate.batIn a separate terminal window start PostgreSQL and Redis with docker:
docker-compose -f docker-compose.dev.yml up --force-recreateThen, continue depending on your system:
With Makefile and Poetry (Linux, MacOS, Windows (WSL))
Install the project using poetry, migrate the database, and create superuser (optional):
make install
make migrate
make superuserRun local server:
make runserverIn a different terminal window, run rq worker and scheduler:
make rq
make rqschedulerTo make migrations after changes in the models, run:
make migrationsTo migrate, run:
make migrateTo run tests, run:
make test
# With coverage
make test-cov
# With html coverage
make test-cov-htmlWith Poetry (All systems with Poetry installed)
Install the project using poetry, migrate the database, and create superuser (optional):
poetry install
poetry run python -m blogapi.manage migrate
poetry run python -m blogapi.manage createsuperuserRun local server:
poetry run python -m blogapi.manage runserverIn a different terminal window, run rq worker and scheduler:
poetry run python -m blogapi.manage rqworker default
poetry run python -m blogapi.manage rqschedulerTo make migrations after changes in the models, run:
poetry run python -m blogapi.manage makemigrationsTo migrate, run:
poetry run python -m blogapi.manage migrateTo run tests, run:
poetry run pytest -v -rs
# With coverage
poetry run pytest -v -rs --cov
# With html coverage
poetry run pytest -v -rs --cov --cov-report htmlWith pip (All systems)
Install the project using pip, migrate the database, and create superuser (optional):
pip install .
python -m blogapi.manage migrate
python -m blogapi.manage createsuperuserRun local server:
python -m blogapi.manage runserverIn a different terminal window, run rq worker and scheduler:
python -m blogapi.manage rqworker default
python -m blogapi.manage rqschedulerTo run tests, run:
pytest -v -rs
# With coverage
pytest -v -rs --cov
# With html coverage
pytest -v -rs --cov --cov-report htmlDjango-Ninja comes with an easy to use interactive API documentation. If you have launched the application, you can check it out on localhost:8000/api/docs.
Django also comes with a featureful admin panel which you can use by visiting localhost:8000/admin (you have to create superuser to use it)
Blog-API implements a simple JWT authentication.
To create a user, make a POST request to the /api/users route.
To log user in, obtain access and refresh tokens by making a POST request to the api/token/pair route.
To access protected routes, add a Authorization header into your request:
Authorization: Bearer <token>
