40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
from django.db import migrations, models
|
|
|
|
|
|
def backfill_user_status(apps, schema_editor):
|
|
User = apps.get_model("accounts", "User")
|
|
for user in User.objects.all():
|
|
if user.is_superuser:
|
|
status = "superadmin"
|
|
flags = {"is_active": True, "is_staff": True, "is_superuser": True}
|
|
elif not user.is_active:
|
|
status = "inactive"
|
|
flags = {"is_active": False, "is_staff": False, "is_superuser": False}
|
|
else:
|
|
status = "active"
|
|
flags = {"is_active": True, "is_staff": False, "is_superuser": False}
|
|
User.objects.filter(pk=user.pk).update(status=status, **flags)
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
dependencies = [
|
|
("accounts", "0002_erd_named_primary_keys"),
|
|
]
|
|
|
|
operations = [
|
|
migrations.AddField(
|
|
model_name="user",
|
|
name="status",
|
|
field=models.CharField(
|
|
choices=[
|
|
("active", "Active"),
|
|
("inactive", "Inactive"),
|
|
("superadmin", "Super Admin"),
|
|
],
|
|
default="active",
|
|
max_length=30,
|
|
),
|
|
),
|
|
migrations.RunPython(backfill_user_status, migrations.RunPython.noop),
|
|
]
|