NDOJ/judge/signals.py

187 lines
5.2 KiB
Python
Raw 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
2024-04-23 20:36:51 +00:00
import judge
2024-09-08 05:31:24 +00:00
from judge import template_context
2023-04-05 17:49:23 +00:00
from judge.utils.problems import finished_submission
2022-05-14 17:57:27 +00:00
from .models import (
BlogPost,
Comment,
Contest,
ContestSubmission,
Judge,
Language,
License,
MiscConfig,
Organization,
Problem,
Profile,
Submission,
2024-04-23 20:36:51 +00:00
NavigationBar,
2024-05-29 05:14:42 +00:00
Solution,
2024-09-08 05:31:24 +00:00
ContestProblem,
2022-05-14 17:57:27 +00:00
)
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,)),
"problem_tls:%s" % instance.id,
"problem_mls:%s" % instance.id,
]
)
cache.delete_many(
[
2024-02-26 20:49:52 +00:00
make_template_fragment_key("problem_html", (instance.id, lang))
2022-05-14 17:57:27 +00:00
for lang, _ in settings.LANGUAGES
]
)
cache.delete_many(
[
"generated-meta-problem:%s:%d" % (lang, instance.id)
for lang, _ in settings.LANGUAGES
]
)
2024-04-13 22:02:54 +00:00
Problem.get_authors.dirty(instance)
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):
judge.utils.users.get_points_rank.dirty(instance.id)
judge.utils.users.get_rating_rank.dirty(instance.id)
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(
2024-02-26 21:47:17 +00:00
[make_template_fragment_key("user_about", (instance.id,))]
2022-05-14 17:57:27 +00:00
+ [
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
2024-04-27 01:37:35 +00:00
judge.models.profile._get_basic_info.dirty(instance.id)
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]
2024-02-26 21:47:17 +00:00
+ [make_template_fragment_key("contest_html", (instance.id,))]
2022-05-14 17:57:27 +00:00
)
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):
2024-05-29 05:14:42 +00:00
cache.delete(make_template_fragment_key("post_content", (instance.id,)))
2024-04-13 22:02:54 +00:00
BlogPost.get_authors.dirty(instance)
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):
2024-02-26 21:47:17 +00:00
cache.delete_many([make_template_fragment_key("organization_html", (instance.id,))])
Organization.get_admin_ids.dirty(instance)
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
)
2024-04-23 20:36:51 +00:00
@receiver(post_save, sender=NavigationBar)
def navbar_update(sender, instance, **kwargs):
2024-09-08 05:31:24 +00:00
template_context._nav_bar.dirty()
2024-05-29 05:14:42 +00:00
@receiver(post_save, sender=Solution)
def solution_update(sender, instance, **kwargs):
cache.delete(make_template_fragment_key("solution_content", (instance.id,)))
2024-09-08 05:31:24 +00:00
@receiver(post_delete, sender=ContestProblem)
def contest_problem_delete(sender, instance, **kwargs):
Submission.objects.filter(
contest_object=instance.contest, contest__isnull=True
).update(contest_object=None)