2017-12-05 03:19:48 +01:00
|
|
|
# Generated by Django 1.11.6 on 2017-12-05 01:08
|
|
|
|
|
|
|
|
import django.db.models.deletion
|
2020-01-14 21:59:46 +01:00
|
|
|
from django.db import migrations, models
|
2022-05-27 23:33:51 +02:00
|
|
|
from django.db.backends.postgresql.schema import BaseDatabaseSchemaEditor
|
2017-12-05 03:19:48 +01:00
|
|
|
from django.db.migrations.state import StateApps
|
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2017-12-05 03:19:48 +01:00
|
|
|
def set_realm_for_existing_scheduledemails(
|
2022-05-27 23:33:51 +02:00
|
|
|
apps: StateApps, schema_editor: BaseDatabaseSchemaEditor
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> None:
|
2017-12-05 03:19:48 +01:00
|
|
|
scheduledemail_model = apps.get_model("zerver", "ScheduledEmail")
|
|
|
|
preregistrationuser_model = apps.get_model("zerver", "PreregistrationUser")
|
|
|
|
for scheduledemail in scheduledemail_model.objects.all():
|
|
|
|
if scheduledemail.type == 3: # ScheduledEmail.INVITATION_REMINDER
|
|
|
|
# Don't think this can be None, but just be safe
|
|
|
|
prereg = preregistrationuser_model.objects.filter(email=scheduledemail.address).first()
|
|
|
|
if prereg is not None:
|
|
|
|
scheduledemail.realm = prereg.realm
|
|
|
|
else:
|
|
|
|
scheduledemail.realm = scheduledemail.user.realm
|
2021-02-12 08:20:45 +01:00
|
|
|
scheduledemail.save(update_fields=["realm"])
|
2017-12-05 03:19:48 +01:00
|
|
|
|
|
|
|
# Shouldn't be needed, but just in case
|
|
|
|
scheduledemail_model.objects.filter(realm=None).delete()
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-12-05 03:19:48 +01:00
|
|
|
class Migration(migrations.Migration):
|
|
|
|
|
|
|
|
dependencies = [
|
2021-02-12 08:20:45 +01:00
|
|
|
("zerver", "0127_disallow_chars_in_stream_and_user_name"),
|
2017-12-05 03:19:48 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
operations = [
|
|
|
|
# Start with ScheduledEmail.realm being non-null
|
|
|
|
migrations.AddField(
|
2021-02-12 08:20:45 +01:00
|
|
|
model_name="scheduledemail",
|
|
|
|
name="realm",
|
2021-02-12 08:19:30 +01:00
|
|
|
field=models.ForeignKey(
|
2021-02-12 08:20:45 +01:00
|
|
|
null=True, on_delete=django.db.models.deletion.CASCADE, to="zerver.Realm"
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
2017-12-05 03:19:48 +01:00
|
|
|
),
|
|
|
|
# Sets realm for existing ScheduledEmails
|
2021-02-12 08:19:30 +01:00
|
|
|
migrations.RunPython(
|
|
|
|
set_realm_for_existing_scheduledemails,
|
|
|
|
reverse_code=migrations.RunPython.noop,
|
|
|
|
elidable=True,
|
|
|
|
),
|
2017-12-05 03:19:48 +01:00
|
|
|
# Require ScheduledEmail.realm to be non-null
|
|
|
|
migrations.AlterField(
|
2021-02-12 08:20:45 +01:00
|
|
|
model_name="scheduledemail",
|
|
|
|
name="realm",
|
|
|
|
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to="zerver.Realm"),
|
2017-12-05 03:19:48 +01:00
|
|
|
),
|
|
|
|
]
|