def create_alert_words(realm_id: int) -> None: user_ids = UserProfile.objects.filter( realm_id=realm_id, is_bot=False, is_active=True, ).values_list("id", flat=True) alert_words = [ "algorithms", "complexity", "founded", "galaxy", "grammar", "illustrious", "natural", "objective", "people", "robotics", "study", ] recs: List[AlertWord] = [] for user_id in user_ids: random.shuffle(alert_words) for i in range(4): recs.append( AlertWord( realm_id=realm_id, user_profile_id=user_id, word=alert_words[i], )) AlertWord.objects.bulk_create(recs)
def create_alert_words(realm_id: int) -> None: user_ids = UserProfile.objects.filter( realm_id=realm_id, is_bot=False, is_active=True, ).values_list('id', flat=True) alert_words = [ 'algorithms', 'complexity', 'founded', 'galaxy', 'grammar', 'illustrious', 'natural', 'objective', 'people', 'robotics', 'study', ] recs: List[AlertWord] = [] for user_id in user_ids: random.shuffle(alert_words) for i in range(4): recs.append( AlertWord( realm_id=realm_id, user_profile_id=user_id, word=alert_words[i], )) AlertWord.objects.bulk_create(recs)
def add_user_alert_words(user_profile: UserProfile, new_words: Iterable[str]) -> List[str]: existing_words_lower = {word.lower() for word in user_alert_words(user_profile)} # Keeping the case, use a dictionary to get the set of # case-insensitive distinct, new alert words word_dict: Dict[str, str] = {} for word in new_words: if word.lower() in existing_words_lower: continue word_dict[word.lower()] = word AlertWord.objects.bulk_create( AlertWord(user_profile=user_profile, word=word, realm=user_profile.realm) for word in word_dict.values() ) # Django bulk_create operations don't flush caches, so we need to do this ourselves. flush_realm_alert_words(user_profile.realm) return user_alert_words(user_profile)