diff --git a/zerver/lib/alert_words.py b/zerver/lib/alert_words.py index 3466789681..70544049d6 100644 --- a/zerver/lib/alert_words.py +++ b/zerver/lib/alert_words.py @@ -8,7 +8,8 @@ from zerver.lib.cache import ( realm_alert_words_automaton_cache_key, realm_alert_words_cache_key, ) -from zerver.models import AlertWord, Realm, UserProfile, flush_realm_alert_words +from zerver.models import AlertWord, Realm, UserProfile +from zerver.models.alert_words import flush_realm_alert_words @cache_with_key(lambda realm: realm_alert_words_cache_key(realm.id), timeout=3600 * 24) diff --git a/zerver/models/__init__.py b/zerver/models/__init__.py index 2898b0bcd8..149902cfd0 100644 --- a/zerver/models/__init__.py +++ b/zerver/models/__init__.py @@ -2,17 +2,11 @@ from typing import List, Tuple, TypeVar, Union from django.db import models from django.db.backends.base.base import BaseDatabaseWrapper -from django.db.models import CASCADE -from django.db.models.signals import post_delete, post_save from django.db.models.sql.compiler import SQLCompiler from django_stubs_ext import ValuesQuerySet from typing_extensions import override -from zerver.lib.cache import ( - cache_delete, - realm_alert_words_automaton_cache_key, - realm_alert_words_cache_key, -) +from zerver.models.alert_words import AlertWord as AlertWord from zerver.models.bots import BotConfigData as BotConfigData from zerver.models.bots import BotStorageData as BotStorageData from zerver.models.bots import Service as Service @@ -131,31 +125,3 @@ def query_for_ids( params=(tuple(user_ids),), ) return query - - -class AlertWord(models.Model): - # Realm isn't necessary, but it's a nice denormalization. Users - # never move to another realm, so it's static, and having Realm - # here optimizes the main query on this table, which is fetching - # all the alert words in a realm. - realm = models.ForeignKey(Realm, db_index=True, on_delete=CASCADE) - user_profile = models.ForeignKey(UserProfile, on_delete=CASCADE) - # Case-insensitive name for the alert word. - word = models.TextField() - - class Meta: - unique_together = ("user_profile", "word") - - -def flush_realm_alert_words(realm_id: int) -> None: - cache_delete(realm_alert_words_cache_key(realm_id)) - cache_delete(realm_alert_words_automaton_cache_key(realm_id)) - - -def flush_alert_word(*, instance: AlertWord, **kwargs: object) -> None: - realm_id = instance.realm_id - flush_realm_alert_words(realm_id) - - -post_save.connect(flush_alert_word, sender=AlertWord) -post_delete.connect(flush_alert_word, sender=AlertWord) diff --git a/zerver/models/alert_words.py b/zerver/models/alert_words.py new file mode 100644 index 0000000000..ec04c3400b --- /dev/null +++ b/zerver/models/alert_words.py @@ -0,0 +1,39 @@ +from django.db import models +from django.db.models import CASCADE +from django.db.models.signals import post_delete, post_save + +from zerver.lib.cache import ( + cache_delete, + realm_alert_words_automaton_cache_key, + realm_alert_words_cache_key, +) +from zerver.models.realms import Realm +from zerver.models.users import UserProfile + + +class AlertWord(models.Model): + # Realm isn't necessary, but it's a nice denormalization. Users + # never move to another realm, so it's static, and having Realm + # here optimizes the main query on this table, which is fetching + # all the alert words in a realm. + realm = models.ForeignKey(Realm, db_index=True, on_delete=CASCADE) + user_profile = models.ForeignKey(UserProfile, on_delete=CASCADE) + # Case-insensitive name for the alert word. + word = models.TextField() + + class Meta: + unique_together = ("user_profile", "word") + + +def flush_realm_alert_words(realm_id: int) -> None: + cache_delete(realm_alert_words_cache_key(realm_id)) + cache_delete(realm_alert_words_automaton_cache_key(realm_id)) + + +def flush_alert_word(*, instance: AlertWord, **kwargs: object) -> None: + realm_id = instance.realm_id + flush_realm_alert_words(realm_id) + + +post_save.connect(flush_alert_word, sender=AlertWord) +post_delete.connect(flush_alert_word, sender=AlertWord) diff --git a/zilencer/management/commands/populate_db.py b/zilencer/management/commands/populate_db.py index c6ec4b0588..ad24b409f3 100644 --- a/zilencer/management/commands/populate_db.py +++ b/zilencer/management/commands/populate_db.py @@ -64,8 +64,8 @@ from zerver.models import ( UserMessage, UserPresence, UserProfile, - flush_alert_word, ) +from zerver.models.alert_words import flush_alert_word from zerver.models.clients import get_client from zerver.models.realms import get_realm from zerver.models.recipients import get_or_create_huddle