From 9e336a7bc95e29864efeae4665178ac201242974 Mon Sep 17 00:00:00 2001 From: cuom1999 Date: Fri, 16 Sep 2022 00:07:27 -0500 Subject: [PATCH] Change add contest form --- judge/forms.py | 55 ++++++++++++--- judge/views/organization.py | 28 +++++--- templates/organization/contest/add.html | 52 -------------- templates/organization/contest/edit.html | 88 ++++++++++++++++++++++++ templates/widgets/datetimepicker.html | 1 + 5 files changed, 153 insertions(+), 71 deletions(-) create mode 100644 templates/organization/contest/edit.html diff --git a/judge/forms.py b/judge/forms.py index 94516c5..f155d49 100644 --- a/judge/forms.py +++ b/judge/forms.py @@ -184,10 +184,49 @@ class AddOrganizationForm(ModelForm): return res -class OrganizationContestForm(ModelForm): +class AddOrganizationContestForm(ModelForm): + def __init__(self, *args, **kwargs): + self.request = kwargs.pop("request", None) + super(AddOrganizationContestForm, self).__init__(*args, **kwargs) + + def save(self, commit=True): + contest = super(AddOrganizationContestForm, self).save(commit=False) + old_save_m2m = self.save_m2m + + def save_m2m(): + for i, problem in enumerate(self.cleaned_data["problems"]): + contest_problem = ContestProblem( + contest=contest, problem=problem, points=100, order=i + 1 + ) + contest_problem.save() + contest.contest_problems.add(contest_problem) + old_save_m2m() + + self.save_m2m = save_m2m + contest.save() + self.save_m2m() + return contest + + class Meta: + model = Contest + fields = ( + "key", + "name", + "start_time", + "end_time", + "problems", + ) + widgets = { + "start_time": DateTimePickerWidget(), + "end_time": DateTimePickerWidget(), + "problems": HeavySelect2MultipleWidget(data_view="problem_select2"), + } + + +class EditOrganizationContestForm(ModelForm): def __init__(self, *args, **kwargs): self.org_id = kwargs.pop("org_id", 0) - super(OrganizationContestForm, self).__init__(*args, **kwargs) + super(EditOrganizationContestForm, self).__init__(*args, **kwargs) for field in [ "authors", "curators", @@ -203,27 +242,25 @@ class OrganizationContestForm(ModelForm): class Meta: model = Contest fields = ( + "is_visible", "key", "name", + "start_time", + "end_time", + "format_name", "authors", "curators", "testers", - "is_visible", + "time_limit", "use_clarifications", "hide_problem_tags", "scoreboard_visibility", "run_pretests_only", "points_precision", - "start_time", - "end_time", - "time_limit", "description", "og_image", "logo_override_image", "summary", - "format_name", - "format_config", - "problem_label_script", "access_code", "private_contestants", "view_contest_scoreboard", diff --git a/judge/views/organization.py b/judge/views/organization.py index a531d74..c251bc5 100644 --- a/judge/views/organization.py +++ b/judge/views/organization.py @@ -44,8 +44,9 @@ from judge.forms import ( AddOrganizationMemberForm, OrganizationBlogForm, OrganizationAdminBlogForm, - OrganizationContestForm, + EditOrganizationContestForm, ContestProblemFormSet, + AddOrganizationContestForm, ) from judge.models import ( BlogPost, @@ -126,7 +127,6 @@ class OrganizationMixin(OrganizationBase): context["logo_override_image"] = self.organization.logo_override_image if "organizations" in context: context.pop("organizations") - print(context) return context def dispatch(self, request, *args, **kwargs): @@ -387,18 +387,12 @@ class OrganizationContestMixin( OrganizationHomeViewContext, ): model = Contest - form_class = OrganizationContestForm def is_contest_editable(self, request, contest): return request.profile in contest.authors.all() or self.can_edit_organization( self.organization ) - def get_form_kwargs(self): - kwargs = super(OrganizationContestMixin, self).get_form_kwargs() - kwargs["org_id"] = self.organization.id - return kwargs - class OrganizationContests( OrganizationContestMixin, MemberOrganizationMixin, ContestList @@ -432,7 +426,6 @@ class OrganizationContests( self.set_editable_contest(contest) for contest in context["future_contests"]: self.set_editable_contest(contest) - print(context) return context @@ -861,17 +854,26 @@ class AddOrganizationContest( AdminOrganizationMixin, OrganizationContestMixin, CreateView ): template_name = "organization/contest/add.html" + form_class = AddOrganizationContestForm def get_title(self): return _("Add contest") + def get_form_kwargs(self): + kwargs = super(AddOrganizationContest, self).get_form_kwargs() + kwargs["request"] = self.request + return kwargs + def form_valid(self, form): with transaction.atomic(), revisions.create_revision(): revisions.set_comment(_("Added from site")) revisions.set_user(self.request.user) + res = super(AddOrganizationContest, self).form_valid(form) + self.object.organizations.add(self.organization) self.object.is_organization_private = True + self.object.authors.add(self.request.profile) self.object.save() return res @@ -885,7 +887,8 @@ class AddOrganizationContest( class EditOrganizationContest( OrganizationContestMixin, MemberOrganizationMixin, UpdateView ): - template_name = "organization/contest/add.html" + template_name = "organization/contest/edit.html" + form_class = EditOrganizationContestForm def setup_contest(self, request, *args, **kwargs): contest_key = kwargs.get("contest", None) @@ -902,6 +905,11 @@ class EditOrganizationContest( status=400, ) + def get_form_kwargs(self): + kwargs = super(EditOrganizationContest, self).get_form_kwargs() + kwargs["org_id"] = self.organization.id + return kwargs + def get(self, request, *args, **kwargs): res = self.setup_contest(request, *args, **kwargs) if res: diff --git a/templates/organization/contest/add.html b/templates/organization/contest/add.html index ff24057..516d77b 100644 --- a/templates/organization/contest/add.html +++ b/templates/organization/contest/add.html @@ -6,28 +6,6 @@ {% block three_col_media %} {{ form.media.css }} - {% endblock %} {% block middle_content %} @@ -55,36 +33,6 @@ {% endif %} {% endfor %} - {% if problems_form %} -

- {{ problems_form.management_form }} - {{_('If you run out of rows, click Save')}} - - - - {% for field in problems_form[0] %} - {% if not field.is_hidden %} - - {% endif %} - {% endfor %} - - - - {% for form in problems_form %} - - {% for field in form %} - - {% endfor %} - - {% endfor %} - -
- {{field.label}} -
{{field}}
{{field.errors}}
- {% endif %} {% endblock %} \ No newline at end of file diff --git a/templates/organization/contest/edit.html b/templates/organization/contest/edit.html new file mode 100644 index 0000000..232b0c0 --- /dev/null +++ b/templates/organization/contest/edit.html @@ -0,0 +1,88 @@ +{% extends "organization/home-base.html" %} + +{% block three_col_js %} + {{ form.media.js }} +{% endblock %} + +{% block three_col_media %} + {{ form.media.css }} + +{% endblock %} + +{% block middle_content %} +
+ {% csrf_token %} + {% if form.errors %} +
+ x + {{ form.non_field_errors() }} + {{ form.errors }} +
+ {% endif %} + {% for field in form %} + {% if not field.is_hidden %} +
+ {{ field.errors }} + +
+ {{ field }} +
+ {% if field.help_text %} + {{ field.help_text|safe }} + {% endif %} +
+ {% endif %} + {% endfor %} + +

+ {{ problems_form.management_form }} + {{_('If you run out of rows, click Save')}} + + + + {% for field in problems_form[0] %} + {% if not field.is_hidden %} + + {% endif %} + {% endfor %} + + + + {% for form in problems_form %} + + {% for field in form %} + + {% endfor %} + + {% endfor %} + +
+ {{field.label}} +
{{field}}
{{field.errors}}
+ +
+{% endblock %} \ No newline at end of file diff --git a/templates/widgets/datetimepicker.html b/templates/widgets/datetimepicker.html index 9363d07..e09b77a 100644 --- a/templates/widgets/datetimepicker.html +++ b/templates/widgets/datetimepicker.html @@ -1,4 +1,5 @@