t04st3r / django-meilisearch

Simple Django app to interact with Meilisearch

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CircleCI python Ruff

django-meilisearch

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

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

Your .env file should be something like this

#django
ENV=dev
DJANGO_DEBUG=True
SECRET_KEY='supersecretkey'

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

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

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.

Once fetched some public holidays you can create/update your index on meilisearch by running the following command

make populate-meilisearch-index

The public_holiday view will expose a search endpoint that is testable by running the server and connect to public_holiday/search/ path or via the swagger UI (/api/schema/swagger-ui/).

The endpoint will accept the following query parameters:

  • q will perform full text search among all the document fields
  • fields is a list param that will select which fields to include in the document search response (something like graphQL feature)
  • sort will sort results by the field specified in this param (prefixing the param value with a dash will sort results in descending order)

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 app to interact with Meilisearch

License:Apache License 2.0


Languages

Language:Python 97.4%Language:Makefile 1.8%Language:Dockerfile 0.8%