export: Do not export direct_members and direct_subgroups field.

We do not need direct_members and direct_subgroups field of
UserGroup objects in the export data since we already have
UserGroupMembership and GroupGroupMembership object data.

While importing we keep these fields empty when creating
UserGroup objects and direct_members and direct_subgroups
fields will get set when UserGroupMembership and
GroupGroupMembership objects are created.

This change will also help us in further changes when we
will change the order of importing to import UserGroup
objects just after Realm objects.
This commit is contained in:
Sahil Batra 2022-09-12 13:36:51 +05:30 committed by Tim Abbott
parent c714ad993b
commit 1e55e7641e
3 changed files with 29 additions and 6 deletions

View File

@ -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(

View File

@ -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)

View File

@ -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]: