Skip to content

Commit

Permalink
Easy changes to support Django 4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Ninjaclasher committed Mar 14, 2024
1 parent 6124519 commit a9e52d7
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 28 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.7
- name: Set up Python 3.8
uses: actions/setup-python@v4
with:
python-version: '3.7'
python-version: '3.8'
- name: Install flake8
run: pip install flake8 flake8-import-order flake8-future-import flake8-commas flake8-logging-format flake8-quotes
- name: Lint with flake8
Expand All @@ -19,10 +19,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.7
- name: Set up Python 3.8
uses: actions/setup-python@v4
with:
python-version: '3.7'
python-version: '3.8'
- name: Cache pip
uses: actions/cache@v2
with:
Expand Down
1 change: 0 additions & 1 deletion judge/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
default_app_config = 'judge.apps.JudgeAppConfig'
6 changes: 3 additions & 3 deletions judge/admin/contest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from adminsortable2.admin import SortableInlineAdminMixin
from adminsortable2.admin import SortableAdminBase, SortableInlineAdminMixin
from django.contrib import admin
from django.core.exceptions import PermissionDenied
from django.db import connection, transaction
Expand Down Expand Up @@ -115,7 +115,7 @@ class Meta:
}


class ContestAdmin(NoBatchDeleteMixin, VersionAdmin):
class ContestAdmin(NoBatchDeleteMixin, SortableAdminBase, VersionAdmin):
fieldsets = (
(None, {'fields': ('key', 'name', 'authors', 'curators', 'testers', 'tester_see_submissions',
'tester_see_scoreboard', 'spectators')}),
Expand Down Expand Up @@ -301,7 +301,7 @@ def rate_view(self, request, id):
raise Http404()
with transaction.atomic():
contest.rate()
return HttpResponseRedirect(request.META.get('HTTP_REFERER', reverse('admin:judge_contest_changelist')))
return HttpResponseRedirect(request.headers.get('referer', reverse('admin:judge_contest_changelist')))

def get_form(self, request, obj=None, **kwargs):
form = super(ContestAdmin, self).get_form(request, obj, **kwargs)
Expand Down
5 changes: 3 additions & 2 deletions judge/admin/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ def get_formset(self, request, obj=None, **kwargs):

def formfield_for_dbfield(self, db_field, **kwargs):
submission = kwargs.pop('obj', None)
label = None
if submission:
if db_field.name == 'participation':
kwargs['queryset'] = ContestParticipation.objects.filter(user=submission.user,
Expand All @@ -94,6 +93,8 @@ def label(obj):
return pgettext('contest problem', '%(problem)s in %(contest)s') % {
'problem': obj.problem.name, 'contest': obj.contest.name,
}
else:
label = None
field = super(ContestSubmissionInline, self).formfield_for_dbfield(db_field, **kwargs)
if label is not None:
field.label_from_instance = label
Expand Down Expand Up @@ -258,4 +259,4 @@ def judge_view(self, request, id):
not submission.problem.is_editor(request.profile):
raise PermissionDenied()
submission.judge(rejudge=True, rejudge_user=request.user)
return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
return HttpResponseRedirect(request.headers.get('referer', '/'))
2 changes: 1 addition & 1 deletion judge/management/commands/generate_sitemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


class Command(BaseCommand):
requires_system_checks = False
requires_system_checks = []

def add_arguments(self, parser):
parser.add_argument('directory', help='directory to generate the sitemap in')
Expand Down
2 changes: 1 addition & 1 deletion judge/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):
full_token = request.META.get('HTTP_AUTHORIZATION', '')
full_token = request.headers.get('authorization', '')
if not full_token:
return self.get_response(request)

Expand Down

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion judge/template_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def site_theme(request):


def math_setting(request):
caniuse = CanIUse(request.META.get('HTTP_USER_AGENT', ''))
caniuse = CanIUse(request.headers.get('user-agent', ''))

# Middleware populating `profile` may not have loaded at this point if we're called from an error context.
if hasattr(request.user, 'profile'):
Expand Down
2 changes: 1 addition & 1 deletion judge/utils/raw_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self, joining_columns, related_model):
def get_joining_columns(self):
return self.joining_columns

def get_extra_restriction(self, where_class, alias, remote_alias):
def get_extra_restriction(self, alias, remote_alias):
pass


Expand Down
19 changes: 14 additions & 5 deletions judge/views/contests.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,10 @@ def _get_queryset(self):
return queryset

