From c5edb58cd47f59eaa84a499f4a0cba8209d0ff82 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Tue, 28 Jan 2014 12:18:19 -0500 Subject: [PATCH] Turn flush_realm() into a post-save hook for Realm. (imported from commit 97e44b51e2b35b4b5b64b4008f6a8d3585a26e33) --- zerver/lib/actions.py | 4 +--- zerver/lib/cache.py | 6 +++++- zerver/models.py | 4 +++- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index 63db57cd19..c3b5699431 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -3,7 +3,7 @@ from __future__ import absolute_import from django.conf import settings from django.core import validators from django.contrib.sessions.models import Session -from zerver.lib.cache import flush_user_profile, flush_realm +from zerver.lib.cache import flush_user_profile from zerver.lib.context_managers import lockfile from zerver.models import Realm, RealmEmoji, Stream, UserProfile, UserActivity, \ Subscription, Recipient, Message, UserMessage, valid_stream_name, \ @@ -167,8 +167,6 @@ def do_deactivate_realm(realm): # notice when they try to log in. delete_user_sessions(user) - flush_realm(realm) - def do_deactivate_user(user_profile, log=True, _cascade=True): if not user_profile.is_active: return diff --git a/zerver/lib/cache.py b/zerver/lib/cache.py index 1ee5eb401c..c29df8264d 100644 --- a/zerver/lib/cache.py +++ b/zerver/lib/cache.py @@ -272,7 +272,11 @@ def flush_user_profile(sender, **kwargs): if kwargs['update_fields'] is None or "alert_words" in kwargs['update_fields']: cache_delete(realm_alert_words_cache_key(user_profile.realm)) -def flush_realm(realm): +# Called by models.py to flush various caches whenever we save +# a Realm object. The main tricky thing here is that Realm info is +# generally cached indirectly through user_profile objects. +def flush_realm(sender, **kwargs): + realm = kwargs['instance'] users = realm.get_active_users() update_user_profile_caches(users) diff --git a/zerver/models.py b/zerver/models.py index 9641a3f4ad..4614a0fcc6 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -4,7 +4,7 @@ from django.db import models from django.conf import settings from django.contrib.auth.models import AbstractBaseUser, UserManager, \ PermissionsMixin -from zerver.lib.cache import cache_with_key, flush_user_profile, \ +from zerver.lib.cache import cache_with_key, flush_user_profile, flush_realm, \ user_profile_by_id_cache_key, user_profile_by_email_cache_key, \ generic_bulk_cached_fetch, cache_set, update_stream_cache, \ display_recipient_cache_key, cache_delete, \ @@ -140,6 +140,8 @@ class Realm(models.Model): ('administer', "Administer a realm"), ) +post_save.connect(flush_realm, sender=Realm) + class RealmAlias(models.Model): realm = models.ForeignKey(Realm, null=True) domain = models.CharField(max_length=80, db_index=True, unique=True)