NDOJ/judge/signals.py

190 lines
5.1 KiB
Python
Raw Permalink Normal View History

2020-01-21 06:35:58 +00:00
import errno
import os
from django.conf import settings
from django.contrib.sites.models import Site
from django.core.cache import cache
from django.core.cache.utils import make_template_fragment_key
from django.db.models.signals import post_delete, post_save
from django.dispatch import receiver
from .caching import finished_submission
2022-05-14 17:57:27 +00:00
from .models import (
BlogPost,
Comment,
Contest,
ContestSubmission,
EFFECTIVE_MATH_ENGINES,
Judge,
Language,
License,
MiscConfig,
Organization,
Problem,
Profile,
Submission,
)
2020-01-21 06:35:58 +00:00
def get_pdf_path(basename):
return os.path.join(settings.DMOJ_PDF_PROBLEM_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):
2022-05-14 17:57:27 +00:00
if hasattr(instance, "_updating_stats_only"):
2020-01-21 06:35:58 +00:00
return
2022-05-14 17:57:27 +00:00
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
]
)
cache.delete_many(
[
"generated-meta-problem:%s:%d" % (lang, instance.id)
for lang, _ in settings.LANGUAGES
]
)
2020-01-21 06:35:58 +00:00
for lang, _ in settings.LANGUAGES:
2022-05-14 17:57:27 +00:00
unlink_if_exists(get_pdf_path("%s.%s.pdf" % (instance.code, lang)))
2020-01-21 06:35:58 +00:00
@receiver(post_save, sender=Profile)
def profile_update(sender, instance, **kwargs):
2022-05-14 17:57:27 +00:00
if hasattr(instance, "_updating_stats_only"):
2020-01-21 06:35:58 +00:00
return
2022-05-14 17:57:27 +00:00
cache.delete_many(
[
make_template_fragment_key("user_about", (instance.id, engine))
for engine in EFFECTIVE_MATH_ENGINES
]
+ [
make_template_fragment_key("org_member_count", (org_id,))
for org_id in instance.organizations.values_list("id", flat=True)
]
)
2020-01-21 06:35:58 +00:00
@receiver(post_save, sender=Contest)
def contest_update(sender, instance, **kwargs):
2022-05-14 17:57:27 +00:00
if hasattr(instance, "_updating_stats_only"):
2020-01-21 06:35:58 +00:00
return
2022-05-14 17:57:27 +00:00
cache.delete_many(
["generated-meta-contest:%d" % instance.id]
+ [
make_template_fragment_key("contest_html", (instance.id, engine))
for engine in EFFECTIVE_MATH_ENGINES
]
)
2020-01-21 06:35:58 +00:00
@receiver(post_save, sender=License)
def license_update(sender, instance, **kwargs):
2022-05-14 17:57:27 +00:00
cache.delete(make_template_fragment_key("license_html", (instance.id,)))
2020-01-21 06:35:58 +00:00
@receiver(post_save, sender=Language)
def language_update(sender, instance, **kwargs):
2022-05-14 17:57:27 +00:00
cache.delete_many(
[make_template_fragment_key("language_html", (instance.id,)), "lang:cn_map"]
)
2020-01-21 06:35:58 +00:00
@receiver(post_save, sender=Judge)
def judge_update(sender, instance, **kwargs):
2022-05-14 17:57:27 +00:00
cache.delete(make_template_fragment_key("judge_html", (instance.id,)))
2020-01-21 06:35:58 +00:00
@receiver(post_save, sender=Comment)
def comment_update(sender, instance, **kwargs):
2022-05-14 17:57:27 +00:00
cache.delete("comment_feed:%d" % instance.id)
2020-01-21 06:35:58 +00:00
@receiver(post_save, sender=BlogPost)
def post_update(sender, instance, **kwargs):
2022-05-14 17:57:27 +00:00
cache.delete_many(
[
make_template_fragment_key("post_summary", (instance.id,)),
"blog_slug:%d" % instance.id,
"blog_feed:%d" % instance.id,
]
)
cache.delete_many(
[
make_template_fragment_key("post_content", (instance.id, engine))
for engine in EFFECTIVE_MATH_ENGINES
]
)
2020-01-21 06:35:58 +00:00
@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.recompute_results()
@receiver(post_save, sender=Organization)
def organization_update(sender, instance, **kwargs):
2022-05-14 17:57:27 +00:00
cache.delete_many(
[
make_template_fragment_key("organization_html", (instance.id, engine))
for engine in EFFECTIVE_MATH_ENGINES
]
)
2020-01-21 06:35:58 +00:00
_misc_config_i18n = [code for code, _ in settings.LANGUAGES]
2022-05-14 17:57:27 +00:00
_misc_config_i18n.append("")
2020-01-21 06:35:58 +00:00
@receiver(post_save, sender=MiscConfig)
def misc_config_update(sender, instance, **kwargs):
2022-05-14 17:57:27 +00:00
cache.delete_many(
[
"misc_config:%s:%s:%s" % (domain, lang, instance.key.split(".")[0])
for lang in _misc_config_i18n
for domain in Site.objects.values_list("domain", flat=True)
]
)
2020-01-21 06:35:58 +00:00
@receiver(post_save, sender=ContestSubmission)
def contest_submission_update(sender, instance, **kwargs):
2022-05-14 17:57:27 +00:00
Submission.objects.filter(id=instance.submission_id).update(
contest_object_id=instance.participation.contest_id
)