return queryset.annotate(
editor_or_tester=Exists(Contest.authors.through.objects.filter(contest=OuterRef('pk'), profile=profile))
.bitor(Exists(Contest.curators.through.objects.filter(contest=OuterRef('pk'), profile=profile)))
.bitor(Exists(Contest.testers.through.objects.filter(contest=OuterRef('pk'), profile=profile))),
editor_or_tester=
Exists(Contest.authors.through.objects.filter(contest=OuterRef('pk'), profile=profile))
| Exists(Contest.curators.through.objects.filter(contest=OuterRef('pk'), profile=profile))
| Exists(Contest.testers.through.objects.filter(contest=OuterRef('pk'), profile=profile)),
completed_contest=Exists(ContestParticipation.objects.filter(contest=OuterRef('pk'), user=profile,
virtual=ContestParticipation.LIVE)),
)
Expand Down Expand Up @@ -283,8 +284,16 @@ def get_context_data(self, **kwargs):
context['metadata'].update(
**self.object.contest_problems
.annotate(
partials_enabled=F('partial').bitand(F('problem__partial')),
pretests_enabled=F('is_pretested').bitand(F('contest__run_pretests_only')),
partials_enabled=Case(
When(partial=True, problem__partial=True, then=Value(True)),
default=Value(False),
output_field=BooleanField(),
),
pretests_enabled=Case(
When(is_pretested=True, contest__run_pretests_only=True, then=Value(True)),
default=Value(False),
output_field=BooleanField(),
),
)
.aggregate(
has_partials=Sum('partials_enabled'),
Expand Down
4 changes: 2 additions & 2 deletions judge/views/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.http import Http404, HttpResponseBadRequest, HttpResponseRedirect, JsonResponse
from django.shortcuts import render
from django.urls import reverse
from django.utils.http import is_safe_url
from django.utils.http import url_has_allowed_host_and_scheme

from judge.tasks import failure, progress, success
from judge.utils.celery import redirect_to_task_status
Expand All @@ -34,7 +34,7 @@ def task_status(request, task_id):
raise Http404()

redirect = request.GET.get('redirect')
if not is_safe_url(redirect, allowed_hosts={request.get_host()}):
if not url_has_allowed_host_and_scheme(redirect, allowed_hosts={request.get_host()}):
redirect = None

status = get_task_status(task_id)
Expand Down
8 changes: 4 additions & 4 deletions judge/views/two_factor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import webauthn
from django.conf import settings
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.views import SuccessURLAllowedHostsMixin
from django.contrib.auth.views import RedirectURLMixin
from django.http import Http404, HttpResponse, HttpResponseBadRequest, HttpResponseRedirect, JsonResponse
from django.urls import reverse
from django.utils.http import is_safe_url
from django.utils.http import url_has_allowed_host_and_scheme
from django.utils.translation import gettext as _, gettext_lazy
from django.views.generic import FormView, View
from django.views.generic.base import ContextMixin
Expand Down Expand Up @@ -226,7 +226,7 @@ def post(self, request, *args, **kwargs):
return HttpResponse()


class TwoFactorLoginView(SuccessURLAllowedHostsMixin, TOTPView, ContextMixin):
class TwoFactorLoginView(RedirectURLMixin, TOTPView, ContextMixin):
form_class = TwoFactorLoginForm
title = gettext_lazy('Perform Two-factor Authentication')
template_name = 'registration/two_factor_auth.html'
Expand All @@ -244,7 +244,7 @@ def check_skip(self):

def next_page(self):
redirect_to = self.request.GET.get('next', '')
url_is_safe = is_safe_url(
url_is_safe = url_has_allowed_host_and_scheme(
url=redirect_to,
allowed_hosts=self.get_success_url_allowed_hosts(),
require_https=self.request.is_secure(),
Expand Down
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Django>=3.2,<4
Django>=4.2,<5
django_compressor>=3
django-mptt>=0.13
django-pagedown<2
django-registration-redux>=2.10
django-reversion>=3.0.5,<4
django-reversion>=5
django-social-share
django-sortedm2m>=3.1.0
django-impersonate
Expand Down Expand Up @@ -36,7 +36,7 @@ martor @ git+https://github.com/DMOJ/martor.git
netaddr
webauthn<1
bleach[css]
django-admin-sortable2<2
django-admin-sortable2
icalendar
# This is a celery dependency whose latest major version is breaking everything.
importlib-metadata<5

0 comments on commit a9e52d7

Please sign in to comment.