diff --git a/judge/forms.py b/judge/forms.py index 504338b..362c067 100644 --- a/judge/forms.py +++ b/judge/forms.py @@ -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: diff --git a/judge/views/contests.py b/judge/views/contests.py index b9f4b14..ff4b320 100644 --- a/judge/views/contests.py +++ b/judge/views/contests.py @@ -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, + ), + ) ) diff --git a/templates/contest/clone.html b/templates/contest/clone.html index f1b4a0f..2d3b4aa 100644 --- a/templates/contest/clone.html +++ b/templates/contest/clone.html @@ -25,17 +25,32 @@ {% endblock %} +{% block js_media %} + +{% endblock %} + {% block body %}