Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
import: Fix import of Stream.creator field.
Mateusz Mandera160076fdfa19
diff --git a/zerver/lib/import_realm.py b/zerver/lib/import_realm.pyindex 0ea7e8165a1..d4f836734c1 100644--- a/zerver/lib/import_realm.py+++ b/zerver/lib/import_realm.py@@ -1081,7 +1081,18 @@ def do_import_realm(import_dir: Path, subdomain: str, processes: int = 1) -> Rea# Stream objects are created by Django.fix_datetime_fields(data, "zerver_stream")re_map_foreign_keys(data, "zerver_stream", "realm", related_table="realm")+re_map_foreign_keys(data, "zerver_stream", "creator", related_table="user_profile")+ # There's a circular dependency between Stream and UserProfile due to+ # the .creator attribute. We untangle it by first remembering the creator_id+ # for all the streams and then removing those fields from the data.+ # That allows us to successfully import streams, and then later after users+ # are imported, we can set the .creator_id for all these streams correctly.+ stream_id_to_creator_id = {}+ for stream in data["zerver_stream"]:+ creator_id = stream.pop("creator_id", None)+ stream_id_to_creator_id[stream["id"]] = creator_id+if role_system_groups_dict is not None:# Because the system user groups are missing, we manually set up# the defaults for can_remove_subscribers_group for all the@@ -1136,6 +1147,13 @@ def do_import_realm(import_dir: Path, subdomain: str, processes: int = 1) -> Reauser_profile.tos_version = UserProfile.TOS_VERSION_BEFORE_FIRST_LOGINUserProfile.objects.bulk_create(user_profiles)+ # UserProfiles have been loaded, so now we're ready to set .creator_id+ # for streams based on the mapping we saved earlier.+ streams = Stream.objects.filter(id__in=stream_id_to_creator_id.keys())+ for stream in streams:+ stream.creator_id = stream_id_to_creator_id[stream.id]+ Stream.objects.bulk_update(streams, ["creator_id"])+re_map_foreign_keys(data, "zerver_defaultstream", "stream", related_table="stream")re_map_foreign_keys(data, "zerver_realmemoji", "author", related_table="user_profile")diff --git a/zerver/tests/test_import_export.py b/zerver/tests/test_import_export.pyindex 9e685272f5e..edf2fd6a35a 100644--- a/zerver/tests/test_import_export.py+++ b/zerver/tests/test_import_export.py@@ -763,6 +763,10 @@ class RealmImportExportTest(ExportFile):cordelia = self.example_user("cordelia")othello = self.example_user("othello")+ denmark_stream = get_stream("Denmark", original_realm)+ denmark_stream.creator = hamlet+ denmark_stream.save()+internal_realm = get_realm(settings.SYSTEM_BOT_REALM)cross_realm_bot = get_system_bot(settings.WELCOME_BOT, internal_realm.id)@@ -1012,6 +1016,12 @@ class RealmImportExportTest(ExportFile):f'<p><span class="user-mention" data-user-id="{imported_polonius_user.id}">@Polonius</span></p>',)+ imported_hamlet_user = UserProfile.objects.get(+ delivery_email=self.example_email("hamlet"), realm=imported_realm+ )+ imported_denmark_stream = Stream.objects.get(name="Denmark", realm=imported_realm)+ self.assertEqual(imported_denmark_stream.creator, imported_hamlet_user)+# Check recipient_id was generated correctly for the imported users and streams.for user_profile in UserProfile.objects.filter(realm=imported_realm):self.assertEqual(