From 0c13c0c2716626c0315dcffb0094a509ce4f3a78 Mon Sep 17 00:00:00 2001 From: Luis Alvergue Date: Tue, 15 Apr 2025 20:04:11 +0000 Subject: [PATCH 1/3] feat: set up Districts app --- pems/districts/__init__.py | 0 pems/districts/apps.py | 5 +++++ pems/districts/migrations/__init__.py | 0 pems/districts/urls.py | 8 ++++++++ pems/districts/views.py | 1 + pems/settings.py | 1 + 6 files changed, 15 insertions(+) create mode 100644 pems/districts/__init__.py create mode 100644 pems/districts/apps.py create mode 100644 pems/districts/migrations/__init__.py create mode 100644 pems/districts/urls.py create mode 100644 pems/districts/views.py diff --git a/pems/districts/__init__.py b/pems/districts/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pems/districts/apps.py b/pems/districts/apps.py new file mode 100644 index 0000000..47dcbcf --- /dev/null +++ b/pems/districts/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class DistrictConfig(AppConfig): + name = "pems.districts" diff --git a/pems/districts/migrations/__init__.py b/pems/districts/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pems/districts/urls.py b/pems/districts/urls.py new file mode 100644 index 0000000..b2c8724 --- /dev/null +++ b/pems/districts/urls.py @@ -0,0 +1,8 @@ +""" +URLConf for the districts app. +""" + +app_name = "districts" +urlpatterns = [ + # /districts +] diff --git a/pems/districts/views.py b/pems/districts/views.py new file mode 100644 index 0000000..60f00ef --- /dev/null +++ b/pems/districts/views.py @@ -0,0 +1 @@ +# Create your views here. diff --git a/pems/settings.py b/pems/settings.py index 4c3b06c..2848e41 100644 --- a/pems/settings.py +++ b/pems/settings.py @@ -31,6 +31,7 @@ def _filter_empty(ls): "django.contrib.sessions", "django.contrib.messages", "django.contrib.staticfiles", + "pems.districts", "pems.streamlit_sample", ] From a39ab452d59fd9acac8f88087ea9acffc81948cd Mon Sep 17 00:00:00 2001 From: Luis Alvergue Date: Tue, 15 Apr 2025 20:55:47 +0000 Subject: [PATCH 2/3] feat(models): add District model and admin interface --- bin/makemigrations.sh | 10 ++++++++++ pems/districts/admin.py | 4 ++++ pems/districts/migrations/0001_initial.py | 21 +++++++++++++++++++++ pems/districts/models.py | 12 ++++++++++++ tests/pytest/pems/conftest.py | 10 ++++++++++ tests/pytest/pems/districts/__init__.py | 0 tests/pytest/pems/districts/test_models.py | 6 ++++++ 7 files changed, 63 insertions(+) create mode 100755 bin/makemigrations.sh create mode 100644 pems/districts/admin.py create mode 100644 pems/districts/migrations/0001_initial.py create mode 100644 pems/districts/models.py create mode 100644 tests/pytest/pems/districts/__init__.py create mode 100644 tests/pytest/pems/districts/test_models.py diff --git a/bin/makemigrations.sh b/bin/makemigrations.sh new file mode 100755 index 0000000..c973e24 --- /dev/null +++ b/bin/makemigrations.sh @@ -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 diff --git a/pems/districts/admin.py b/pems/districts/admin.py new file mode 100644 index 0000000..4301090 --- /dev/null +++ b/pems/districts/admin.py @@ -0,0 +1,4 @@ +from django.contrib import admin +from .models import District + +admin.site.register(District) diff --git a/pems/districts/migrations/0001_initial.py b/pems/districts/migrations/0001_initial.py new file mode 100644 index 0000000..411a45c --- /dev/null +++ b/pems/districts/migrations/0001_initial.py @@ -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")), + ], + ), + ] diff --git a/pems/districts/models.py b/pems/districts/models.py new file mode 100644 index 0000000..cb8cf9b --- /dev/null +++ b/pems/districts/models.py @@ -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}" diff --git a/tests/pytest/pems/conftest.py b/tests/pytest/pems/conftest.py index e69de29..1d6bcb3 100644 --- a/tests/pytest/pems/conftest.py +++ b/tests/pytest/pems/conftest.py @@ -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 diff --git a/tests/pytest/pems/districts/__init__.py b/tests/pytest/pems/districts/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/pytest/pems/districts/test_models.py b/tests/pytest/pems/districts/test_models.py new file mode 100644 index 0000000..a43e05d --- /dev/null +++ b/tests/pytest/pems/districts/test_models.py @@ -0,0 +1,6 @@ +import pytest + + +@pytest.mark.django_db +def test_District_str(model_District): + assert str(model_District) == "1 - Eureka" From 5acbd72cd9cfd4f6b4d54b20a52551f419436bad Mon Sep 17 00:00:00 2001 From: Luis Alvergue Date: Tue, 15 Apr 2025 20:56:43 +0000 Subject: [PATCH 3/3] chore: add local fixtures for districts setup --- .env.sample | 1 + bin/reset_db.sh | 9 ++++ pems/local_fixtures.json | 98 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 pems/local_fixtures.json diff --git a/.env.sample b/.env.sample index b6483d2..7e2c05e 100644 --- a/.env.sample +++ b/.env.sample @@ -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 diff --git a/bin/reset_db.sh b/bin/reset_db.sh index 43f168e..53333a1 100755 --- a/bin/reset_db.sh +++ b/bin/reset_db.sh @@ -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 diff --git a/pems/local_fixtures.json b/pems/local_fixtures.json new file mode 100644 index 0000000..39ac5b0 --- /dev/null +++ b/pems/local_fixtures.json @@ -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" + } + } +]