custom_profile: Apply ProfileDataElementUpdateDict.

We explicitly annotate variables or parameters with
`ProfileDataElementUpdateDict` as necessary.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
This commit is contained in:
Zixuan James Li 2022-07-08 11:17:46 -04:00 committed by Tim Abbott
parent 5116efa3de
commit 52be020d0c
5 changed files with 15 additions and 15 deletions

View File

@ -7,7 +7,7 @@ from django.utils.translation import gettext as _
from zerver.lib.exceptions import JsonableError
from zerver.lib.external_accounts import DEFAULT_EXTERNAL_ACCOUNTS
from zerver.lib.streams import render_stream_description
from zerver.lib.types import ProfileDataElementValue, ProfileFieldData
from zerver.lib.types import ProfileDataElementUpdateDict, ProfileFieldData
from zerver.models import (
CustomProfileField,
CustomProfileFieldValue,
@ -122,7 +122,7 @@ def notify_user_update_custom_profile_data(
def do_update_user_custom_profile_data_if_changed(
user_profile: UserProfile,
data: List[Dict[str, Union[int, ProfileDataElementValue]]],
data: List[ProfileDataElementUpdateDict],
) -> None:
with transaction.atomic():
for custom_profile_field in data:

View File

@ -1,7 +1,7 @@
import re
import unicodedata
from collections import defaultdict
from typing import Any, Dict, Iterable, List, Optional, Sequence, TypedDict, Union, cast
from typing import Any, Dict, Iterable, List, Optional, Sequence, TypedDict, Union
import dateutil.parser as date_parser
from django.conf import settings
@ -26,7 +26,7 @@ from zerver.lib.exceptions import (
OrganizationOwnerRequired,
)
from zerver.lib.timezone import canonicalize_timezone
from zerver.lib.types import ProfileDataElementValue
from zerver.lib.types import ProfileDataElementUpdateDict, ProfileDataElementValue
from zerver.models import (
CustomProfileField,
CustomProfileFieldValue,
@ -378,7 +378,7 @@ def validate_user_custom_profile_field(
def validate_user_custom_profile_data(
realm_id: int, profile_data: List[Dict[str, Union[int, ProfileDataElementValue]]]
realm_id: int, profile_data: List[ProfileDataElementUpdateDict]
) -> None:
# This function validate all custom field values according to their field type.
for item in profile_data:
@ -389,9 +389,7 @@ def validate_user_custom_profile_data(
raise JsonableError(_("Field id {id} not found.").format(id=field_id))
try:
validate_user_custom_profile_field(
realm_id, field, cast(ProfileDataElementValue, item["value"])
)
validate_user_custom_profile_field(realm_id, field, item["value"])
except ValidationError as error:
raise JsonableError(error.message)

View File

@ -13,6 +13,7 @@ from zerver.lib.external_accounts import DEFAULT_EXTERNAL_ACCOUNTS
from zerver.lib.markdown import markdown_convert
from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.test_helpers import queries_captured
from zerver.lib.types import ProfileDataElementUpdateDict
from zerver.models import (
CustomProfileField,
CustomProfileFieldValue,
@ -374,7 +375,7 @@ class DeleteCustomProfileFieldTest(CustomProfileFieldTestCase):
self.assert_json_error(result, f"Field id {invalid_field_id} not found.")
field = CustomProfileField.objects.get(name="Mentor", realm=realm)
data: List[Dict[str, Union[int, str, List[int]]]] = [
data: List[ProfileDataElementUpdateDict] = [
{"id": field.id, "value": [self.example_user("aaron").id]},
]
do_update_user_custom_profile_data_if_changed(iago, data)
@ -404,7 +405,7 @@ class DeleteCustomProfileFieldTest(CustomProfileFieldTestCase):
user_profile = self.example_user("iago")
realm = user_profile.realm
field = CustomProfileField.objects.get(name="Phone number", realm=realm)
data: List[Dict[str, Union[int, str, List[int]]]] = [
data: List[ProfileDataElementUpdateDict] = [
{"id": field.id, "value": "123456"},
]
do_update_user_custom_profile_data_if_changed(user_profile, data)
@ -693,7 +694,7 @@ class UpdateCustomProfileFieldTest(CustomProfileFieldTestCase):
self.assertIsNone(value)
self.assertIsNone(rendered_value)
update_dict: Dict[str, Union[int, str, List[int]]] = {
update_dict: ProfileDataElementUpdateDict = {
"id": quote.id,
"value": "***beware*** of jealousy...",
}
@ -713,7 +714,7 @@ class UpdateCustomProfileFieldTest(CustomProfileFieldTestCase):
# Set field value:
field = CustomProfileField.objects.get(name="Mentor", realm=realm)
data: List[Dict[str, Union[int, str, List[int]]]] = [
data: List[ProfileDataElementUpdateDict] = [
{"id": field.id, "value": [self.example_user("aaron").id]},
]
do_update_user_custom_profile_data_if_changed(iago, data)

View File

@ -199,6 +199,7 @@ from zerver.lib.test_helpers import (
stdout_suppressed,
)
from zerver.lib.topic import TOPIC_NAME
from zerver.lib.types import ProfileDataElementUpdateDict
from zerver.lib.user_groups import create_user_group
from zerver.lib.user_mutes import get_mute_object
from zerver.models import (
@ -1013,7 +1014,7 @@ class NormalActionsTest(BaseAction):
field_id = self.user_profile.realm.customprofilefield_set.get(
realm=self.user_profile.realm, name="Biography"
).id
field = {
field: ProfileDataElementUpdateDict = {
"id": field_id,
"value": "New value",
}

View File

@ -86,7 +86,7 @@ from zerver.lib.redis_utils import get_dict_from_redis, get_redis_client, put_di
from zerver.lib.request import RequestNotes
from zerver.lib.sessions import delete_user_sessions
from zerver.lib.subdomains import get_subdomain
from zerver.lib.types import ProfileDataElementValue
from zerver.lib.types import ProfileDataElementUpdateDict
from zerver.lib.url_encoding import append_url_query_string
from zerver.lib.users import check_full_name, validate_user_custom_profile_field
from zerver.models import (
@ -1361,7 +1361,7 @@ def sync_user_profile_custom_fields(
var_name = "_".join(data["name"].lower().split(" "))
existing_values[var_name] = data["value"]
profile_data: List[Dict[str, Union[int, ProfileDataElementValue]]] = []
profile_data: List[ProfileDataElementUpdateDict] = []
for var_name, value in custom_field_name_to_value.items():
try:
field = fields_by_var_name[var_name]