Skip to content

Feat: initialize Django and create db #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"service": "dev",
"forwardPorts": ["docs:8000", "kibana:5601"],
"workspaceFolder": "/home/caltrans/src",
"postStartCommand": ["/bin/bash", "bin/reset_db.sh"],
"postAttachCommand": ["/bin/bash", ".devcontainer/postAttach.sh"],
"customizations": {
"vscode": {
Expand Down
6 changes: 6 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# Django superuser account for backend admin access
DJANGO_SUPERUSER_USERNAME=pems-admin
DJANGO_SUPERUSER_EMAIL=pems-admin@compiler.la
DJANGO_SUPERUSER_PASSWORD=superuser12345!

# Django storage
DJANGO_DB_RESET=true
DJANGO_STORAGE_DIR=.
DJANGO_DB_FILE=django.db

Expand Down
10 changes: 10 additions & 0 deletions bin/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
set -eux

# run database migrations

python manage.py migrate

# collect static files

python manage.py collectstatic --no-input
24 changes: 24 additions & 0 deletions bin/reset_db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash
set -ex

# whether to reset database file, defaults to true
DB_RESET="${DJANGO_DB_RESET:-true}"

if [[ $DB_RESET = true ]]; then
# construct the path to the database file from environment or default
DB_DIR="${DJANGO_STORAGE_DIR:-.}"
DB_FILE="${DJANGO_DB_FILE:-django.db}"
DB_PATH="${DB_DIR}/${DB_FILE}"

rm -f "${DB_PATH}"

# run database migrations and other initialization
bin/init.sh

# create a superuser account for backend admin access
# set username, email, and password using environment variables
# DJANGO_SUPERUSER_USERNAME, DJANGO_SUPERUSER_EMAIL, and DJANGO_SUPERUSER_PASSWORD
python manage.py createsuperuser --no-input
else
echo "DB_RESET is false, skipping"
fi
1 change: 1 addition & 0 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ services:
context: .
dockerfile: .devcontainer/Dockerfile
image: caltrans/pems:main
env_file: .env
volumes:
- ./:/home/caltrans/src

Expand Down
2 changes: 2 additions & 0 deletions pems/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ def _filter_empty(ls):

STATIC_URL = "static/"

STATIC_ROOT = os.path.join(BASE_DIR, "static")

# Default primary key field type
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field

Expand Down
Loading