Skip to content

Commit e1a3dd6

Browse files
authored
Merge pull request #156 from sandiegopython/davidfischer/Jer-Pha-organizers
Database driven organizers page
2 parents 1de8add + cb827a7 commit e1a3dd6

File tree

23 files changed

+355
-24
lines changed

23 files changed

+355
-24
lines changed

.env/local.sample

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,35 @@
11
# This is a sample of environment variables which are used only to run Docker locally.
22
# These are never used in production.
33

4+
# Django
5+
# ------------------------------------------------------------------------------
6+
# Run Django in production mode (DEBUG=False)
7+
DJANGO_SETTINGS_MODULE=config.settings.prod
8+
49
# Use a strong secret in production
510
SECRET_KEY="this-is-a-bad-secret"
611

7-
# In production, we use postgres but for testing a deployment, using SQLite is fine
8-
DATABASE_URL="sqlite:///db.sqlite3"
12+
13+
# PostgreSQL
14+
# ------------------------------------------------------------------------------
15+
# This must match .env/postgres
16+
DATABASE_URL=pgsql://localuser:localpass@postgres:5432/sandiegopython
17+
18+
19+
# Redis
20+
# ------------------------------------------------------------------------------
21+
REDIS_URL=redis://redis:6379/0
22+
23+
24+
# S3/R2 Media Storage
25+
# ------------------------------------------------------------------------------
26+
# If not empty, S3/R2 will be used for media storage
27+
AWS_S3_ACCESS_KEY_ID=
28+
AWS_S3_SECRET_ACCESS_KEY=
29+
AWS_STORAGE_BUCKET_NAME=
30+
# If using a custom domain for media storage, set the MEDIA_URL
31+
# and AWS_S3_CUSTOM_DOMAIN
32+
AWS_S3_CUSTOM_DOMAIN=
33+
MEDIA_URL=/media/
34+
# The endpoint URL is necessary for Cloudflare R2
35+
AWS_S3_ENDPOINT_URL=

.env/postgres

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# PostgreSQL
2+
# ------------------------------------------------------------------------------
3+
POSTGRES_HOST=postgres
4+
POSTGRES_PORT=5432
5+
POSTGRES_DB=sandiegopython
6+
POSTGRES_USER=localuser
7+
POSTGRES_PASSWORD=localpass

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ dmypy.json
131131
############################################
132132
/node_modules/
133133
/staticfiles/
134-
/pythonsd/media/
134+
/media/
135135
/pythonsd/static/css/
136136
/GIT_COMMIT
137137
/BUILD_DATE

Dockerfile

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,24 @@ RUN apt-get install -y --no-install-recommends \
2020
make \
2121
build-essential \
2222
g++ \
23+
postgresql-client \
2324
git
2425

2526
RUN mkdir -p /code
2627

2728
WORKDIR /code
2829

29-
COPY . /code/
30+
# Requirements are installed here to ensure they will be cached.
31+
# https://docs.docker.com/build/cache/#use-the-dedicated-run-cache
32+
COPY ./requirements /requirements
33+
RUN pip install --upgrade pip
34+
RUN --mount=type=cache,target=/root/.cache/pip pip install -r /requirements/deployment.txt
35+
RUN --mount=type=cache,target=/root/.cache/pip pip install -r /requirements/local.txt
3036

31-
# Cache dependencies when building which should result in faster docker builds
32-
RUN --mount=type=cache,target=/root/.cache/pip set -ex && \
33-
pip install --upgrade --no-cache-dir pip && \
34-
pip install -r /code/requirements.txt && \
35-
pip install -r /code/requirements/local.txt
37+
COPY . /code/
3638

3739
# Build JS/static assets
38-
RUN npm install
40+
RUN --mount=type=cache,target=/root/.npm npm install
3941
RUN npm run build
4042

4143
RUN python manage.py collectstatic --noinput
@@ -52,4 +54,4 @@ RUN date -u +'%Y-%m-%dT%H:%M:%SZ' > BUILD_DATE
5254

5355
EXPOSE 8000
5456

55-
CMD ["gunicorn", "--timeout", "15", "--bind", ":8000", "--workers", "2", "--max-requests", "10000", "--max-requests-jitter", "100", "config.wsgi"]
57+
CMD ["gunicorn", "--timeout", "15", "--bind", ":8000", "--workers", "2", "--max-requests", "10000", "--max-requests-jitter", "100", "--log-file", "-", "config.wsgi"]

Makefile

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
.PHONY: help test clean deploy
1+
.PHONY: help test clean dockerbuild dockerserve dockershell deploy
2+
3+
4+
DOCKER_CONFIG=compose.yaml
25

36

47
help:
58
@echo "Please use \`make <target>' where <target> is one of"
69
@echo " test Run the full test suite"
710
@echo " clean Delete assets processed by webpack"
11+
@echo " dockerbuild Build the Docker compose dev environment"
12+
@echo " dockerserve Run the Docker containers for the site"
13+
@echo " (starts a webserver on http://localhost:8000)"
14+
@echo " dockershell Connect to a bash shell on the Django Docker container"
815
@echo " deploy Deploy the app to fly.io"
916

1017

