t04st3r / django-celery

Simple Django integration project with celery

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CircleCI python Ruff

django-celery

This is a sample project used as a base for django related projects.

Install

To run django locally you need python version to 3.11.3 and pipenv, you can use pyenv to set your desired python version.

Run containers

Django needs postgres and redis container to be up and running, to do so clone the project, enter in the project folder and hit:

docker compose up postgres redis maildev celery

Install django deps and run django server

inside the project folder make sure you use python 3.11.3

python --version

you should see

Python 3.11.3

install requirements (listed inside Pipfile) by

pipenv install

To run tests you will need also dev dependencies

pipenv install --dev

Create a .env file using the .env.example file and changing the url to be localhost in the following env variables:

POSTGRES_HOST
REDIS_URL
EMAIL_HOST
CELERY_BROKER_URL
CELERY_RESULT_BACKEND

Your .env file should be something like this

#django
ENV=ci
DJANGO_DEBUG=True
SECRET_KEY='supersecretkey'
EMAIL_HOST=localhost
EMAIL_PORT=1025
EMAIL_HOST_USER=dev
EMAIL_HOST_PASSWORD=dev

#db
POSTGRES_USER=postgres
POSTGRES_DB=app_db
POSTGRES_HOST=localhost
POSTGRES_PASSWORD=password
POSTGRES_PORT=5432

#redis
REDIS_URL=redis://localhost:6379/0

#celery
CELERY_BROKER_URL=redis://localhost:6379
CELERY_RESULT_BACKEND=redis://localhost:6379

Once done enter inside your pipenv virtual environment by

pipenv shell

Run migrations with

python manage.py migrate

Or (if you have make installed) by

make migrate

Everything is done! You can now start the server with

python manage.py runserver

Or

make run-dev

How does it works

This apps gather data from Public Holiday API via a django command, you can populate PublicHoliday models with

python manage.py populate_models

Or

make populate-models

For each command run a random country is selected and all the public holidays for that country would be fetched and stored in the db.

Anytime a PublicHoliday model object is created/updated a signal is dispatched that, in turn, will schedule a celery task that will basically send an email with the JSON representation of the object. The email are not really sent but will be collected by the maildev container.

You can test this flow by trying to change a property, or create a new model via the django admin. In order to login in the django admin you need to create a superuser. It can be done by hitting:

python manage.py createsuperuser

To test the correct celery task execution you can connect to the maildev web interface at the url:

http://localhost:1080

You should see the mail being sent from the celery task (see also public_holiday.tasks.py and public_holiday.signals.py)

Testing

Testing requirements can be installed by

pipenv install --dev

To run the ruff linter on your codebase hit

make lint

pre-commit (that in turn will run ruff check) can be installed as well with

pre-commit install

You can run django tests by simply run

pytest

To simulate the testing pipeline in CircleCI just run

docker compose run django ci

About

Simple Django integration project with celery

License:Apache License 2.0


Languages

Language:Python 95.3%Language:Makefile 3.3%Language:Dockerfile 1.4%