<commit_before>import os
import errno

from django.conf import settings
from django.core.cache.utils import make_template_fragment_key
from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver
from django.core.cache import cache

from .models import Problem, Contest, Submission, Organization, Profile, MiscConfig, Language, Judge, \
    BlogPost, ContestSubmission, Comment, License
from .caching import finished_submission


@receiver(post_save, sender=Problem)
def problem_update(sender, instance, **kwargs):
    cache.delete_many([
        make_template_fragment_key('problem_html', (instance.id, True)),
        make_template_fragment_key('problem_html', (instance.id, False)),
        make_template_fragment_key('problem_authors', (instance.id,)),
        make_template_fragment_key('submission_problem', (instance.id,)),
        make_template_fragment_key('problem_feed', (instance.id,))
    ])

    if hasattr(settings, 'PROBLEM_PDF_CACHE'):
        try:
            os.unlink(os.path.join(settings.PROBLEM_PDF_CACHE, '%s.pdf' % instance.code))
        except OSError as e:
            if e.errno != errno.ENOENT:
                raise
        try:
            os.unlink(os.path.join(settings.PROBLEM_PDF_CACHE, '%s.log' % instance.code))
        except OSError as e:
            if e.errno != errno.ENOENT:
                raise


@receiver(post_save, sender=Profile)
def profile_update(sender, instance, **kwargs):
    cache.delete_many([
        make_template_fragment_key('submission_user', (instance.id,)),
        make_template_fragment_key('org_member_count', (instance.organization_id,))
    ])


@receiver(post_save, sender=Contest)
def contest_update(sender, instance, **kwargs):
    cache.delete(make_template_fragment_key('contest_html', (instance.id,)))


@receiver(post_save, sender=License)
def license_update(sender, instance, **kwargs):
    cache.delete(make_template_fragment_key('license_html', (instance.id,)))


@receiver(post_save, sender=Language)
def language_update(sender, instance, **kwargs):
    cache.delete(make_template_fragment_key('language_html', (instance.id,)))


@receiver(post_save, sender=Judge)
def language_update(sender, instance, **kwargs):
    cache.delete(make_template_fragment_key('judge_html', (instance.id,)))


@receiver(post_save, sender=Comment)
def comment_update(sender, instance, **kwargs):
    cache.delete('comment_feed:%d' % instance.id)


@receiver(post_save, sender=BlogPost)
def post_update(sender, instance, **kwargs):<commit_msg>Signal needs to handle multiple organizations<commit_after>import os
import errno

from django.conf import settings
from django.core.cache.utils import make_template_fragment_key
from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver
from django.core.cache import cache

from .models import Problem, Contest, Submission, Organization, Profile, MiscConfig, Language, Judge, \
    BlogPost, ContestSubmission, Comment, License
from .caching import finished_submission


@receiver(post_save, sender=Problem)
def problem_update(sender, instance, **kwargs):
    cache.delete_many([
        make_template_fragment_key('problem_html', (instance.id, True)),
        make_template_fragment_key('problem_html', (instance.id, False)),
        make_template_fragment_key('problem_authors', (instance.id,)),
        make_template_fragment_key('submission_problem', (instance.id,)),
        make_template_fragment_key('problem_feed', (instance.id,))
    ])

    if hasattr(settings, 'PROBLEM_PDF_CACHE'):
        try:
            os.unlink(os.path.join(settings.PROBLEM_PDF_CACHE, '%s.pdf' % instance.code))
        except OSError as e:
            if e.errno != errno.ENOENT:
                raise
        try:
            os.unlink(os.path.join(settings.PROBLEM_PDF_CACHE, '%s.log' % instance.code))
        except OSError as e:
            if e.errno != errno.ENOENT:
                raise


@receiver(post_save, sender=Profile)
def profile_update(sender, instance, **kwargs):
    cache.delete_many([make_template_fragment_key('submission_user', (instance.id,))] +
                      [make_template_fragment_key('org_member_count', (org_id,))
                       for org_id in instance.organizations.values_list('id', flat=True)])


@receiver(post_save, sender=Contest)
def contest_update(sender, instance, **kwargs):
    cache.delete(make_template_fragment_key('contest_html', (instance.id,)))


@receiver(post_save, sender=License)
def license_update(sender, instance, **kwargs):
    cache.delete(make_template_fragment_key('license_html', (instance.id,)))


@receiver(post_save, sender=Language)
def language_update(sender, instance, **kwargs):
    cache.delete(make_template_fragment_key('language_html', (instance.id,)))


@receiver(post_save, sender=Judge)
def language_update(sender, instance, **kwargs):
    cache.delete(make_template_fragment_key('judge_html', (instance.id,)))


@receiver(post_save, sender=Comment)
def comment_update(sender, instance, **kwargs):
    cache.delete('comment_feed:%d' % instance.id)


@receiver(post_save, sender=BlogPost)
def post_update(sender, instance, **kwargs):
{'fraction_non_alphanumeric': 0.12094907407407407, 'fraction_numerical': 0.0, 'mean_word_length': 4.748337028824833, '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': '1407036', 'n_tokens_mistral': 1612, 'n_tokens_neox': 1552, 'n_words': 302}