baseline: 批次A-D 成果 + membership 半成品(测试红)

This commit is contained in:
agent
2026-09-11 23:11:35 +08:00
commit b3f3095d53
311 changed files with 40540 additions and 0 deletions
@@ -0,0 +1,73 @@
# Generated by Django 5.2.12 on 2026-09-06 16:34
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='Tenant',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('code', models.SlugField(help_text='租户代码(小写字母数字)', max_length=64, unique=True)),
('name', models.CharField(help_text='租户名称(公司/品牌名)', max_length=128)),
('contact', models.CharField(blank=True, default='', max_length=64)),
('phone', models.CharField(blank=True, default='', max_length=32)),
('is_active', models.BooleanField(default=True)),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
],
options={
'db_table': 'core_tenant',
'ordering': ['code'],
},
),
migrations.CreateModel(
name='Org',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('code', models.CharField(help_text='组织代码(在租户内唯一)', max_length=64)),
('name', models.CharField(max_length=128)),
('org_path', models.CharField(blank=True, default='', help_text='组织层级路径,如 0001.0002.0003;用于快速子树查询', max_length=255)),
('is_active', models.BooleanField(default=True)),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('parent', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='children', to='core.org')),
('tenant', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='orgs', to='core.tenant')),
],
options={
'db_table': 'core_org',
'ordering': ['org_path', 'code'],
'unique_together': {('tenant', 'code')},
},
),
migrations.CreateModel(
name='AuditLog',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('action', models.CharField(choices=[('create', '创建'), ('update', '更新'), ('delete', '删除'), ('view', '查看'), ('login', '登录'), ('logout', '登出'), ('export', '导出')], max_length=16)),
('target_type', models.CharField(help_text='目标模型名,如 SalesOrder', max_length=64)),
('target_id', models.CharField(blank=True, default='', max_length=64)),
('detail', models.JSONField(blank=True, default=dict, help_text='操作详情 JSON')),
('ip', models.GenericIPAddressField(blank=True, null=True)),
('user_agent', models.CharField(blank=True, default='', max_length=255)),
('created_at', models.DateTimeField(auto_now_add=True)),
('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='audit_logs', to=settings.AUTH_USER_MODEL)),
('tenant', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='audit_logs', to='core.tenant')),
],
options={
'db_table': 'core_audit_log',
'ordering': ['-created_at'],
'indexes': [models.Index(fields=['tenant', '-created_at'], name='core_audit__tenant__985477_idx'), models.Index(fields=['target_type', 'target_id'], name='core_audit__target__fd0ddd_idx'), models.Index(fields=['user', '-created_at'], name='core_audit__user_id_465bdf_idx')],
},
),
]
@@ -0,0 +1,82 @@
# Generated by Django 5.2.12 on 2026-09-11 13:06
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
def backfill_legacy_memberships(apps, schema_editor):
"""Keep existing installs usable while making tenant access explicit.
Existing superusers retain operator access to every active tenant. Other
users get the configured/default tenant, and the historical demo account
also gets the demo tenant needed by the public demo token flow.
"""
Tenant = apps.get_model('core', 'Tenant')
Membership = apps.get_model('core', 'TenantMembership')
User = apps.get_model(settings.AUTH_USER_MODEL.split('.')[0], settings.AUTH_USER_MODEL.split('.')[1])
default = Tenant.objects.filter(code='default', is_active=True).first()
demo = Tenant.objects.filter(code='demo', is_active=True).first()
for user in User.objects.all().iterator():
tenants = list(Tenant.objects.filter(is_active=True)) if user.is_superuser else ([default] if default else [])
if user.username == 'demo' and demo is not None:
tenants.append(demo)
for tenant in {t.pk: t for t in tenants if t is not None}.values():
Membership.objects.get_or_create(
user_id=user.pk, tenant_id=tenant.pk,
defaults={'role': 'owner' if user.is_superuser else 'member', 'is_active': True},
)
class Migration(migrations.Migration):
dependencies = [
('core', '0001_initial'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='TenantMembership',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('role', models.CharField(choices=[('owner', '所有者'), ('admin', '管理员'), ('member', '成员')], default='member', max_length=16)),
('is_active', models.BooleanField(default=True)),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('tenant', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='memberships', to='core.tenant')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='tenant_memberships', to=settings.AUTH_USER_MODEL)),
],
options={
'db_table': 'core_tenant_membership',
'indexes': [models.Index(fields=['user', 'is_active'], name='core_tenant_user_id_51dd63_idx'), models.Index(fields=['tenant', 'is_active'], name='core_tenant_tenant__f85f17_idx')],
'constraints': [models.UniqueConstraint(fields=('user', 'tenant'), name='core_membership_user_tenant_uniq')],
},
),
migrations.RunPython(backfill_legacy_memberships, migrations.RunPython.noop),
]
def backfill_legacy_memberships(apps, schema_editor):
"""Keep existing installs usable while making tenant access explicit.
Existing superusers retain operator access to every active tenant. Other
users get the configured/default tenant, and the historical demo account
also gets the demo tenant needed by the public demo token flow.
"""
Tenant = apps.get_model('core', 'Tenant')
Membership = apps.get_model('core', 'TenantMembership')
User = apps.get_model(settings.AUTH_USER_MODEL.split('.')[0], settings.AUTH_USER_MODEL.split('.')[1])
default = Tenant.objects.filter(code='default', is_active=True).first()
demo = Tenant.objects.filter(code='demo', is_active=True).first()
for user in User.objects.all().iterator():
tenants = list(Tenant.objects.filter(is_active=True)) if user.is_superuser else ([default] if default else [])
if user.username == 'demo' and demo is not None:
tenants.append(demo)
for tenant in {t.pk: t for t in tenants if t is not None}.values():
Membership.objects.get_or_create(
user_id=user.pk, tenant_id=tenant.pk,
defaults={'role': 'owner' if user.is_superuser else 'member', 'is_active': True},
)