users: Move query into caller of get_custom_profile_field_values.

This will be useful for supporting a smaller query for a single user.
This commit is contained in:
Tim Abbott
2020-02-06 17:36:55 -08:00
parent 79e5dd1374
commit aa9286a1f9

View File

@@ -15,8 +15,7 @@ from zerver.lib.avatar import avatar_url, get_avatar_field
from zerver.lib.exceptions import OrganizationAdministratorRequired from zerver.lib.exceptions import OrganizationAdministratorRequired
from zerver.models import UserProfile, Service, Realm, \ from zerver.models import UserProfile, Service, Realm, \
get_user_profile_by_id_in_realm, CustomProfileFieldValue, \ get_user_profile_by_id_in_realm, CustomProfileFieldValue, \
get_realm_user_dicts, \ get_realm_user_dicts, CustomProfileField
CustomProfileField
from zulip_bots.custom_exceptions import ConfigValidationError from zulip_bots.custom_exceptions import ConfigValidationError
@@ -375,10 +374,8 @@ def get_cross_realm_dicts() -> List[Dict[str, Any]]:
return result return result
def get_custom_profile_field_values(realm_id: int) -> Dict[int, Dict[str, Any]]: def get_custom_profile_field_values(custom_profile_field_values:
# TODO: Consider optimizing this query away with caching. List[CustomProfileFieldValue]) -> Dict[int, Dict[str, Any]]:
custom_profile_field_values = CustomProfileFieldValue.objects.select_related(
"field").filter(user_profile__realm_id=realm_id)
profiles_by_user_id = defaultdict(dict) # type: Dict[int, Dict[str, Any]] profiles_by_user_id = defaultdict(dict) # type: Dict[int, Dict[str, Any]]
for profile_field in custom_profile_field_values: for profile_field in custom_profile_field_values:
user_id = profile_field.user_profile_id user_id = profile_field.user_profile_id
@@ -400,7 +397,11 @@ def get_raw_user_data(realm: Realm, acting_user: UserProfile, client_gravatar: b
custom_profile_field_data = None custom_profile_field_data = None
if include_custom_profile_fields: if include_custom_profile_fields:
profiles_by_user_id = get_custom_profile_field_values(realm.id) base_query = CustomProfileFieldValue.objects.select_related("field")
# TODO: Consider optimizing this query away with caching.
custom_profile_field_values = base_query.filter(user_profile__realm_id=realm.id)
profiles_by_user_id = get_custom_profile_field_values(custom_profile_field_values)
result = {} result = {}
for row in user_dicts: for row in user_dicts:
if profiles_by_user_id is not None: if profiles_by_user_id is not None: