models: Rename get_huddle to get_or_create_huddle.

Small follow-up to d86e4ac34d.
get_ makes it sound like it doesn't have side-effects, when these are
actually much like the django ORM .get_or_create function.
This commit is contained in:
Mateusz Mandera 2022-09-27 18:43:07 +02:00 committed by Tim Abbott
parent 403e59622b
commit 065b59213b
2 changed files with 6 additions and 6 deletions

View File

@ -2811,7 +2811,7 @@ def get_huddle_recipient(user_profile_ids: Set[int]) -> Recipient:
# the sender. Note that get_huddle hits the cache, and then # the sender. Note that get_huddle hits the cache, and then
# we hit another cache to get the recipient. We may want to # we hit another cache to get the recipient. We may want to
# unify our caching strategy here. # unify our caching strategy here.
huddle = get_huddle(list(user_profile_ids)) huddle = get_or_create_huddle(list(user_profile_ids))
assert huddle.recipient is not None assert huddle.recipient is not None
return huddle.recipient return huddle.recipient
@ -3982,20 +3982,20 @@ def huddle_hash_cache_key(huddle_hash: str) -> str:
return f"huddle_by_hash:{huddle_hash}" return f"huddle_by_hash:{huddle_hash}"
def get_huddle(id_list: List[int]) -> Huddle: def get_or_create_huddle(id_list: List[int]) -> Huddle:
""" """
Takes a list of user IDs and returns the Huddle object for the Takes a list of user IDs and returns the Huddle object for the
group consisting of these users. If the Huddle object does not group consisting of these users. If the Huddle object does not
yet exist, it will be transparently created. yet exist, it will be transparently created.
""" """
huddle_hash = get_huddle_hash(id_list) huddle_hash = get_huddle_hash(id_list)
return get_huddle_backend(huddle_hash, id_list) return get_or_create_huddle_backend(huddle_hash, id_list)
@cache_with_key( @cache_with_key(
lambda huddle_hash, id_list: huddle_hash_cache_key(huddle_hash), timeout=3600 * 24 * 7 lambda huddle_hash, id_list: huddle_hash_cache_key(huddle_hash), timeout=3600 * 24 * 7
) )
def get_huddle_backend(huddle_hash: str, id_list: List[int]) -> Huddle: def get_or_create_huddle_backend(huddle_hash: str, id_list: List[int]) -> Huddle:
with transaction.atomic(): with transaction.atomic():
(huddle, created) = Huddle.objects.get_or_create(huddle_hash=huddle_hash) (huddle, created) = Huddle.objects.get_or_create(huddle_hash=huddle_hash)
if created: if created:

View File

@ -60,7 +60,7 @@ from zerver.models import (
UserPresence, UserPresence,
UserProfile, UserProfile,
get_client, get_client,
get_huddle, get_or_create_huddle,
get_realm, get_realm,
get_stream, get_stream,
get_user, get_user,
@ -793,7 +793,7 @@ class Command(BaseCommand):
# Create several initial huddles # Create several initial huddles
for i in range(options["num_huddles"]): for i in range(options["num_huddles"]):
get_huddle(random.sample(user_profiles_ids, random.randint(3, 4))) get_or_create_huddle(random.sample(user_profiles_ids, random.randint(3, 4)))
# Create several initial pairs for personals # Create several initial pairs for personals
personals_pairs = [ personals_pairs = [