feat: scaffold Django multi-tenant project with 5 of 9 apps
Phase 1 scaffolding: config/, core/, base models, AES-256-GCM phone encryption, enums mirror apps.tenant: Tenant + Domain (django-tenants) apps.org: 11 models (OrgUnit hierarchy, Staff, audit logs) apps.account: 4 models (UserAccount as AUTH_USER_MODEL, login/password tracking) apps.permission: 7 models (RBAC + overrides + datascope + append-only changelog) apps.region: 5 models (District, BusinessArea, MetroLine, MetroStation, School) All migrations generated, manage.py check passes
This commit is contained in:
0
apps/region/__init__.py
Normal file
0
apps/region/__init__.py
Normal file
7
apps/region/apps.py
Normal file
7
apps/region/apps.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class RegionConfig(AppConfig):
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "apps.region"
|
||||
label = "region"
|
||||
128
apps/region/migrations/0001_initial.py
Normal file
128
apps/region/migrations/0001_initial.py
Normal file
@@ -0,0 +1,128 @@
|
||||
# Generated by Django 4.2.16 on 2026-04-29 08:57
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import uuid
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='BusinessArea',
|
||||
fields=[
|
||||
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
|
||||
('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('name', models.CharField(max_length=100)),
|
||||
('sort_order', models.IntegerField(default=0)),
|
||||
('latitude', models.DecimalField(blank=True, decimal_places=7, max_digits=10, null=True)),
|
||||
('longitude', models.DecimalField(blank=True, decimal_places=7, max_digits=10, null=True)),
|
||||
('is_active', models.BooleanField(default=True)),
|
||||
],
|
||||
options={
|
||||
'db_table': 'business_areas',
|
||||
'ordering': ['district_id', 'sort_order', 'name'],
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='District',
|
||||
fields=[
|
||||
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
|
||||
('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('city', models.CharField(max_length=50)),
|
||||
('name', models.CharField(max_length=50)),
|
||||
('short_name', models.CharField(blank=True, default='', max_length=20)),
|
||||
('sort_order', models.IntegerField(default=0)),
|
||||
('is_active', models.BooleanField(default=True)),
|
||||
],
|
||||
options={
|
||||
'db_table': 'districts',
|
||||
'ordering': ['city', 'sort_order', 'name'],
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='MetroLine',
|
||||
fields=[
|
||||
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
|
||||
('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('city', models.CharField(max_length=50)),
|
||||
('name', models.CharField(max_length=50)),
|
||||
('color', models.CharField(blank=True, default='', max_length=7)),
|
||||
('sort_order', models.IntegerField(default=0)),
|
||||
('is_active', models.BooleanField(default=True)),
|
||||
],
|
||||
options={
|
||||
'db_table': 'metro_lines',
|
||||
'ordering': ['city', 'sort_order', 'name'],
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='School',
|
||||
fields=[
|
||||
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
|
||||
('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('name', models.CharField(max_length=100)),
|
||||
('type', models.CharField(blank=True, choices=[('primary', '小学'), ('middle', '初中'), ('high', '高中'), ('k9', '九年一贯制'), ('k12', '十二年一贯制')], default='', max_length=20)),
|
||||
('nature', models.CharField(blank=True, choices=[('public', '公立'), ('private', '私立'), ('international', '国际')], default='', max_length=20)),
|
||||
('level', models.CharField(blank=True, choices=[('normal', '普通'), ('key', '重点'), ('top', '名校')], default='', max_length=20)),
|
||||
('is_active', models.BooleanField(default=True)),
|
||||
('district', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='schools', to='region.district')),
|
||||
],
|
||||
options={
|
||||
'db_table': 'schools',
|
||||
'ordering': ['name'],
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='MetroStation',
|
||||
fields=[
|
||||
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
|
||||
('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('name', models.CharField(max_length=50)),
|
||||
('latitude', models.DecimalField(blank=True, decimal_places=7, max_digits=10, null=True)),
|
||||
('longitude', models.DecimalField(blank=True, decimal_places=7, max_digits=10, null=True)),
|
||||
('sort_order', models.IntegerField(default=0)),
|
||||
('is_active', models.BooleanField(default=True)),
|
||||
('metro_line', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='stations', to='region.metroline')),
|
||||
],
|
||||
options={
|
||||
'db_table': 'metro_stations',
|
||||
'ordering': ['metro_line_id', 'sort_order'],
|
||||
},
|
||||
),
|
||||
migrations.AddConstraint(
|
||||
model_name='district',
|
||||
constraint=models.UniqueConstraint(condition=models.Q(('is_active', True)), fields=('city', 'name'), name='uq_districts_city_name'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='businessarea',
|
||||
name='district',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='business_areas', to='region.district'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='school',
|
||||
index=models.Index(condition=models.Q(('is_active', True)), fields=['district'], name='idx_schools_district'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='metrostation',
|
||||
index=models.Index(condition=models.Q(('is_active', True)), fields=['metro_line'], name='idx_metro_stations_line'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='businessarea',
|
||||
index=models.Index(condition=models.Q(('is_active', True)), fields=['district'], name='idx_business_areas_district'),
|
||||
),
|
||||
migrations.AddConstraint(
|
||||
model_name='businessarea',
|
||||
constraint=models.UniqueConstraint(fields=('district', 'name'), name='uq_business_areas_name'),
|
||||
),
|
||||
]
|
||||
0
apps/region/migrations/__init__.py
Normal file
0
apps/region/migrations/__init__.py
Normal file
15
apps/region/models/__init__.py
Normal file
15
apps/region/models/__init__.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from apps.region.models.region import (
|
||||
BusinessArea,
|
||||
District,
|
||||
MetroLine,
|
||||
MetroStation,
|
||||
School,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"BusinessArea",
|
||||
"District",
|
||||
"MetroLine",
|
||||
"MetroStation",
|
||||
"School",
|
||||
]
|
||||
145
apps/region/models/region.py
Normal file
145
apps/region/models/region.py
Normal file
@@ -0,0 +1,145 @@
|
||||
from django.db import models
|
||||
|
||||
from core.enums import SchoolLevel, SchoolNature, SchoolType
|
||||
from core.models.base import TimeStampedModel
|
||||
|
||||
|
||||
class District(TimeStampedModel):
|
||||
city = models.CharField(max_length=50)
|
||||
name = models.CharField(max_length=50)
|
||||
short_name = models.CharField(max_length=20, blank=True, default="")
|
||||
sort_order = models.IntegerField(default=0)
|
||||
is_active = models.BooleanField(default=True)
|
||||
|
||||
class Meta:
|
||||
db_table = "districts"
|
||||
constraints = [
|
||||
models.UniqueConstraint(
|
||||
fields=["city", "name"],
|
||||
condition=models.Q(is_active=True),
|
||||
name="uq_districts_city_name",
|
||||
),
|
||||
]
|
||||
ordering = ["city", "sort_order", "name"]
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"{self.city} / {self.name}"
|
||||
|
||||
|
||||
class BusinessArea(TimeStampedModel):
|
||||
district = models.ForeignKey(
|
||||
"region.District",
|
||||
on_delete=models.PROTECT,
|
||||
related_name="business_areas",
|
||||
)
|
||||
name = models.CharField(max_length=100)
|
||||
sort_order = models.IntegerField(default=0)
|
||||
latitude = models.DecimalField(max_digits=10, decimal_places=7, null=True, blank=True)
|
||||
longitude = models.DecimalField(max_digits=10, decimal_places=7, null=True, blank=True)
|
||||
is_active = models.BooleanField(default=True)
|
||||
|
||||
class Meta:
|
||||
db_table = "business_areas"
|
||||
constraints = [
|
||||
models.UniqueConstraint(
|
||||
fields=["district", "name"],
|
||||
name="uq_business_areas_name",
|
||||
),
|
||||
]
|
||||
indexes = [
|
||||
models.Index(
|
||||
fields=["district"],
|
||||
name="idx_business_areas_district",
|
||||
condition=models.Q(is_active=True),
|
||||
),
|
||||
]
|
||||
ordering = ["district_id", "sort_order", "name"]
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.name
|
||||
|
||||
|
||||
class MetroLine(TimeStampedModel):
|
||||
city = models.CharField(max_length=50)
|
||||
name = models.CharField(max_length=50)
|
||||
color = models.CharField(max_length=7, blank=True, default="")
|
||||
sort_order = models.IntegerField(default=0)
|
||||
is_active = models.BooleanField(default=True)
|
||||
|
||||
class Meta:
|
||||
db_table = "metro_lines"
|
||||
ordering = ["city", "sort_order", "name"]
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"{self.city} {self.name}"
|
||||
|
||||
|
||||
class MetroStation(TimeStampedModel):
|
||||
metro_line = models.ForeignKey(
|
||||
"region.MetroLine",
|
||||
on_delete=models.CASCADE,
|
||||
related_name="stations",
|
||||
)
|
||||
name = models.CharField(max_length=50)
|
||||
latitude = models.DecimalField(max_digits=10, decimal_places=7, null=True, blank=True)
|
||||
longitude = models.DecimalField(max_digits=10, decimal_places=7, null=True, blank=True)
|
||||
sort_order = models.IntegerField(default=0)
|
||||
is_active = models.BooleanField(default=True)
|
||||
|
||||
class Meta:
|
||||
db_table = "metro_stations"
|
||||
indexes = [
|
||||
models.Index(
|
||||
fields=["metro_line"],
|
||||
name="idx_metro_stations_line",
|
||||
condition=models.Q(is_active=True),
|
||||
),
|
||||
]
|
||||
ordering = ["metro_line_id", "sort_order"]
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.name
|
||||
|
||||
|
||||
class School(TimeStampedModel):
|
||||
district = models.ForeignKey(
|
||||
"region.District",
|
||||
null=True,
|
||||
blank=True,
|
||||
on_delete=models.SET_NULL,
|
||||
related_name="schools",
|
||||
)
|
||||
name = models.CharField(max_length=100)
|
||||
type = models.CharField(
|
||||
max_length=20,
|
||||
blank=True,
|
||||
default="",
|
||||
choices=SchoolType.choices,
|
||||
)
|
||||
nature = models.CharField(
|
||||
max_length=20,
|
||||
blank=True,
|
||||
default="",
|
||||
choices=SchoolNature.choices,
|
||||
)
|
||||
level = models.CharField(
|
||||
max_length=20,
|
||||
blank=True,
|
||||
default="",
|
||||
choices=SchoolLevel.choices,
|
||||
)
|
||||
is_active = models.BooleanField(default=True)
|
||||
|
||||
class Meta:
|
||||
db_table = "schools"
|
||||
indexes = [
|
||||
models.Index(
|
||||
fields=["district"],
|
||||
name="idx_schools_district",
|
||||
condition=models.Q(is_active=True),
|
||||
),
|
||||
]
|
||||
ordering = ["name"]
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.name
|
||||
0
apps/region/serializers.py
Normal file
0
apps/region/serializers.py
Normal file
0
apps/region/services/__init__.py
Normal file
0
apps/region/services/__init__.py
Normal file
0
apps/region/tasks.py
Normal file
0
apps/region/tasks.py
Normal file
0
apps/region/templates/region/.gitkeep
Normal file
0
apps/region/templates/region/.gitkeep
Normal file
0
apps/region/tests/__init__.py
Normal file
0
apps/region/tests/__init__.py
Normal file
5
apps/region/urls.py
Normal file
5
apps/region/urls.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from django.urls import path
|
||||
|
||||
app_name = "region"
|
||||
|
||||
urlpatterns: list = []
|
||||
0
apps/region/views.py
Normal file
0
apps/region/views.py
Normal file
Reference in New Issue
Block a user