<commit_before>import errno
import os

from django.conf import settings
from django.core.cache import cache
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 .caching import finished_submission
from .models import Problem, Contest, Submission, Organization, Profile, MiscConfig, Language, Judge, \
    BlogPost, ContestSubmission, Comment, License, EFFECTIVE_MATH_ENGINES


def get_pdf_path(basename):
    return os.path.join(settings.PROBLEM_PDF_CACHE, basename)


def unlink_if_exists(file):
    try:
        os.unlink(file)
    except OSError as e:
        if e.errno != errno.ENOENT:
            raise


@receiver(post_save, sender=Problem)
def problem_update(sender, instance, **kwargs):
    if hasattr(instance, '_updating_stats_only'):
        return

    cache.delete_many([
        make_template_fragment_key('submission_problem', (instance.id,)),
        make_template_fragment_key('problem_feed', (instance.id,)),
        'problem_tls:%s' % instance.id, 'problem_mls:%s' % instance.id,
    ])
    cache.delete_many([
        make_template_fragment_key('problem_html', (instance.id, engine, lang))
        for lang, _ in settings.LANGUAGES for engine in EFFECTIVE_MATH_ENGINES
    ])
    cache.delete_many([
        make_template_fragment_key('problem_authors', (instance.id, lang))
        for lang, _ in settings.LANGUAGES
    ])

    if hasattr(settings, 'PROBLEM_PDF_CACHE'):
        for lang, _ in settings.LANGUAGES:
            unlink_if_exists(get_pdf_path('%s.%s.pdf' % (instance.code, lang)))
            unlink_if_exists(get_pdf_path('%s.%s.log' % (instance.code, lang)))


@receiver(post_save, sender=Profile)
def profile_update(sender, instance, **kwargs):
    if hasattr(instance, '_updating_stats_only'):
        return

    cache.delete_many([make_template_fragment_key('user_about', (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):
    if hasattr(instance, '_updating_stats_only'):
        return

    cache.delete_many(['generated-meta:%d' % instance.id] +
                      [make_template_fragment_key('contest_html', (instance.id, engine))
                       for engine in EFFECTIVE_MATH_ENGINES])


@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_many([make_template_fragment_key('language_html', (instance.id,)),
                       'lang:cn_map'])


@receiver(post_save, sender=Judge)
def judge_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):
    cache.delete_many([
        make_template_fragment_key('post_summary', (instance.id,)),
        make_template_fragment_key('post_content', (instance.id,)),
        'blog_slug:%d' % instance.id,
        'blog_feed:%d' % instance.id,
    ])


@receiver(post_delete, sender=Submission)
def submission_delete(sender, instance, **kwargs):
    finished_submission(instance)
    instance.user.calculate_points()


@receiver(post_delete, sender=ContestSubmission)
def contest_submission_delete(sender, instance, **kwargs):
    participation = instance.participation
    participation.recalculate_score()


@receiver(post_save, sender=Organization)
def organization_update(sender, instance, **kwargs):
    cache.delete_many([make_template_fragment_key('organization_html', (instance.id, engine))
                       for engine in EFFECTIVE_MATH_ENGINES])

_misc_config_i18n = [code for code, _ in settings.LANGUAGES]
_misc_config_i18n.append('')


@receiver(post_save, sender=MiscConfig)
def misc_config_update(sender, instance, **kwargs):
    cache.delete_many(['misc_config:%s:%s' % (lang, instance.key) for lang in _misc_config_i18n])<commit_msg>Must split by language; #500<commit_after>import errno
import os

from django.conf import settings
from django.core.cache import cache
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 .caching import finished_submission
from .models import Problem, Contest, Submission, Organization, Profile, MiscConfig, Language, Judge, \
    BlogPost, ContestSubmission, Comment, License, EFFECTIVE_MATH_ENGINES


def get_pdf_path(basename):
    return os.path.join(settings.PROBLEM_PDF_CACHE, basename)


def unlink_if_exists(file):
    try:
        os.unlink(file)
    except OSError as e:
        if e.errno != errno.ENOENT:
            raise


@receiver(post_save, sender=Problem)
def problem_update(sender, instance, **kwargs):
    if hasattr(instance, '_updating_stats_only'):
        return

    cache.delete_many([
        make_template_fragment_key('submission_problem', (instance.id,)),
        make_template_fragment_key('problem_feed', (instance.id,)),
        'problem_tls:%s' % instance.id, 'problem_mls:%s' % instance.id,
    ])
    cache.delete_many([
        make_template_fragment_key('problem_html', (instance.id, engine, lang))
        for lang, _ in settings.LANGUAGES for engine in EFFECTIVE_MATH_ENGINES
    ])
    cache.delete_many([
        make_template_fragment_key('problem_authors', (instance.id, lang))
        for lang, _ in settings.LANGUAGES
    ])

    if hasattr(settings, 'PROBLEM_PDF_CACHE'):
        for lang, _ in settings.LANGUAGES:
            unlink_if_exists(get_pdf_path('%s.%s.pdf' % (instance.code, lang)))
            unlink_if_exists(get_pdf_path('%s.%s.log' % (instance.code, lang)))


@receiver(post_save, sender=Profile)
def profile_update(sender, instance, **kwargs):
    if hasattr(instance, '_updating_stats_only'):
        return

    cache.delete_many([make_template_fragment_key('user_about', (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):
    if hasattr(instance, '_updating_stats_only'):
        return

    cache.delete_many(['generated-meta:%d' % instance.id] +
                      [make_template_fragment_key('contest_html', (instance.id, engine))
                       for engine in EFFECTIVE_MATH_ENGINES])


@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_many([make_template_fragment_key('language_html', (instance.id,)),
                       'lang:cn_map'])


@receiver(post_save, sender=Judge)
def judge_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):
    cache.delete_many([
        make_template_fragment_key('post_summary', (instance.id,)),
        make_template_fragment_key('post_content', (instance.id,)),
        'blog_slug:%d' % instance.id,
        'blog_feed:%d' % instance.id,
    ])


@receiver(post_delete, sender=Submission)
def submission_delete(sender, instance, **kwargs):
    finished_submission(instance)
    instance.user.calculate_points()


@receiver(post_delete, sender=ContestSubmission)
def contest_submission_delete(sender, instance, **kwargs):
    participation = instance.participation
    participation.recalculate_score()


@receiver(post_save, sender=Organization)
def organization_update(sender, instance, **kwargs):
    cache.delete_many([make_template_fragment_key('organization_html', (instance.id, engine))
                       for engine in EFFECTIVE_MATH_ENGINES])

_misc_config_i18n = [code for code, _ in settings.LANGUAGES]
_misc_config_i18n.append('')


@receiver(post_save, sender=MiscConfig)
def misc_config_update(sender, instance, **kwargs):
    cache.delete_many(['misc_config:%s:%s' % (lang, instance.key.split('.')[0]) for lang in _misc_config_i18n])
{'fraction_non_alphanumeric': 0.11981357280891212, 'fraction_numerical': 0.0018188018642719109, 'mean_word_length': 4.502188868042526, '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': '1407035', 'n_tokens_mistral': 2833, 'n_tokens_neox': 2724, 'n_words': 505}