<commit_before># -*- coding: utf-8 -*-
"""Signal handlers for API models."""

from __future__ import unicode_literals

from .models import Feature, Section


def feature_sections_changed_update_order(
        sender, instance, action, reverse, model, pk_set, **kwargs):
    """Maintain feature.section_order."""
    if action not in ('post_add', 'post_remove', 'post_clear'):
        # post_clear is not handled, because clear is called in
        # django.db.models.fields.related.ReverseManyRelatedObjects.__set__
        # before setting the new order
        return
    if getattr(instance, '_delay_cache', False):
        return

    if model == Section:
        assert type(instance) == Feature
        features = [instance]
        if pk_set:
            sections = list(Section.objects.filter(pk__in=pk_set))
        else:
            sections = []
    else:
        if pk_set:
            features = list(Feature.objects.filter(pk__in=pk_set))
        else:
            features = []
        sections = [instance]

    from .tasks import update_cache_for_instance
    for feature in features:
        update_cache_for_instance('Feature', feature.pk, feature)
    for section in sections:
        update_cache_for_instance('Section', section.pk, section)


def post_delete_update_cache(sender, instance, **kwargs):
    """Invalidate the cache when an instance is deleted."""
    name = sender.__name__
    delay_cache = getattr(instance, '_delay_cache', False)
    if not delay_cache:
        from .tasks import update_cache_for_instance
        update_cache_for_instance(name, instance.pk, instance)


def post_save_update_cache(sender, instance, created, raw, **kwargs):
    """Invalidate the cache when an instance is created or updated."""
    if raw:
        return
    name = sender.__name__
    delay_cache = getattr(instance, '_delay_cache', False)
    if not delay_cache:
        from .tasks import update_cache_for_instance
        update_cache_for_instance(name, instance.pk, instance)


def add_user_to_change_resource_group(
        signal, sender, instance, created, raw, **kwargs):
    """Add change-resource permission to new users."""
    if created and not raw:
        from django.contrib.auth.models import Group
        instance.groups.add(Group.objects.get(name='change-resource'))
        post_save_update_cache(sender, instance, created, raw, **kwargs)<commit_msg>bug 1247690 - No pre-populated cache for new users<commit_after># -*- coding: utf-8 -*-
"""Signal handlers for API models."""

from __future__ import unicode_literals

from .models import Feature, Section


def feature_sections_changed_update_order(
        sender, instance, action, reverse, model, pk_set, **kwargs):
    """Maintain feature.section_order."""
    if action not in ('post_add', 'post_remove', 'post_clear'):
        # post_clear is not handled, because clear is called in
        # django.db.models.fields.related.ReverseManyRelatedObjects.__set__
        # before setting the new order
        return
    if getattr(instance, '_delay_cache', False):
        return

    if model == Section:
        assert type(instance) == Feature
        features = [instance]
        if pk_set:
            sections = list(Section.objects.filter(pk__in=pk_set))
        else:
            sections = []
    else:
        if pk_set:
            features = list(Feature.objects.filter(pk__in=pk_set))
        else:
            features = []
        sections = [instance]

    from .tasks import update_cache_for_instance
    for feature in features:
        update_cache_for_instance('Feature', feature.pk, feature)
    for section in sections:
        update_cache_for_instance('Section', section.pk, section)


def post_delete_update_cache(sender, instance, **kwargs):
    """Invalidate the cache when an instance is deleted."""
    name = sender.__name__
    delay_cache = getattr(instance, '_delay_cache', False)
    if not delay_cache:
        from .tasks import update_cache_for_instance
        update_cache_for_instance(name, instance.pk, instance)


def post_save_update_cache(sender, instance, created, raw, **kwargs):
    """Invalidate the cache when an instance is created or updated."""
    if raw:
        return
    name = sender.__name__
    if name == 'User' and created:
        return
    delay_cache = getattr(instance, '_delay_cache', False)
    if not delay_cache:
        from .tasks import update_cache_for_instance
        update_cache_for_instance(name, instance.pk, instance)


def add_user_to_change_resource_group(
        signal, sender, instance, created, raw, **kwargs):
    """Add change-resource permission to new users."""
    if created and not raw:
        from django.contrib.auth.models import Group
        instance.groups.add(Group.objects.get(name='change-resource'))
{'fraction_non_alphanumeric': 0.09550561797752809, 'fraction_numerical': 0.0018726591760299626, 'mean_word_length': 3.1191088260497, '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': '2986292', 'n_tokens_mistral': 1342, 'n_tokens_neox': 1315, 'n_words': 385}