Skip to content

Feat: districts Django app setup #91

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 3 commits into from
Apr 16, 2025
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 .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ DJANGO_SUPERUSER_PASSWORD=superuser12345!
DJANGO_DB_RESET=true
DJANGO_STORAGE_DIR=.
DJANGO_DB_FILE=django.db
DJANGO_DB_FIXTURES="pems/local_fixtures.json"

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

# generate

python manage.py makemigrations

# reformat with black

python -m black pems/districts/migrations/*.py
9 changes: 9 additions & 0 deletions bin/reset_db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,12 @@ if [[ $DB_RESET = true ]]; then
else
echo "DB_RESET is false, skipping"
fi

valid_fixtures=$(echo "$DJANGO_DB_FIXTURES" | grep -e fixtures\.json$ || test $? = 1)

if [[ -n "$valid_fixtures" ]]; then
# load data fixtures
python manage.py loaddata "$DJANGO_DB_FIXTURES"
else
echo "No JSON fixtures to load"
fi
Empty file added pems/districts/__init__.py
Empty file.
4 changes: 4 additions & 0 deletions pems/districts/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from django.contrib import admin
from .models import District

admin.site.register(District)
5 changes: 5 additions & 0 deletions pems/districts/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class DistrictConfig(AppConfig):
name = "pems.districts"
21 changes: 21 additions & 0 deletions pems/districts/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 5.1.6 on 2025-04-15 20:59

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = []

operations = [
migrations.CreateModel(
name="District",
fields=[
("id", models.AutoField(primary_key=True, serialize=False)),
("number", models.TextField(blank=True, default="", help_text="The number of the Caltrans district")),
("name", models.TextField(blank=True, default="", help_text="The short name of the Caltrans district")),
],
),
]
Empty file.
12 changes: 12 additions & 0 deletions pems/districts/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.db import models


class District(models.Model):
"""Data associated with a CalTrans District."""

id = models.AutoField(primary_key=True)
number = models.TextField(default="", blank=True, help_text="The number of the Caltrans district")
name = models.TextField(default="", blank=True, help_text="The short name of the Caltrans district")

def __str__(self):
return f"{self.number} - {self.name}"
8 changes: 8 additions & 0 deletions pems/districts/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""
URLConf for the districts app.
"""

app_name = "districts"
urlpatterns = [
# /districts
]
1 change: 1 addition & 0 deletions pems/districts/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Create your views here.
98 changes: 98 additions & 0 deletions pems/local_fixtures.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
[
{
"model": "districts.district",
"pk": 1,
"fields": {
"number": "1",
"name": "Eureka"
}
},
{
"model": "districts.district",
"pk": 2,
"fields": {
"number": "2",
"name": "Redding"
}
},
{
"model": "districts.district",
"pk": 3,
"fields": {
"number": "3",
"name": "Marysville / Sacramento"
}
},
{
"model": "districts.district",
"pk": 4,
"fields": {
"number": "4",
"name": "Bay Area / Oakland"
}
},
{
"model": "districts.district",
"pk": 5,
"fields": {
"number": "5",
"name": "San Luis Obispo / Santa Barbara"
}
},
{
"model": "districts.district",
"pk": 6,
"fields": {
"number": "6",
"name": "Fresno / Bakersfield"
}
},
{
"model": "districts.district",
"pk": 7,
"fields": {
"number": "7",
"name": "Los Angeles / Ventura"
}
},
{
"model": "districts.district",
"pk": 8,
"fields": {
"number": "8",
"name": "San Bernardino / Riverside"
}
},
{
"model": "districts.district",
"pk": 9,
"fields": {
"number": "9",
"name": "Bishop"
}
},
{
"model": "districts.district",
"pk": 10,
"fields": {
"number": "10",
"name": "Stockton"
}
},
{
"model": "districts.district",
"pk": 11,
"fields": {
"number": "11",
"name": "San Diego"
}
},
{
"model": "districts.district",
"pk": 12,
"fields": {
"number": "12",
"name": "Orange County"
}
}
]
1 change: 1 addition & 0 deletions pems/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def _filter_empty(ls):
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"pems.districts",
"pems.streamlit_sample",
]

Expand Down
10 changes: 10 additions & 0 deletions tests/pytest/pems/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import pytest

from pems.districts.models import District


@pytest.fixture
def model_District():
district = District.objects.create(number="1", name="Eureka")

return district
Empty file.
6 changes: 6 additions & 0 deletions tests/pytest/pems/districts/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import pytest


@pytest.mark.django_db
def test_District_str(model_District):
assert str(model_District) == "1 - Eureka"
Loading