Only allow group contest when cloning

This commit is contained in:
cuom1999 2023-08-14 09:10:28 -05:00
parent 8f42885482
commit 68e705404e
3 changed files with 56 additions and 5 deletions

View file

@ -474,6 +474,15 @@ class ContestCloneForm(Form):
max_length=20,
validators=[RegexValidator("^[a-z0-9]+$", _("Contest id must be ^[a-z0-9]+$"))],
)
organization = ChoiceField(choices=(), required=True)
def __init__(self, *args, org_choices=(), profile=None, **kwargs):
super(ContestCloneForm, self).__init__(*args, **kwargs)
self.fields["organization"].widget = Select2Widget(
attrs={"style": "width: 100%", "data-placeholder": _("Group")},
)
self.fields["organization"].choices = org_choices
self.profile = profile
def clean_key(self):
key = self.cleaned_data["key"]
@ -481,6 +490,16 @@ class ContestCloneForm(Form):
raise ValidationError(_("Contest with key already exists."))
return key
def clean_organization(self):
organization_id = self.cleaned_data["organization"]
try:
organization = Organization.objects.get(id=organization_id)
except Exception:
raise ValidationError(_("Group doesn't exist."))
if not organization.admins.filter(id=self.profile.id).exists():
raise ValidationError(_("You don't have permission in this group."))
return organization
class ProblemPointsVoteForm(ModelForm):
class Meta:

View file

@ -453,9 +453,19 @@ class ContestClone(
form_class = ContestCloneForm
permission_required = "judge.clone_contest"
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs["org_choices"] = tuple(
Organization.objects.filter(admins=self.request.profile).values_list(
"id", "name"
)
)
kwargs["profile"] = self.request.profile
return kwargs
def form_valid(self, form):
tags = self.object.tags.all()
organizations = self.object.organizations.all()
organization = form.cleaned_data["organization"]
private_contestants = self.object.private_contestants.all()
view_contest_scoreboard = self.object.view_contest_scoreboard.all()
contest_problems = self.object.contest_problems.all()
@ -469,7 +479,7 @@ class ContestClone(
contest.save()
contest.tags.set(tags)
contest.organizations.set(organizations)
contest.organizations.set([organization])
contest.private_contestants.set(private_contestants)
contest.view_contest_scoreboard.set(view_contest_scoreboard)
contest.authors.add(self.request.profile)
@ -480,7 +490,14 @@ class ContestClone(
ContestProblem.objects.bulk_create(contest_problems)
return HttpResponseRedirect(
reverse("admin:judge_contest_change", args=(contest.id,))
reverse(
"organization_contest_edit",
args=(
organization.id,
organization.slug,
contest.key,
),
)
)

View file

@ -25,17 +25,32 @@
</style>
{% endblock %}
{% block js_media %}
<script type="text/javascript">
$(function() {
$("#id_organization").select2();
});
</script>
{% endblock %}
{% block body %}
<form id="contest-clone-panel" action="" method="post" class="form-area">
{% csrf_token %}
{% if form.errors %}
{% if form.key.errors %}
<div id="form-errors">
{{ form.key.errors }}
<div>{{ form.key.errors }}</div>
</div>
{% endif %}
<div><label class="inline-header grayed">{{ _('Enter a new key for the cloned contest:') }}</label></div>
<div id="contest-key-container"><span class="fullwidth">{{ form.key }}</span></div>
<div><label class="inline-header grayed">{{ _('Group:') }}</label></div>
{{form.organization}}
{% if form.organization.errors %}
<div id="form-errors">
<div>{{ form.organization.errors }}</div>
</div>
{% endif %}
<hr>
<button style="float: right;" type="submit">{{ _('Clone!') }}</button>
</form>