Files
zulip/zerver/lib/alert_words.py
Leo Franchi b2ddd670e4 [schema] Add backend support for per-user alert words
(imported from commit 7a9c596a010cbedbddf594c5d9c68bb9ed46d122)
2013-09-05 10:18:40 -04:00

35 lines
1.0 KiB
Python

import re
import zerver.models
import itertools
import logging
import ujson
def alert_words_in_realm(realm):
users = zerver.models.UserProfile.objects.filter(realm=realm, is_active=True)
all_user_words = dict((user, user_alert_words(user)) for user in users)
users_with_words = dict((u, w) for (u, w) in all_user_words.iteritems() if len(w))
return users_with_words
def user_alert_words(user_profile):
return ujson.loads(user_profile.alert_words)
def add_user_alert_words(user_profile, alert_words):
words = user_alert_words(user_profile)
new_words = [w for w in alert_words if not w in words]
words.extend(new_words)
set_user_alert_words(user_profile, words)
def remove_user_alert_words(user_profile, alert_words):
words = user_alert_words(user_profile)
words = [w for w in words if not w in alert_words]
set_user_alert_words(user_profile, words)
def set_user_alert_words(user_profile, alert_words):
user_profile.alert_words = ujson.dumps(alert_words)
user_profile.save(update_fields=['alert_words'])