Skip to content

Commit 5eeeaf9

Browse files
authored
Feat: initialize Django and create db (#37)
2 parents 60b15e6 + 15f66cf commit 5eeeaf9

File tree

6 files changed

+44
-0
lines changed

6 files changed

+44
-0
lines changed

.devcontainer/devcontainer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"service": "dev",
55
"forwardPorts": ["docs:8000", "kibana:5601"],
66
"workspaceFolder": "/home/caltrans/src",
7+
"postStartCommand": ["/bin/bash", "bin/reset_db.sh"],
78
"postAttachCommand": ["/bin/bash", ".devcontainer/postAttach.sh"],
89
"customizations": {
910
"vscode": {

.env.sample

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1+
# Django superuser account for backend admin access
2+
DJANGO_SUPERUSER_USERNAME=pems-admin
3+
DJANGO_SUPERUSER_EMAIL=pems-admin@compiler.la
4+
DJANGO_SUPERUSER_PASSWORD=superuser12345!
5+
16
# Django storage
7+
DJANGO_DB_RESET=true
28
DJANGO_STORAGE_DIR=.
39
DJANGO_DB_FILE=django.db
410

bin/init.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
set -eux
3+
4+
# run database migrations
5+
6+
python manage.py migrate
7+
8+
# collect static files
9+
10+
python manage.py collectstatic --no-input

bin/reset_db.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env bash
2+
set -ex
3+
4+
# whether to reset database file, defaults to true
5+
DB_RESET="${DJANGO_DB_RESET:-true}"
6+
7+
if [[ $DB_RESET = true ]]; then
8+
# construct the path to the database file from environment or default
9+
DB_DIR="${DJANGO_STORAGE_DIR:-.}"
10+
DB_FILE="${DJANGO_DB_FILE:-django.db}"
11+
DB_PATH="${DB_DIR}/${DB_FILE}"
12+
13+
rm -f "${DB_PATH}"
14+
15+
# run database migrations and other initialization
16+
bin/init.sh
17+
18+
# create a superuser account for backend admin access
19+
# set username, email, and password using environment variables
20+
# DJANGO_SUPERUSER_USERNAME, DJANGO_SUPERUSER_EMAIL, and DJANGO_SUPERUSER_PASSWORD
21+
python manage.py createsuperuser --no-input
22+
else
23+
echo "DB_RESET is false, skipping"
24+
fi

compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ services:
66
context: .
77
dockerfile: .devcontainer/Dockerfile
88
image: caltrans/pems:main
9+
env_file: .env
910
volumes:
1011
- ./:/home/caltrans/src
1112

pems/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ def _filter_empty(ls):
112112

113113
STATIC_URL = "static/"
114114

115+
STATIC_ROOT = os.path.join(BASE_DIR, "static")
116+
115117
# Default primary key field type
116118
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field
117119

0 commit comments

Comments
 (0)