<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, NavigationBar, MiscConfig, Language, Judge, \
    BlogPost, ContestSubmission, Comment
from .caching import update_submission, finished_submission


@receiver(post_save, sender=Problem)
def problem_update(sender, instance, **kwargs):
    cache.delete_many([
        make_template_fragment_key('problem_html', (instance.id,)),
        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('user_on_rank', (instance.id,)),
        make_template_fragment_key('user_org_on_rank', (instance.id,)),
        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=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):
    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_save, sender=Submission)
def submission_update(sender, instance, **kwargs):
    update_submission(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()
    cache.delete(make_template_fragment_key('conrank_user_prob',
                                            (participation.profile.user_id,
                                             participation.contest_id)))


@receiver(post_save, sender=Organization)
def organization_update(sender, instance, **kwargs):
    cache.delete(make_template_fragment_key('organization_html', (instance.id,)))
    cache.delete_many([make_template_fragment_key('user_org_on_rank', (user,))
                       for user in instance.members.values_list('id', flat=True)])


@receiver(post_save, sender=NavigationBar)
def navigation_update(sender, instance, **kwargs):
    cache.delete_many(['navbar', 'navbar_dict', make_template_fragment_key('navbar')])


@receiver(post_save, sender=MiscConfig)
def misc_config_update(sender, instance, **kwargs):
    cache.delete('misc_config:%s' % instance.key)<commit_msg>No more user_on_rank and user_org_on_rank.<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, NavigationBar, MiscConfig, Language, Judge, \
    BlogPost, ContestSubmission, Comment
from .caching import update_submission, finished_submission


@receiver(post_save, sender=Problem)
def problem_update(sender, instance, **kwargs):
    cache.delete_many([
        make_template_fragment_key('problem_html', (instance.id,)),
        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=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):
    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_save, sender=Submission)
def submission_update(sender, instance, **kwargs):
    update_submission(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()
    cache.delete(make_template_fragment_key('conrank_user_prob',
                                            (participation.profile.user_id,
                                             participation.contest_id)))


@receiver(post_save, sender=Organization)
def organization_update(sender, instance, **kwargs):
    cache.delete(make_template_fragment_key('organization_html', (instance.id,)))


@receiver(post_save, sender=NavigationBar)
def navigation_update(sender, instance, **kwargs):
    cache.delete_many(['navbar', 'navbar_dict', make_template_fragment_key('navbar')])


@receiver(post_save, sender=MiscConfig)
def misc_config_update(sender, instance, **kwargs):
    cache.delete('misc_config:%s' % instance.key)
{'fraction_non_alphanumeric': 0.1199949154696835, 'fraction_numerical': 0.0, 'mean_word_length': 4.632068718682892, '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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2174834', 'n_tokens_mistral': 2408, 'n_tokens_neox': 2329, 'n_words': 408}