Extract changed() helper in flush_user_profile().

The verbose style of `changed` is partly to appease mypy.
This commit is contained in:
Steve Howell
2017-10-21 18:14:20 -07:00
committed by Tim Abbott
parent 70c053d86f
commit c8875693c8

View File

@@ -7,7 +7,7 @@ from django.conf import settings
from django.db.models import Q
from django.core.cache.backends.base import BaseCache
from typing import Any, Callable, Dict, Iterable, List, Optional, Union, TypeVar, Text
from typing import Any, Callable, Dict, Iterable, List, Optional, Union, Set, TypeVar, Text
from zerver.lib.utils import statsd, statsd_key, make_safe_digest
import subprocess
@@ -380,15 +380,25 @@ def flush_user_profile(sender, **kwargs):
user_profile = kwargs['instance']
delete_user_profile_caches([user_profile])
def changed(fields):
# type: (List[str]) -> bool
if kwargs.get('update_fields') is None:
# adds/deletes should invalidate the cache
return True
update_fields = set(kwargs['update_fields'])
for f in fields:
if f in update_fields:
return True
return False
# Invalidate our active_users_in_realm info dict if any user has changed
# the fields in the dict or become (in)active
if kwargs.get('update_fields') is None or \
len(set(realm_user_dict_fields) &
set(kwargs['update_fields'])) > 0:
if changed(realm_user_dict_fields):
cache_delete(realm_user_dicts_cache_key(user_profile.realm_id))
if kwargs.get('update_fields') is None or \
('is_active' in kwargs['update_fields']):
if changed(['is_active']):
cache_delete(active_user_ids_cache_key(user_profile.realm_id))
if kwargs.get('updated_fields') is None or \
@@ -397,13 +407,12 @@ def flush_user_profile(sender, **kwargs):
# Invalidate our bots_in_realm info dict if any bot has
# changed the fields in the dict or become (in)active
if user_profile.is_bot and (kwargs['update_fields'] is None or
(set(bot_dict_fields) & set(kwargs['update_fields']))):
if user_profile.is_bot and changed(bot_dict_fields):
cache_delete(bot_dicts_in_realm_cache_key(user_profile.realm))
# Invalidate realm-wide alert words cache if any user in the realm has changed
# alert words
if kwargs.get('update_fields') is None or "alert_words" in kwargs['update_fields']:
if changed(['alert_words']):
cache_delete(realm_alert_words_cache_key(user_profile.realm))
# Called by models.py to flush various caches whenever we save