realm_domains: Type QuerySet.values as RealmDomainDict.

This ensure that the return type is compatible with the
actual type of `realm.realmdomain_set.values`.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
This commit is contained in:
Zixuan James Li 2022-06-23 14:04:16 -04:00 committed by Tim Abbott
parent ab1bbdda65
commit 8fa94138bc
3 changed files with 33 additions and 23 deletions

View File

@ -9,6 +9,7 @@ from zerver.models import (
Realm, Realm,
RealmAuditLog, RealmAuditLog,
RealmDomain, RealmDomain,
RealmDomainDict,
UserProfile, UserProfile,
active_user_ids, active_user_ids,
get_realm_domains, get_realm_domains,
@ -23,6 +24,7 @@ def do_add_realm_domain(
realm_domain = RealmDomain.objects.create( realm_domain = RealmDomain.objects.create(
realm=realm, domain=domain, allow_subdomains=allow_subdomains realm=realm, domain=domain, allow_subdomains=allow_subdomains
) )
added_domain = RealmDomainDict(domain=domain, allow_subdomains=allow_subdomains)
RealmAuditLog.objects.create( RealmAuditLog.objects.create(
realm=realm, realm=realm,
@ -32,7 +34,7 @@ def do_add_realm_domain(
extra_data=orjson.dumps( extra_data=orjson.dumps(
{ {
"realm_domains": get_realm_domains(realm), "realm_domains": get_realm_domains(realm),
"added_domain": {"domain": domain, "allow_subdomains": allow_subdomains}, "added_domain": added_domain,
} }
).decode(), ).decode(),
) )
@ -40,7 +42,7 @@ def do_add_realm_domain(
event = dict( event = dict(
type="realm_domains", type="realm_domains",
op="add", op="add",
realm_domain=dict( realm_domain=RealmDomainDict(
domain=realm_domain.domain, allow_subdomains=realm_domain.allow_subdomains domain=realm_domain.domain, allow_subdomains=realm_domain.allow_subdomains
), ),
) )
@ -55,6 +57,10 @@ def do_change_realm_domain(
) -> None: ) -> None:
realm_domain.allow_subdomains = allow_subdomains realm_domain.allow_subdomains = allow_subdomains
realm_domain.save(update_fields=["allow_subdomains"]) realm_domain.save(update_fields=["allow_subdomains"])
changed_domain = RealmDomainDict(
domain=realm_domain.domain,
allow_subdomains=realm_domain.allow_subdomains,
)
RealmAuditLog.objects.create( RealmAuditLog.objects.create(
realm=realm_domain.realm, realm=realm_domain.realm,
@ -64,10 +70,7 @@ def do_change_realm_domain(
extra_data=orjson.dumps( extra_data=orjson.dumps(
{ {
"realm_domains": get_realm_domains(realm_domain.realm), "realm_domains": get_realm_domains(realm_domain.realm),
"changed_domain": { "changed_domain": changed_domain,
"domain": realm_domain.domain,
"allow_subdomains": realm_domain.allow_subdomains,
},
} }
).decode(), ).decode(),
) )
@ -91,6 +94,10 @@ def do_remove_realm_domain(
realm = realm_domain.realm realm = realm_domain.realm
domain = realm_domain.domain domain = realm_domain.domain
realm_domain.delete() realm_domain.delete()
removed_domain = RealmDomainDict(
domain=realm_domain.domain,
allow_subdomains=realm_domain.allow_subdomains,
)
RealmAuditLog.objects.create( RealmAuditLog.objects.create(
realm=realm, realm=realm,
@ -100,10 +107,7 @@ def do_remove_realm_domain(
extra_data=orjson.dumps( extra_data=orjson.dumps(
{ {
"realm_domains": get_realm_domains(realm), "realm_domains": get_realm_domains(realm),
"removed_domain": { "removed_domain": removed_domain,
"domain": realm_domain.domain,
"allow_subdomains": realm_domain.allow_subdomains,
},
} }
).decode(), ).decode(),
) )

View File

@ -1075,7 +1075,12 @@ class EmailContainsPlusError(Exception):
pass pass
def get_realm_domains(realm: Realm) -> List[Dict[str, Union[str, bool]]]: class RealmDomainDict(TypedDict):
domain: str
allow_subdomains: bool
def get_realm_domains(realm: Realm) -> List[RealmDomainDict]:
return list(realm.realmdomain_set.values("domain", "allow_subdomains")) return list(realm.realmdomain_set.values("domain", "allow_subdomains"))

View File

@ -58,6 +58,7 @@ from zerver.models import (
Message, Message,
Realm, Realm,
RealmAuditLog, RealmAuditLog,
RealmDomainDict,
RealmPlayground, RealmPlayground,
Recipient, Recipient,
Subscription, Subscription,
@ -684,10 +685,10 @@ class TestRealmAuditLog(ZulipTestCase):
now = timezone_now() now = timezone_now()
realm_domain = do_add_realm_domain(user.realm, "zulip.org", False, acting_user=user) realm_domain = do_add_realm_domain(user.realm, "zulip.org", False, acting_user=user)
added_domain: Dict[str, Union[str, bool]] = { added_domain = RealmDomainDict(
"domain": "zulip.org", domain="zulip.org",
"allow_subdomains": False, allow_subdomains=False,
} )
expected_extra_data = { expected_extra_data = {
"realm_domains": initial_domains + [added_domain], "realm_domains": initial_domains + [added_domain],
"added_domain": added_domain, "added_domain": added_domain,
@ -705,10 +706,10 @@ class TestRealmAuditLog(ZulipTestCase):
now = timezone_now() now = timezone_now()
do_change_realm_domain(realm_domain, True, acting_user=user) do_change_realm_domain(realm_domain, True, acting_user=user)
changed_domain: Dict[str, Union[str, bool]] = { changed_domain = RealmDomainDict(
"domain": "zulip.org", domain="zulip.org",
"allow_subdomains": True, allow_subdomains=True,
} )
expected_extra_data = { expected_extra_data = {
"realm_domains": initial_domains + [changed_domain], "realm_domains": initial_domains + [changed_domain],
"changed_domain": changed_domain, "changed_domain": changed_domain,
@ -726,10 +727,10 @@ class TestRealmAuditLog(ZulipTestCase):
now = timezone_now() now = timezone_now()
do_remove_realm_domain(realm_domain, acting_user=user) do_remove_realm_domain(realm_domain, acting_user=user)
removed_domain = { removed_domain = RealmDomainDict(
"domain": "zulip.org", domain="zulip.org",
"allow_subdomains": True, allow_subdomains=True,
} )
expected_extra_data = { expected_extra_data = {
"realm_domains": initial_domains, "realm_domains": initial_domains,
"removed_domain": removed_domain, "removed_domain": removed_domain,