Skip to content

Commit

Permalink
admin: Add create_private_problem perm
Browse files Browse the repository at this point in the history
  • Loading branch information
kiritofeng committed Dec 12, 2024
1 parent f9086f0 commit 71ec730
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
21 changes: 18 additions & 3 deletions judge/admin/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from django import forms
from django.contrib import admin
from django.core.exceptions import PermissionDenied
from django.db import transaction
from django.forms import ModelForm
from django.urls import reverse, reverse_lazy
Expand Down Expand Up @@ -150,7 +151,8 @@ class ProblemAdmin(NoBatchDeleteMixin, VersionAdmin):
def get_actions(self, request):
actions = super(ProblemAdmin, self).get_actions(request)

if request.user.has_perm('judge.change_public_visibility'):
if request.user.has_perm('judge.change_public_visibility') or \
request.user.has_perm('judge.create_private_problem'):
func, name, desc = self.get_action('make_public')
actions[name] = (func, name, desc)

Expand All @@ -164,8 +166,10 @@ def get_actions(self, request):

def get_readonly_fields(self, request, obj=None):
fields = self.readonly_fields
if not request.user.has_perm('judge.change_public_visibility'):
fields += ('is_public',)
if not request.user.has_perm('judge.create_private_problem'):
fields += ('organizations',)
if not request.user.has_perm('judge.change_public_visibility'):
fields += ('is_public',)
if not request.user.has_perm('judge.change_manually_managed'):
fields += ('is_manually_managed',)
if not request.user.has_perm('judge.problem_full_markup'):
Expand Down Expand Up @@ -195,6 +199,8 @@ def update_publish_date(self, request, queryset):

@admin.display(description=_('Mark problems as public'))
def make_public(self, request, queryset):
if not request.user.has_perm('judge.change_public_visibility'):
queryset = queryset.filter(is_organization_private=True)
count = queryset.update(is_public=True)
for problem_id in queryset.values_list('id', flat=True):
self._rescore(request, problem_id)
Expand All @@ -204,6 +210,8 @@ def make_public(self, request, queryset):

@admin.display(description=_('Mark problems as private'))
def make_private(self, request, queryset):
if not request.user.has_perm('judge.change_public_visibility'):
queryset = queryset.filter(is_organization_private=True)
count = queryset.update(is_public=False)
for problem_id in queryset.values_list('id', flat=True):
self._rescore(request, problem_id)
Expand Down Expand Up @@ -233,6 +241,13 @@ def save_model(self, request, obj, form, change):
# `organizations` will not appear in `cleaned_data` if user cannot edit it
if form.changed_data and 'organizations' in form.changed_data:
obj.is_organization_private = bool(form.cleaned_data['organizations'])

if form.cleaned_data.get('is_public') and not request.user.has_perm('judge.change_public_visibility'):
if not obj.is_organization_private:
raise PermissionDenied
if not request.user.has_perm('judge.create_private_problem'):
raise PermissionDenied

super(ProblemAdmin, self).save_model(request, obj, form, change)
if (
form.changed_data and
Expand Down
1 change: 1 addition & 0 deletions judge/models/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ class Meta:
('change_public_visibility', _('Change is_public field')),
('change_manually_managed', _('Change is_manually_managed field')),
('see_organization_problem', _('See organization-private problems')),
('create_private_problem', _('Create private problems')),
)
verbose_name = _('problem')
verbose_name_plural = _('problems')
Expand Down

0 comments on commit 71ec730

Please sign in to comment.