Skip to content

Commit ce1a9c7

Browse files
committed
✨(tycho) add base Django project
Bootstrap an almost pristine Django project with Docker integration. - Integrate Honcho as we plan to deploy this project using Scalingo - Integrate Django environ to support multiple environment settings using environment variables - Integrate Sentry - Integrate PostgreSQL - Run using Gunicorn
1 parent 70025d0 commit ce1a9c7

27 files changed

+1100
-8
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ share/python-wheels/
2929
*.egg
3030
MANIFEST
3131

32-
# Installed pipenv packages
32+
# Installed packages
3333
.cache
3434
.local
3535

@@ -59,3 +59,4 @@ scripts/*.csv
5959

6060
.todo*
6161
.pre-commit-cache/
62+
.coverage

Makefile

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
SHELL := /bin/bash
33

44
# -- Docker
5-
COMPOSE = bin/compose
6-
COMPOSE_UP = $(COMPOSE) up -d --remove-orphans
7-
COMPOSE_RUN = $(COMPOSE) run --rm --no-deps
8-
COMPOSE_RUN_DEV_UV = $(COMPOSE_RUN) dev uv run --project dev
5+
COMPOSE = bin/compose
6+
COMPOSE_UP = $(COMPOSE) up -d --remove-orphans
7+
COMPOSE_RUN = $(COMPOSE) run --rm --no-deps
8+
COMPOSE_RUN_DEV_UV = $(COMPOSE_RUN) dev uv run
9+
COMPOSE_RUN_TYCHO_UV = $(COMPOSE_RUN) tycho uv run
910

1011
default: help
1112

@@ -23,6 +24,8 @@ default: help
2324
bootstrap: ## setup development environment (build dev service and install git hooks)
2425
bootstrap: \
2526
build \
27+
migrate \
28+
create-superuser \
2629
jupytext--to-ipynb
2730
.PHONY: bootstrap
2831

@@ -46,6 +49,10 @@ build-notebook: ## build custom jupyter notebook image
4649
@$(COMPOSE) build notebook
4750
.PHONY: build-notebook
4851

52+
build-tycho: ## build tycho image
53+
@$(COMPOSE) build tycho
54+
.PHONY: build-tycho
55+
4956
jupytext--to-md: ## convert local ipynb files into md
5057
bin/jupytext --to md **/*.ipynb
5158
.PHONY: jupytext--to-md
@@ -63,6 +70,21 @@ logs-notebook: ## display notebook logs (follow mode)
6370
@$(COMPOSE) logs -f notebook
6471
.PHONY: logs-notebook
6572

73+
logs-tycho: ## display tycho logs (follow mode)
74+
@$(COMPOSE) logs -f tycho
75+
.PHONY: logs-tycho
76+
77+
### Setup
78+
migrate: ## migrate tycho database
79+
@echo "Migrating tycho database…"
80+
@bin/manage migrate
81+
.PHONY: migrate
82+
83+
create-superuser: ## create tycho super user
84+
@echo "Creating tycho super user…"
85+
@bin/manage createsuperuser --noinput || true
86+
.PHONY: create-superuser
87+
6688
### RUN
6789
run-all: ## run the whole stack
6890
run-all: \
@@ -78,16 +100,22 @@ run-es: ## run the elasticsearch service
78100
$(COMPOSE_UP) elasticsearch
79101
.PHONY: run-es
80102

103+
run-tycho: ## run the tycho service
104+
$(COMPOSE_UP) tycho
105+
.PHONY: run-tycho
106+
81107
## LINT
82108
# -- Global linting
83109
lint: ## lint all sources
84110
lint: \
85-
lint-notebook
111+
lint-notebook \
112+
lint-tycho
86113
.PHONY: lint
87114

88115
lint-fix: ## lint and fix all sources
89116
lint-fix: \
90-
lint-notebook-fix
117+
lint-notebook-fix \
118+
lint-tycho-fix
91119
.PHONY: lint-fix
92120

93121
# -- Per-service linting
@@ -103,7 +131,41 @@ lint-notebook-fix: ## lint and fix notebook python sources
103131
$(COMPOSE_RUN_DEV_UV) ruff format src/notebook/
104132
.PHONY: lint-notebook-fix
105133

106-
### MANAGE docker services
134+
lint-tycho: ## lint tycho python sources
135+
lint-tycho: \
136+
lint-tycho-ruff \
137+
lint-tycho-mypy
138+
.PHONY: lint-tycho
139+
140+
lint-tycho-ruff: ## lint tycho python sources with ruff
141+
@echo 'lint:tycho-ruff started…'
142+
$(COMPOSE_RUN_TYCHO_UV) ruff check .
143+
$(COMPOSE_RUN_TYCHO_UV) ruff format --check .
144+
.PHONY: lint-tycho-ruff
145+
146+
lint-tycho-ruff-fix: ## lint and fix tycho python sources with ruff
147+
@echo 'lint:tycho-ruff-fix started…'
148+
$(COMPOSE_RUN_TYCHO_UV) ruff check --fix .
149+
$(COMPOSE_RUN_TYCHO_UV) ruff format .
150+
.PHONY: lint-tycho-ruff-fix
151+
152+
lint-tycho-mypy: ## lint tycho python sources with mypy
153+
@echo 'lint:tycho-mypy started…'
154+
$(COMPOSE_RUN_TYCHO_UV) mypy .
155+
.PHONY: lint-tycho-mypy
156+
157+
## TEST
158+
test: ## test all services
159+
test: \
160+
test-tycho
161+
.PHONY: test
162+
163+
test-tycho: ## test tycho python sources
164+
@echo 'test:tychostarted…'
165+
$(COMPOSE_RUN_TYCHO_UV) pytest
166+
.PHONY: test-tycho
167+
168+
## MANAGE docker services
107169
status: ## an alias for "docker compose ps"
108170
@$(COMPOSE) ps
109171
.PHONY: status

