2017-02-15 04:35:10 +01:00
|
|
|
# Generated by Django 1.10.5 on 2017-03-04 07:33
|
2017-11-16 00:44:00 +01:00
|
|
|
import django.db.models.deletion
|
2017-02-15 04:35:10 +01:00
|
|
|
from django.conf import settings
|
|
|
|
from django.db import migrations, models
|
2023-03-04 01:40:40 +01:00
|
|
|
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
|
2017-11-16 00:44:00 +01:00
|
|
|
from django.db.migrations.state import StateApps
|
2017-04-15 04:03:56 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
2017-02-15 04:35:10 +01:00
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
def backfill_user_activations_and_deactivations(
|
2022-05-27 23:33:51 +02:00
|
|
|
apps: StateApps, schema_editor: BaseDatabaseSchemaEditor
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> None:
|
2017-04-15 04:03:56 +02:00
|
|
|
migration_time = timezone_now()
|
2021-02-12 08:20:45 +01:00
|
|
|
RealmAuditLog = apps.get_model("zerver", "RealmAuditLog")
|
|
|
|
UserProfile = apps.get_model("zerver", "UserProfile")
|
2017-02-15 04:35:10 +01:00
|
|
|
|
|
|
|
for user in UserProfile.objects.all():
|
2021-02-12 08:19:30 +01:00
|
|
|
RealmAuditLog.objects.create(
|
|
|
|
realm=user.realm,
|
|
|
|
modified_user=user,
|
2021-02-12 08:20:45 +01:00
|
|
|
event_type="user_created",
|
2021-02-12 08:19:30 +01:00
|
|
|
event_time=user.date_joined,
|
|
|
|
backfilled=False,
|
|
|
|
)
|
2017-02-15 04:35:10 +01:00
|
|
|
|
|
|
|
for user in UserProfile.objects.filter(is_active=False):
|
2021-02-12 08:19:30 +01:00
|
|
|
RealmAuditLog.objects.create(
|
|
|
|
realm=user.realm,
|
|
|
|
modified_user=user,
|
2021-02-12 08:20:45 +01:00
|
|
|
event_type="user_deactivated",
|
2021-02-12 08:19:30 +01:00
|
|
|
event_time=migration_time,
|
|
|
|
backfilled=True,
|
|
|
|
)
|
|
|
|
|
2017-02-15 04:35:10 +01:00
|
|
|
|
2022-05-27 23:33:51 +02:00
|
|
|
def reverse_code(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
RealmAuditLog = apps.get_model("zerver", "RealmAuditLog")
|
|
|
|
RealmAuditLog.objects.filter(event_type="user_created").delete()
|
|
|
|
RealmAuditLog.objects.filter(event_type="user_deactivated").delete()
|
2017-02-15 04:35:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
dependencies = [
|
2021-02-12 08:20:45 +01:00
|
|
|
("zerver", "0056_userprofile_emoji_alt_code"),
|
2017-02-15 04:35:10 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
operations = [
|
|
|
|
migrations.CreateModel(
|
2021-02-12 08:20:45 +01:00
|
|
|
name="RealmAuditLog",
|
2017-02-15 04:35:10 +01:00
|
|
|
fields=[
|
2021-02-12 08:19:30 +01:00
|
|
|
(
|
2021-02-12 08:20:45 +01:00
|
|
|
"id",
|
2021-02-12 08:19:30 +01:00
|
|
|
models.AutoField(
|
2021-02-12 08:20:45 +01:00
|
|
|
auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
|
|
|
),
|
2021-02-12 08:20:45 +01:00
|
|
|
("event_type", models.CharField(max_length=40)),
|
|
|
|
("backfilled", models.BooleanField(default=False)),
|
|
|
|
("event_time", models.DateTimeField()),
|
2021-02-12 08:19:30 +01:00
|
|
|
(
|
2021-02-12 08:20:45 +01:00
|
|
|
"acting_user",
|
2021-02-12 08:19:30 +01:00
|
|
|
models.ForeignKey(
|
|
|
|
null=True,
|
|
|
|
on_delete=django.db.models.deletion.CASCADE,
|
2021-02-12 08:20:45 +01:00
|
|
|
related_name="+",
|
2021-02-12 08:19:30 +01:00
|
|
|
to=settings.AUTH_USER_MODEL,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
2021-02-12 08:20:45 +01:00
|
|
|
"modified_stream",
|
2021-02-12 08:19:30 +01:00
|
|
|
models.ForeignKey(
|
2021-02-12 08:20:45 +01:00
|
|
|
null=True, on_delete=django.db.models.deletion.CASCADE, to="zerver.Stream"
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
2021-02-12 08:20:45 +01:00
|
|
|
"modified_user",
|
2021-02-12 08:19:30 +01:00
|
|
|
models.ForeignKey(
|
|
|
|
null=True,
|
|
|
|
on_delete=django.db.models.deletion.CASCADE,
|
2021-02-12 08:20:45 +01:00
|
|
|
related_name="+",
|
2021-02-12 08:19:30 +01:00
|
|
|
to=settings.AUTH_USER_MODEL,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
2021-02-12 08:20:45 +01:00
|
|
|
"realm",
|
2021-02-12 08:19:30 +01:00
|
|
|
models.ForeignKey(
|
2021-02-12 08:20:45 +01:00
|
|
|
on_delete=django.db.models.deletion.CASCADE, to="zerver.Realm"
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
|
|
|
),
|
2017-02-15 04:35:10 +01:00
|
|
|
],
|
|
|
|
),
|
2021-02-12 08:19:30 +01:00
|
|
|
migrations.RunPython(
|
|
|
|
backfill_user_activations_and_deactivations, reverse_code=reverse_code, elidable=True
|
|
|
|
),
|
2017-02-15 04:35:10 +01:00
|
|
|
]
|