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
146 lines
4.2 KiB
Python
146 lines
4.2 KiB
Python
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
|