Change add contest form
This commit is contained in:
parent
aeab9e8d0e
commit
9e336a7bc9
5 changed files with 153 additions and 71 deletions
|
@ -184,10 +184,49 @@ class AddOrganizationForm(ModelForm):
|
||||||
return res
|
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):
|
def __init__(self, *args, **kwargs):
|
||||||
self.org_id = kwargs.pop("org_id", 0)
|
self.org_id = kwargs.pop("org_id", 0)
|
||||||
super(OrganizationContestForm, self).__init__(*args, **kwargs)
|
super(EditOrganizationContestForm, self).__init__(*args, **kwargs)
|
||||||
for field in [
|
for field in [
|
||||||
"authors",
|
"authors",
|
||||||
"curators",
|
"curators",
|
||||||
|
@ -203,27 +242,25 @@ class OrganizationContestForm(ModelForm):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Contest
|
model = Contest
|
||||||
fields = (
|
fields = (
|
||||||
|
"is_visible",
|
||||||
"key",
|
"key",
|
||||||
"name",
|
"name",
|
||||||
|
"start_time",
|
||||||
|
"end_time",
|
||||||
|
"format_name",
|
||||||
"authors",
|
"authors",
|
||||||
"curators",
|
"curators",
|
||||||
"testers",
|
"testers",
|
||||||
"is_visible",
|
"time_limit",
|
||||||
"use_clarifications",
|
"use_clarifications",
|
||||||
"hide_problem_tags",
|
"hide_problem_tags",
|
||||||
"scoreboard_visibility",
|
"scoreboard_visibility",
|
||||||
"run_pretests_only",
|
"run_pretests_only",
|
||||||
"points_precision",
|
"points_precision",
|
||||||
"start_time",
|
|
||||||
"end_time",
|
|
||||||
"time_limit",
|
|
||||||
"description",
|
"description",
|
||||||
"og_image",
|
"og_image",
|
||||||
"logo_override_image",
|
"logo_override_image",
|
||||||
"summary",
|
"summary",
|
||||||
"format_name",
|
|
||||||
"format_config",
|
|
||||||
"problem_label_script",
|
|
||||||
"access_code",
|
"access_code",
|
||||||
"private_contestants",
|
"private_contestants",
|
||||||
"view_contest_scoreboard",
|
"view_contest_scoreboard",
|
||||||
|
|
|
@ -44,8 +44,9 @@ from judge.forms import (
|
||||||
AddOrganizationMemberForm,
|
AddOrganizationMemberForm,
|
||||||
OrganizationBlogForm,
|
OrganizationBlogForm,
|
||||||
OrganizationAdminBlogForm,
|
OrganizationAdminBlogForm,
|
||||||
OrganizationContestForm,
|
EditOrganizationContestForm,
|
||||||
ContestProblemFormSet,
|
ContestProblemFormSet,
|
||||||
|
AddOrganizationContestForm,
|
||||||
)
|
)
|
||||||
from judge.models import (
|
from judge.models import (
|
||||||
BlogPost,
|
BlogPost,
|
||||||
|
@ -126,7 +127,6 @@ class OrganizationMixin(OrganizationBase):
|
||||||
context["logo_override_image"] = self.organization.logo_override_image
|
context["logo_override_image"] = self.organization.logo_override_image
|
||||||
if "organizations" in context:
|
if "organizations" in context:
|
||||||
context.pop("organizations")
|
context.pop("organizations")
|
||||||
print(context)
|
|
||||||
return context
|
return context
|
||||||
|
|
||||||
def dispatch(self, request, *args, **kwargs):
|
def dispatch(self, request, *args, **kwargs):
|
||||||
|
@ -387,18 +387,12 @@ class OrganizationContestMixin(
|
||||||
OrganizationHomeViewContext,
|
OrganizationHomeViewContext,
|
||||||
):
|
):
|
||||||
model = Contest
|
model = Contest
|
||||||
form_class = OrganizationContestForm
|
|
||||||
|
|
||||||
def is_contest_editable(self, request, contest):
|
def is_contest_editable(self, request, contest):
|
||||||
return request.profile in contest.authors.all() or self.can_edit_organization(
|
return request.profile in contest.authors.all() or self.can_edit_organization(
|
||||||
self.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(
|
class OrganizationContests(
|
||||||
OrganizationContestMixin, MemberOrganizationMixin, ContestList
|
OrganizationContestMixin, MemberOrganizationMixin, ContestList
|
||||||
|
@ -432,7 +426,6 @@ class OrganizationContests(
|
||||||
self.set_editable_contest(contest)
|
self.set_editable_contest(contest)
|
||||||
for contest in context["future_contests"]:
|
for contest in context["future_contests"]:
|
||||||
self.set_editable_contest(contest)
|
self.set_editable_contest(contest)
|
||||||
print(context)
|
|
||||||
return context
|
return context
|
||||||
|
|
||||||
|
|
||||||
|
@ -861,17 +854,26 @@ class AddOrganizationContest(
|
||||||
AdminOrganizationMixin, OrganizationContestMixin, CreateView
|
AdminOrganizationMixin, OrganizationContestMixin, CreateView
|
||||||
):
|
):
|
||||||
template_name = "organization/contest/add.html"
|
template_name = "organization/contest/add.html"
|
||||||
|
form_class = AddOrganizationContestForm
|
||||||
|
|
||||||
def get_title(self):
|
def get_title(self):
|
||||||
return _("Add contest")
|
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):
|
def form_valid(self, form):
|
||||||
with transaction.atomic(), revisions.create_revision():
|
with transaction.atomic(), revisions.create_revision():
|
||||||
revisions.set_comment(_("Added from site"))
|
revisions.set_comment(_("Added from site"))
|
||||||
revisions.set_user(self.request.user)
|
revisions.set_user(self.request.user)
|
||||||
|
|
||||||
res = super(AddOrganizationContest, self).form_valid(form)
|
res = super(AddOrganizationContest, self).form_valid(form)
|
||||||
|
|
||||||
self.object.organizations.add(self.organization)
|
self.object.organizations.add(self.organization)
|
||||||
self.object.is_organization_private = True
|
self.object.is_organization_private = True
|
||||||
|
self.object.authors.add(self.request.profile)
|
||||||
self.object.save()
|
self.object.save()
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
@ -885,7 +887,8 @@ class AddOrganizationContest(
|
||||||
class EditOrganizationContest(
|
class EditOrganizationContest(
|
||||||
OrganizationContestMixin, MemberOrganizationMixin, UpdateView
|
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):
|
def setup_contest(self, request, *args, **kwargs):
|
||||||
contest_key = kwargs.get("contest", None)
|
contest_key = kwargs.get("contest", None)
|
||||||
|
@ -902,6 +905,11 @@ class EditOrganizationContest(
|
||||||
status=400,
|
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):
|
def get(self, request, *args, **kwargs):
|
||||||
res = self.setup_contest(request, *args, **kwargs)
|
res = self.setup_contest(request, *args, **kwargs)
|
||||||
if res:
|
if res:
|
||||||
|
|
|
@ -6,28 +6,6 @@
|
||||||
|
|
||||||
{% block three_col_media %}
|
{% block three_col_media %}
|
||||||
{{ form.media.css }}
|
{{ form.media.css }}
|
||||||
<style>
|
|
||||||
#org-field-wrapper-scoreboard_visibility,
|
|
||||||
#org-field-wrapper-points_precision,
|
|
||||||
#org-field-wrapper-start_time,
|
|
||||||
#org-field-wrapper-end_time,
|
|
||||||
#org-field-wrapper-time_limit,
|
|
||||||
#org-field-wrapper-format_name {
|
|
||||||
display: inline-flex;
|
|
||||||
}
|
|
||||||
.problems-problem {
|
|
||||||
width: 40%;
|
|
||||||
}
|
|
||||||
input[type=number] {
|
|
||||||
width: 5em;
|
|
||||||
}
|
|
||||||
.middle-content {
|
|
||||||
z-index: 1;
|
|
||||||
}
|
|
||||||
#three-col-container {
|
|
||||||
overflow: auto;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block middle_content %}
|
{% block middle_content %}
|
||||||
|
@ -55,36 +33,6 @@
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
{% if problems_form %}
|
|
||||||
<hr><br>
|
|
||||||
{{ problems_form.management_form }}
|
|
||||||
<i>{{_('If you run out of rows, click Save')}}</i>
|
|
||||||
<table class="table">
|
|
||||||
<thead>
|
|
||||||
<tr>
|
|
||||||
{% for field in problems_form[0] %}
|
|
||||||
{% if not field.is_hidden %}
|
|
||||||
<th>
|
|
||||||
{{field.label}}
|
|
||||||
</th>
|
|
||||||
{% endif %}
|
|
||||||
{% endfor %}
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
<tbody>
|
|
||||||
{% for form in problems_form %}
|
|
||||||
<tr>
|
|
||||||
{% for field in form %}
|
|
||||||
<td class="problems-{{field.name}}" title="
|
|
||||||
{{ field.help_text|safe if field.help_text }}"
|
|
||||||
style="{{ 'display:none' if field.is_hidden }}"
|
|
||||||
>{{field}}<div style="color:red">{{field.errors}}</div></td>
|
|
||||||
{% endfor %}
|
|
||||||
</tr>
|
|
||||||
{% endfor %}
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
{% endif %}
|
|
||||||
<button type="submit">{{ _('Save') }}</button>
|
<button type="submit">{{ _('Save') }}</button>
|
||||||
</form>
|
</form>
|
||||||
{% endblock %}
|
{% endblock %}
|
88
templates/organization/contest/edit.html
Normal file
88
templates/organization/contest/edit.html
Normal file
|
@ -0,0 +1,88 @@
|
||||||
|
{% extends "organization/home-base.html" %}
|
||||||
|
|
||||||
|
{% block three_col_js %}
|
||||||
|
{{ form.media.js }}
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block three_col_media %}
|
||||||
|
{{ form.media.css }}
|
||||||
|
<style>
|
||||||
|
#org-field-wrapper-scoreboard_visibility,
|
||||||
|
#org-field-wrapper-points_precision,
|
||||||
|
#org-field-wrapper-start_time,
|
||||||
|
#org-field-wrapper-end_time,
|
||||||
|
#org-field-wrapper-time_limit,
|
||||||
|
#org-field-wrapper-format_name {
|
||||||
|
display: inline-flex;
|
||||||
|
}
|
||||||
|
.problems-problem {
|
||||||
|
width: 40%;
|
||||||
|
}
|
||||||
|
input[type=number] {
|
||||||
|
width: 5em;
|
||||||
|
}
|
||||||
|
.middle-content {
|
||||||
|
z-index: 1;
|
||||||
|
}
|
||||||
|
#three-col-container {
|
||||||
|
overflow: auto;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block middle_content %}
|
||||||
|
<form action="" method="post">
|
||||||
|
{% csrf_token %}
|
||||||
|
{% if form.errors %}
|
||||||
|
<div class="alert alert-danger alert-dismissable">
|
||||||
|
<a href="#" class="close">x</a>
|
||||||
|
{{ form.non_field_errors() }}
|
||||||
|
{{ form.errors }}
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
{% for field in form %}
|
||||||
|
{% if not field.is_hidden %}
|
||||||
|
<div style="margin-bottom: 1em;">
|
||||||
|
{{ field.errors }}
|
||||||
|
<label for="{{field.id_for_label }}"><b>{{ field.label }}{% if field.field.required %}<span style="color:red"> * </span>{% endif %}:</b> </label>
|
||||||
|
<div class="org-field-wrapper" id="org-field-wrapper-{{field.html_name }}">
|
||||||
|
{{ field }}
|
||||||
|
</div>
|
||||||
|
{% if field.help_text %}
|
||||||
|
<i style="display: block">{{ field.help_text|safe }}</i>
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
{% endfor %}
|
||||||
|
|
||||||
|
<hr><br>
|
||||||
|
{{ problems_form.management_form }}
|
||||||
|
<i>{{_('If you run out of rows, click Save')}}</i>
|
||||||
|
<table class="table">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
{% for field in problems_form[0] %}
|
||||||
|
{% if not field.is_hidden %}
|
||||||
|
<th>
|
||||||
|
{{field.label}}
|
||||||
|
</th>
|
||||||
|
{% endif %}
|
||||||
|
{% endfor %}
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{% for form in problems_form %}
|
||||||
|
<tr>
|
||||||
|
{% for field in form %}
|
||||||
|
<td class="problems-{{field.name}}" title="
|
||||||
|
{{ field.help_text|safe if field.help_text }}"
|
||||||
|
style="{{ 'display:none' if field.is_hidden }}"
|
||||||
|
>{{field}}<div style="color:red">{{field.errors}}</div></td>
|
||||||
|
{% endfor %}
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<button type="submit">{{ _('Save') }}</button>
|
||||||
|
</form>
|
||||||
|
{% endblock %}
|
|
@ -1,4 +1,5 @@
|
||||||
<input
|
<input
|
||||||
|
autocomplete="off"
|
||||||
type="{{ widget.type }}"
|
type="{{ widget.type }}"
|
||||||
name="{{ widget.name }}"
|
name="{{ widget.name }}"
|
||||||
{% if widget.value != None %}
|
{% if widget.value != None %}
|
||||||
|
|
Loading…
Add table
Reference in a new issue