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 17, 2024
1 parent 743bf09 commit 61e6001
Show file tree
Hide file tree
Showing 3 changed files with 36 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 3.2.25 on 2024-12-17 03:55

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('judge', '0147_judge_add_tiers'),
]

operations = [
migrations.AlterModelOptions(
name='problem',
options={'permissions': (('see_private_problem', 'See hidden problems'), ('edit_own_problem', 'Edit own problems'), ('edit_all_problem', 'Edit all problems'), ('edit_public_problem', 'Edit all public problems'), ('problem_full_markup', 'Edit problems with full markup'), ('clone_problem', 'Clone problem'), ('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'},
),
]
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 61e6001

Please sign in to comment.