@@ -14,6 +21,21 @@ test:
1421
clean:
1522
rm -rf assets/dist/*
1623

24+
# Build the local multi-container application
25+
# This command can take a while the first time
26+
dockerbuild:
27+
docker compose -f $(DOCKER_CONFIG) build
28+
29+
# You should run "dockerbuild" at least once before running this
30+
# It isn't a dependency because running "dockerbuild" can take some time
31+
dockerserve:
32+
docker compose -f $(DOCKER_CONFIG) up
33+
34+
# Use this command to inspect the container, run management commands,
35+
# or run anything else on the Django container
36+
dockershell:
37+
docker compose -f $(DOCKER_CONFIG) run --rm django /bin/bash
38+
1739
# Build and deploy the production container
1840
deploy:
1941
flyctl deploy

README.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,19 @@ you can build the container and run it locally:
4242
cp .env/local.sample .env/local
4343

4444
# Build the docker image for sandiegopython.org
45-
docker buildx build -t sandiegopython.org .
46-
47-
# Start a development server on http://localhost:8000
48-
docker run --env-file=".env/local" --publish=8000:8000 sandiegopython.org
49-
50-
# You can start a shell to the container with the following:
51-
docker run --env-file=".env/local" -it sandiegopython.org /bin/bash
45+
# Use Docker compose to have Redis and PostgreSQL just like in production
46+
# Note: Docker is used in production but Docker compose is just for development
47+
make dockerbuild
48+
49+
# Start a development web server on http://localhost:8000
50+
# Use ctrl+C to stop
51+
make dockerserve
52+
53+
# While the server is running,
54+
# you can start a bash shell to the container with the following:
55+
# Once you have a bash shell, you can run migrations,
56+
# manually connect to the local Postgres database or anything else
57+
make dockershell
5258
```
5359

5460

assets/src/sass/_theme.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
vertical-align: -.125rem;
1717
}
1818

19+
.icon-1-5x {
20+
width: 1.5rem;
21+
height: 1.5rem;
22+
vertical-align: -.125rem;
23+
}
24+
1925
.icon-2x {
2026
width: 2rem;
2127
height: 2rem;

compose.yaml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Docker Compose Local Development Setup
2+
#
3+
# This starts a local multi-container development environment
4+
# with Postgres, Redis, and Django.
5+
# The configuration comes from .env/local and .env/postgres
6+
#
7+
# To run:
8+
# $ make dockerbuild
9+
# $ make dockerserve
10+
11+
volumes:
12+
local_postgres_data: {}
13+
14+
services:
15+
django:
16+
build:
17+
context: .
18+
dockerfile: ./Dockerfile
19+
image: sandiegopython_local_django
20+
depends_on:
21+
- postgres
22+
env_file:
23+
- ./.env/local
24+
- ./.env/postgres
25+
ports:
26+
- "${SANDIEGOPYTHON_DJANGO_PORT:-8000}:8000"
27+
# Allow us to run `docker attach` and get
28+
# control on STDIN and be able to debug our code with interactive pdb
29+
stdin_open: true
30+
tty: true
31+
32+
postgres:
33+
image: postgres:15.2
34+
volumes:
35+
- local_postgres_data:/var/lib/postgresql/data
36+
env_file:
37+
- ./.env/postgres
38+
39+
redis:
40+
image: redis:5.0

config/settings/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
8686
# --------------------------------------------------------------------------
8787
DATABASES = {"default": dj_database_url.config(default="sqlite:///db.sqlite3")}
88+
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
8889

8990

9091
# Internationalization
@@ -115,7 +116,7 @@
115116
os.path.join(BASE_DIR, "pythonsd", "static"),
116117
]
117118

118-
MEDIA_URL = "/media/"
119+
MEDIA_URL = os.environ.get("MEDIA_URL", default="/media/")
119120
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
120121

121122

config/settings/prod.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@
3434
ADMIN_URL = os.environ.get("ADMIN_URL", "admin")
3535

3636

37+
# Django-storages
38+
# https://django-storages.readthedocs.io
39+
# --------------------------------------------------------------------------
40+
# Optionally store media files in S3/R2/etc.
41+
AWS_S3_ACCESS_KEY_ID = os.environ.get("AWS_S3_ACCESS_KEY_ID")
42+
AWS_S3_SECRET_ACCESS_KEY = os.environ.get("AWS_S3_SECRET_ACCESS_KEY")
43+
AWS_STORAGE_BUCKET_NAME = os.environ.get("AWS_STORAGE_BUCKET_NAME")
44+
# When using media storage with a custom domain
45+
# set this and set MEDIA_URL
46+
AWS_S3_CUSTOM_DOMAIN = os.environ.get("AWS_S3_CUSTOM_DOMAIN")
47+
# The endpoint URL is necessary for Cloudflare R2
48+
AWS_S3_ENDPOINT_URL = os.environ.get("AWS_S3_ENDPOINT_URL", default=None)
49+
if AWS_S3_ACCESS_KEY_ID and AWS_S3_SECRET_ACCESS_KEY and AWS_STORAGE_BUCKET_NAME:
50+
DEFAULT_FILE_STORAGE = "storages.backends.s3.S3Storage"
51+
52+
3753
# Database
3854
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
3955
# --------------------------------------------------------------------------

0 commit comments

Comments
 (0)