models: Extract zerver.models.alert_words.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2023-12-15 12:04:40 -08:00 committed by Tim Abbott
parent 1f1b2f9a68
commit 33d140c8dc
4 changed files with 43 additions and 37 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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