diff --git a/zerver/lib/export.py b/zerver/lib/export.py index 8583c7455b..389175c666 100644 --- a/zerver/lib/export.py +++ b/zerver/lib/export.py @@ -420,6 +420,8 @@ def make_raw(query: Any, exclude: Optional[List[Field]] = None) -> List[Record]: instances below. """ for field in instance._meta.many_to_many: + if exclude is not None and field.name in exclude: + continue value = data[field.name] data[field.name] = [row.id for row in value] @@ -775,6 +777,7 @@ def get_realm_config() -> Config: model=UserGroup, normal_parent=realm_config, include_rows="realm_id__in", + exclude=["direct_members", "direct_subgroups"], ) Config( diff --git a/zerver/lib/import_realm.py b/zerver/lib/import_realm.py index a1a997427d..9cc5c73682 100644 --- a/zerver/lib/import_realm.py +++ b/zerver/lib/import_realm.py @@ -1171,12 +1171,6 @@ def do_import_realm(import_dir: Path, subdomain: str, processes: int = 1) -> Rea if "zerver_usergroup" in data: re_map_foreign_keys(data, "zerver_usergroup", "realm", related_table="realm") - re_map_foreign_keys_many_to_many( - data, "zerver_usergroup", "direct_members", related_table="user_profile" - ) - re_map_foreign_keys_many_to_many( - data, "zerver_usergroup", "direct_subgroups", related_table="usergroup" - ) update_model_ids(UserGroup, data, "usergroup") bulk_import_model(data, UserGroup) diff --git a/zerver/tests/test_import_export.py b/zerver/tests/test_import_export.py index eba728e658..cf0ae14442 100644 --- a/zerver/tests/test_import_export.py +++ b/zerver/tests/test_import_export.py @@ -435,6 +435,12 @@ class RealmImportExportTest(ExportFile): self.assert_length(exported_realm_user_default, 1) self.assertEqual(exported_realm_user_default[0]["default_language"], "de") + exported_usergroups = data["zerver_usergroup"] + self.assert_length(exported_usergroups, 8) + self.assertEqual(exported_usergroups[1]["name"], "@role:administrators") + self.assertFalse("direct_members" in exported_usergroups[1]) + self.assertFalse("direct_subgroups" in exported_usergroups[1]) + data = read_json("messages-000001.json") um = UserMessage.objects.all()[0] exported_um = self.find_by_id(data["zerver_usermessage"], um.id) @@ -1132,6 +1138,26 @@ class RealmImportExportTest(ExportFile): subgroups = {membership.subgroup.name for membership in group_group_membership} return subgroups + @getter + def get_user_group_direct_members(r: Realm) -> Set[str]: + # We already check the members of the group through UserGroupMembership + # objects, but we also want to check direct_members field is set + # correctly since we do not include this in export data. + usergroup = UserGroup.objects.get(realm=r, name="hamletcharacters") + direct_members = usergroup.direct_members.all() + direct_member_emails = {user.email for user in direct_members} + return direct_member_emails + + @getter + def get_user_group_direct_subgroups(r: Realm) -> Set[str]: + # We already check the subgroups of the group through GroupGroupMembership + # objects, but we also want to check that direct_subgroups field is set + # correctly since we do not include this in export data. + usergroup = UserGroup.objects.get(realm=r, name="@role:members") + direct_subgroups = usergroup.direct_subgroups.all() + direct_subgroup_names = {group.name for group in direct_subgroups} + return direct_subgroup_names + # test botstoragedata and botconfigdata @getter def get_botstoragedata(r: Realm) -> Dict[str, object]: