Files
upstream-zulip/zerver/migrations/0373_fix_deleteduser_dummies.py
upstream-mirror 64898b2c79
Some checks failed
Code scanning / CodeQL (push) Failing after 15s
Zulip production suite / Ubuntu 22.04 production build (push) Failing after 26s
Zulip production suite / Debian 12 production install with custom db name and user (push) Has been skipped
Zulip production suite / Ubuntu 22.04 production install and PostgreSQL upgrade with pgroonga (push) Has been skipped
Zulip production suite / Ubuntu 24.04 production install (push) Has been skipped
Zulip production suite / Ubuntu 26.04 production install (push) Has been skipped
Zulip production suite / Debian 13 production install (push) Has been skipped
Zulip production suite / 7.0 Version Upgrade (push) Has been skipped
Zulip production suite / 8.0 Version Upgrade (push) Has been skipped
Zulip production suite / 6.0 Version Upgrade (push) Has been skipped
Zulip production suite / 10.0 Version Upgrade (push) Has been skipped
Zulip production suite / 9.0 Version Upgrade (push) Has been skipped
Zulip production suite / 12.0 Version Upgrade (push) Has been skipped
Zulip production suite / 11.0 Version Upgrade (push) Has been skipped
GitHub Actions Security Analysis with zizmor / zizmor (push) Failing after 3s
Zulip CI / Debian 12 (Python 3.11, backend + documentation) (push) Failing after 26s
Zulip CI / Ubuntu 22.04 (Python 3.10, backend + frontend) (push) Failing after 3s
Zulip CI / Ubuntu 24.04 (Python 3.12, backend) (push) Failing after 25s
Zulip CI / Ubuntu 26.04 (Python 3.14, backend) (push) Failing after 25s
Zulip CI / Debian 13 (Python 3.13, backend) (push) Failing after 28s
Zulip production suite / Required jobs (push) Has been cancelled
Zulip CI / Required jobs (push) Has been cancelled
API Documentation Update Check / check-feature-level-updated (push) Has been cancelled
API Documentation Update Check / notify-if-api-docs-changed (push) Has been cancelled
snapshot of https://github.com/zulip/zulip.git @ 48c69911c7dd4e03b20552eb3bd77ad8151c1357 (2026-09-15)
2026-09-15 15:05:30 +05:00

89 lines
3.1 KiB
Python

from email.headerregistry import Address
from typing import Any
from django.conf import settings
from django.core.exceptions import ValidationError
from django.core.validators import validate_email
from django.db import migrations
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django.db.migrations.state import StateApps
def host_for_subdomain(subdomain: str) -> str:
if subdomain == "":
return settings.EXTERNAL_HOST
default_host = f"{subdomain}.{settings.EXTERNAL_HOST}"
return settings.REALM_HOSTS.get(subdomain, default_host)
def get_fake_email_domain(realm: Any) -> str:
"""
Taken from zerver.models. Adjusted to work in a migration without changing
behavior.
"""
try:
# Check that realm.host can be used to form valid email addresses.
realm_host = host_for_subdomain(realm.string_id)
validate_email(Address(username="bot", domain=realm_host).addr_spec)
return realm_host
except ValidationError:
pass
try:
# Check that the fake email domain can be used to form valid email addresses.
validate_email(Address(username="bot", domain=settings.FAKE_EMAIL_DOMAIN).addr_spec)
except ValidationError:
raise Exception(
settings.FAKE_EMAIL_DOMAIN + " is not a valid domain. "
"Consider setting the FAKE_EMAIL_DOMAIN setting."
)
return settings.FAKE_EMAIL_DOMAIN
def fix_dummy_users(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor) -> None:
"""
do_delete_users had two bugs:
1. Creating the replacement dummy users with active=True
2. Creating the replacement dummy users with email domain set to realm.url,
which may not be a valid email domain.
Prior commits fixed the bugs, and this migration fixes the pre-existing objects.
"""
UserProfile = apps.get_model("zerver", "UserProfile")
Subscription = apps.get_model("zerver", "Subscription")
users_to_fix = UserProfile.objects.filter(
is_mirror_dummy=True, is_active=True, delivery_email__regex=r"^deleteduser\d+@.+"
)
update_fields = ["is_active"]
for user_profile in users_to_fix:
user_profile.is_active = False
try:
validate_email(user_profile.delivery_email)
except ValidationError:
user_profile.delivery_email = Address(
username=f"deleteduser{user_profile.id}",
domain=get_fake_email_domain(user_profile.realm),
).addr_spec
update_fields.append("delivery_email")
UserProfile.objects.bulk_update(users_to_fix, update_fields)
# The denormalized is_user_active field needs to be updated too.
Subscription.objects.filter(user_profile__in=users_to_fix).update(is_user_active=False)
class Migration(migrations.Migration):
dependencies = [
("zerver", "0372_realmemoji_unique_realm_emoji_when_false_deactivated"),
]
operations = [
migrations.RunPython(
fix_dummy_users,
reverse_code=migrations.RunPython.noop,
elidable=True,
)
]