online-judge/judge/signals.py
Tudor Brindus 3e597c03b1 Simplify PDF rendering backend
- Drop Puppeteer and Selenium backends
- "Inline" pdfoid, and move `pdf_problems.py` to `utils` + rename it
  `pdfoid.py`
- Drop a lot of pointless error checking; if we fail we'll want to raise
  a 500 regardless

Co-authored-by: Quantum <quantum2048@gmail.com>
2023-03-09 13:33:44 +02:00

149 lines
5.3 KiB
Python

import errno
import os
from typing import Optional
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_delete, post_save
from django.dispatch import receiver
from .caching import finished_submission
from .models import BlogPost, Comment, Contest, ContestSubmission, EFFECTIVE_MATH_ENGINES, Judge, Language, License, \
MiscConfig, Organization, Problem, Profile, Submission, WebAuthnCredential
def get_pdf_path(basename: str) -> Optional[str]:
if not settings.DMOJ_PDF_PROBLEM_CACHE:
return None
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):
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])
cache.delete_many(['generated-meta-problem:%s:%d' % (lang, instance.id) for lang, _ in settings.LANGUAGES])
for lang, _ in settings.LANGUAGES:
cached_pdf_filename = get_pdf_path('%s.%s.pdf' % (instance.code, lang))
if cached_pdf_filename is not None:
unlink_if_exists(cached_pdf_filename)
@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, 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)])
@receiver(post_delete, sender=WebAuthnCredential)
def webauthn_delete(sender, instance, **kwargs):
profile = instance.user
if profile.webauthn_credentials.count() == 0:
profile.is_webauthn_enabled = False
profile.save(update_fields=['is_webauthn_enabled'])
@receiver(post_save, sender=Contest)
def contest_update(sender, instance, **kwargs):
if hasattr(instance, '_updating_stats_only'):
return
cache.delete_many(['generated-meta-contest:%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,)),
'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])
@receiver(post_delete, sender=Submission)
def submission_delete(sender, instance, **kwargs):
finished_submission(instance)
instance.user._updating_stats_only = True
instance.user.calculate_points()
instance.problem._updating_stats_only = True
instance.problem.update_stats()
@receiver(post_delete, sender=ContestSubmission)
def contest_submission_delete(sender, instance, **kwargs):
participation = instance.participation
participation.recompute_results()
Submission.objects.filter(id=instance.submission_id).update(contest_object=None)
@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])
@receiver(post_save, sender=MiscConfig)
def misc_config_update(sender, instance, **kwargs):
cache.delete('misc_config')
@receiver(post_delete, sender=MiscConfig)
def misc_config_delete(sender, instance, **kwargs):
cache.delete('misc_config')
@receiver(post_save, sender=ContestSubmission)
def contest_submission_update(sender, instance, **kwargs):
Submission.objects.filter(id=instance.submission_id).update(contest_object_id=instance.participation.contest_id)