<commit_before>from content import models as content
from core import signals
from django.contrib import auth
from django.core import cache
from django.core.cache import utils
from entities import models as gestalten
from features.groups import models as groups
from features.memberships import models as memberships
from features.subscriptions import models as subscriptions
import itertools


def model_ids_including_none(model_class):
    return itertools.chain([None], model_class._default_manager.values_list('id', flat=True))


def invalidate_cache(fragment, *args):
    cache.cache.delete(utils.make_template_fragment_key(fragment, args))


def comments_count_changed(comment):
    invalidate_cache('conversation-preview', comment.content.id)
    for gestalt_id in model_ids_including_none(gestalten.Gestalt):
        invalidate_cache('content-detail', gestalt_id, comment.content.id)


def content_changed(content):
    invalidate_cache('content-preview', content.id)
    invalidate_cache('conversation-preview', content.id)
    for gestalt_id in model_ids_including_none(gestalten.Gestalt):
        invalidate_cache('content-detail', gestalt_id, content.id)


def gestalt_changed(gestalt):
    for group_id in model_ids_including_none(groups.Group):
        invalidate_cache('site-menu', gestalt.id, group_id)


def group_changed(group):
    invalidate_cache('sidebar-groups')
    for gestalt_id in model_ids_including_none(gestalten.Gestalt):
        invalidate_cache('group-header', gestalt_id, group.id)
        invalidate_cache('group-preview', gestalt_id, group.id)
        invalidate_cache('site-menu', gestalt_id, group.id)


def groups_count_changed(group):
    invalidate_cache('sidebar-groups')


def memberships_count_changed(membership):
    invalidate_cache('group-header', membership.member.id, membership.group.id)
    invalidate_cache('group-preview', membership.member.id, membership.group.id)
    invalidate_cache('site-menu', membership.member.id, membership.group.id)


def subscriptions_count_changed(subscription):
    if type(subscription.subscribed_to) == content.Content:
        for gestalt_id in model_ids_including_none(gestalten.Gestalt):
            invalidate_cache('content-detail', gestalt_id, subscription.subscribed_to.id)
    if type(subscription.subscribed_to) == groups.Group:
        invalidate_cache(
                'group-header', subscription.subscriber.id, subscription.subscribed_to.id)
        invalidate_cache(
                'group-preview', subscription.subscriber.id, subscription.subscribed_to.id)


def user_changed(user):
    gestalt_changed(user.gestalt)<commit_msg>Fix site-menu caching<commit_after>from content import models as content
from core import signals
from django.contrib import auth
from django.core import cache
from django.core.cache import utils
from entities import models as gestalten
from features.groups import models as groups
from features.memberships import models as memberships
from features.subscriptions import models as subscriptions
import itertools


def model_ids_including_none(model_class):
    return itertools.chain([''], model_class._default_manager.values_list('id', flat=True))


def invalidate_cache(fragment, *args):
    cache.cache.delete(utils.make_template_fragment_key(fragment, args))


def comments_count_changed(comment):
    invalidate_cache('conversation-preview', comment.content.id)
    for gestalt_id in model_ids_including_none(gestalten.Gestalt):
        invalidate_cache('content-detail', gestalt_id, comment.content.id)


def content_changed(content):
    invalidate_cache('content-preview', content.id)
    invalidate_cache('conversation-preview', content.id)
    for gestalt_id in model_ids_including_none(gestalten.Gestalt):
        invalidate_cache('content-detail', gestalt_id, content.id)


def gestalt_changed(gestalt):
    for group_id in model_ids_including_none(groups.Group):
        invalidate_cache('site-menu', gestalt.id, group_id)


def group_changed(group):
    invalidate_cache('sidebar-groups')
    for gestalt_id in model_ids_including_none(gestalten.Gestalt):
        invalidate_cache('group-header', gestalt_id, group.id)
        invalidate_cache('group-preview', gestalt_id, group.id)
        invalidate_cache('site-menu', gestalt_id, group.id)


def groups_count_changed(group):
    invalidate_cache('sidebar-groups')


def memberships_count_changed(membership):
    print('MEMBERSHIP', membership.group, membership.member)
    invalidate_cache('group-header', membership.member.id, membership.group.id)
    invalidate_cache('group-preview', membership.member.id, membership.group.id)
    for group_id in model_ids_including_none(groups.Group):
        invalidate_cache('site-menu', membership.member.id, group_id)


def subscriptions_count_changed(subscription):
    if type(subscription.subscribed_to) == content.Content:
        for gestalt_id in model_ids_including_none(gestalten.Gestalt):
            invalidate_cache('content-detail', gestalt_id, subscription.subscribed_to.id)
    if type(subscription.subscribed_to) == groups.Group:
        invalidate_cache(
                'group-header', subscription.subscriber.id, subscription.subscribed_to.id)
        invalidate_cache(
                'group-preview', subscription.subscriber.id, subscription.subscribed_to.id)


def user_changed(user):
    gestalt_changed(user.gestalt)
{'fraction_non_alphanumeric': 0.09386617100371747, 'fraction_numerical': 0.0, 'mean_word_length': 5.142694063926941, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4909312', 'n_tokens_mistral': 1579, 'n_tokens_neox': 1554, 'n_words': 304}