bin/manage

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env bash
2+
3+
set -eo pipefail
4+
5+
declare DOCKER_USER
6+
DOCKER_UID="$(id -u)"
7+
DOCKER_GID="$(id -g)"
8+
DOCKER_USER="${DOCKER_UID}:${DOCKER_GID}"
9+
10+
DOCKER_USER=${DOCKER_USER} \
11+
DOCKER_UID=${DOCKER_UID} \
12+
DOCKER_GID=${DOCKER_GID} \
13+
docker compose run -it \
14+
tycho \
15+
uv run python manage.py "$@"

bin/pytest

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env bash
2+
3+
set -eo pipefail
4+
5+
declare TEST=true
6+
declare service=${SERVICE:-tycho}
7+
declare cmd="uv run pytest"
8+
declare volume="./src/${service}:/app"
9+
declare DOCKER_USER
10+
DOCKER_UID="$(id -u)"
11+
DOCKER_GID="$(id -g)"
12+
DOCKER_USER="${DOCKER_UID}:${DOCKER_GID}"
13+
14+
15+
DOCKER_USER=${DOCKER_USER} \
16+
DOCKER_UID=${DOCKER_UID} \
17+
DOCKER_GID=${DOCKER_GID} \
18+
docker compose run --rm \
19+
-e TEST="${TEST}" \
20+
-v "${volume}" \
21+
"${service}" \
22+
${cmd} "$@"

bin/uv

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
3+
set -eo pipefail
4+
5+
declare service="${SERVICE:-tycho}"
6+
declare volume="./src/${service}:/app"
7+
declare DOCKER_USER
8+
DOCKER_UID="$(id -u)"
9+
DOCKER_GID="$(id -g)"
10+
DOCKER_USER="${DOCKER_UID}:${DOCKER_GID}"
11+
12+
DOCKER_USER=${DOCKER_USER} \
13+
DOCKER_UID=${DOCKER_UID} \
14+
DOCKER_GID=${DOCKER_GID} \
15+
docker compose run --rm --no-deps \
16+
-u "uv:uv" \
17+
-v "${volume}" \
18+
"${service}" \
19+
uv "$@"

docker-compose.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,36 @@ services:
4545
test: curl --fail http://localhost:9200/_cluster/health?wait_for_status=green || exit 1
4646
interval: 1s
4747
retries: 60
48+
49+
postgresql:
50+
image: postgres:16.10
51+
env_file:
52+
- env.d/postgresql
53+
- env.d/tycho
54+
healthcheck:
55+
test:
56+
- "CMD-SHELL"
57+
- "pg_isready"
58+
- "-d"
59+
- "$${POSTGRES_DB}"
60+
interval: 10s
61+
timeout: 5s
62+
retries: 5
63+
64+
tycho:
65+
container_name: tycho
66+
build:
67+
context: ./src/tycho
68+
args:
69+
DOCKER_UID: ${DOCKER_UID:-1000}
70+
DOCKER_GID: ${DOCKER_GID:-1000}
71+
user: ${DOCKER_USER:-1000}
72+
ports:
73+
- "8000:8000"
74+
env_file:
75+
- env.d/tycho
76+
volumes:
77+
- ./src/tycho:/app
78+
depends_on:
79+
postgresql:
80+
condition: service_healthy

env.d/postgresql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
POSTGRES_DB=tycho
2+
POSTGRES_USER=tycho
3+
POSTGRES_PASSWORD=pass

env.d/tycho

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
PORT=8000
2+
3+
# Django
4+
TYCHO_DEBUG=1
5+
TYCHO_ALLOWED_HOSTS=*
6+
TYCHO_SECRET_KEY=the_secret_key
7+
8+
# database
9+
TYCHO_DATABASE_URL=psql://tycho:pass@postgresql:5432/tycho
10+
11+
# Third-party integrations
12+
TYCHO_SENTRY_DSN=
13+
TYCHO_SENTRY_TRACES_SAMPLE_RATE=1.0
14+
TYCHO_SENTRY_PROFILES_SAMPLE_RATE=1.0
15+
16+
# superuser
17+
DJANGO_SUPERUSER_USERNAME=admin
18+
DJANGO_SUPERUSER_PASSWORD=admin
19+
DJANGO_SUPERUSER_EMAIL=admin@example.com

src/tycho/.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.12

src/tycho/CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a
6+
Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to
7+
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).
8+
9+
## [Unreleased]
10+
11+
[unreleased]: https://github.com/betagouv/csplab/

0 commit comments

Comments
 (0)