mirror of
https://gitlab.com/animath/si/plateforme-corres2math.git
synced 2025-06-22 13:58:23 +02:00
Update a team in a popup <3
This commit is contained in:
@ -4,7 +4,7 @@ from django import forms
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from .models import Team
|
||||
from .models import Team, Participation
|
||||
|
||||
|
||||
class TeamForm(forms.ModelForm):
|
||||
@ -36,3 +36,9 @@ class JoinTeamForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = Team
|
||||
fields = ('access_code',)
|
||||
|
||||
|
||||
class ParticipationForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = Participation
|
||||
fields = ('problem',)
|
||||
|
@ -2,6 +2,8 @@ from django.core.validators import RegexValidator
|
||||
from django.db import models
|
||||
from django.db.models import Index
|
||||
from django.utils.crypto import get_random_string
|
||||
from django.utils.functional import lazy
|
||||
from django.utils.text import format_lazy
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
|
||||
@ -56,7 +58,7 @@ class Participation(models.Model):
|
||||
)
|
||||
|
||||
problem = models.IntegerField(
|
||||
choices=[(i, _("Problem #{problem:d}").format(problem=i)) for i in range(1, 5)],
|
||||
choices=[(i, format_lazy(_("Problem #{problem:d}"), problem=i)) for i in range(1, 5)],
|
||||
null=True,
|
||||
default=None,
|
||||
verbose_name=_("problem number"),
|
||||
|
@ -10,4 +10,4 @@
|
||||
</div>
|
||||
<button class="btn btn-success" type="submit">{% trans "Create" %}</button>
|
||||
</form>
|
||||
{% endblock content %}
|
||||
{% endblock content %}
|
||||
|
@ -1,4 +1,4 @@
|
||||
{% load crispy_forms_filters i18n %}
|
||||
{% load i18n %}
|
||||
|
||||
<div id="createTeamModal" class="modal fade" tabindex="-1" role="dialog">
|
||||
<div class="modal-dialog" role="document">
|
||||
|
@ -1,4 +1,4 @@
|
||||
{% load crispy_forms_filters i18n %}
|
||||
{% load i18n %}
|
||||
|
||||
<div id="joinTeamModal" class="modal fade" tabindex="-1" role="dialog">
|
||||
<div class="modal-dialog" role="document">
|
||||
|
@ -27,8 +27,25 @@
|
||||
<dd class="col-sm-6">{{ team.students.all|join:", "|default:any }}</dd>
|
||||
|
||||
<dt class="col-sm-6 text-right">{% trans "Chosen problem:" %}</dt>
|
||||
<dd class="col-sm-6">{{ team.participation.problem|default:any }}</dd>
|
||||
<dd class="col-sm-6">{{ team.participation.get_problem_display|default:any }}</dd>
|
||||
</dl>
|
||||
</div>
|
||||
<div class="card-footer text-center">
|
||||
<button class="btn btn-primary" data-toggle="modal" data-target="#updateTeamModal">{% trans "Update" %}</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% include "participation/update_team_modal.html" %}
|
||||
{% endblock %}
|
||||
|
||||
{% block extrajavascript %}
|
||||
<script>
|
||||
$(document).ready(function() {
|
||||
$('button[data-target="#updateTeamModal"]').click(function() {
|
||||
let modalBody = $("#updateTeamModal div.modal-body");
|
||||
if (!modalBody.html().trim())
|
||||
modalBody.load("{% url "participation:update_team" pk=team.pk %} #form-content");
|
||||
});
|
||||
});
|
||||
</script>
|
||||
{% endblock %}
|
||||
|
15
apps/participation/templates/participation/update_team.html
Normal file
15
apps/participation/templates/participation/update_team.html
Normal file
@ -0,0 +1,15 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% load crispy_forms_filters i18n %}
|
||||
|
||||
{% block content %}
|
||||
<form method="post">
|
||||
<div id="form-content">
|
||||
{% csrf_token %}
|
||||
{{ form|crispy }}
|
||||
{{ participation_form|crispy }}
|
||||
</div>
|
||||
<button class="btn btn-success" type="submit">{% trans "Update" %}</button>
|
||||
</form>
|
||||
{% endblock content %}
|
||||
|
@ -0,0 +1,21 @@
|
||||
{% load i18n %}
|
||||
|
||||
<div id="updateTeamModal" class="modal fade" tabindex="-1" role="dialog">
|
||||
<div class="modal-dialog" role="document">
|
||||
<form method="post" action="{% url "participation:update_team" pk=team.pk %}">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title">{% trans "Update team" %}</h5>
|
||||
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
|
||||
<span aria-hidden="true">×</span>
|
||||
</button>
|
||||
</div>
|
||||
<div class="modal-body"></div>
|
||||
<div class="modal-footer">
|
||||
<button type="submit" class="btn btn-primary">{% trans "Update" %}</button>
|
||||
<button type="button" class="btn btn-secondary" data-dismiss="modal">{% trans "Close" %}</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
@ -1,6 +1,6 @@
|
||||
from django.urls import path
|
||||
|
||||
from .views import CreateTeamView, JoinTeamView, MyTeamDetailView, TeamDetailView
|
||||
from .views import CreateTeamView, JoinTeamView, MyTeamDetailView, TeamDetailView, TeamUpdateView
|
||||
|
||||
|
||||
app_name = "participation"
|
||||
@ -10,4 +10,5 @@ urlpatterns = [
|
||||
path("join_team/", JoinTeamView.as_view(), name="join_team"),
|
||||
path("team/", MyTeamDetailView.as_view(), name="my_team_detail"),
|
||||
path("team/<int:pk>/", TeamDetailView.as_view(), name="team_detail"),
|
||||
path("team/<int:pk>/update/", TeamUpdateView.as_view(), name="update_team"),
|
||||
]
|
||||
|
@ -3,9 +3,9 @@ from django.core.exceptions import PermissionDenied
|
||||
from django.db import transaction
|
||||
from django.urls import reverse_lazy
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.views.generic import CreateView, FormView, DetailView, RedirectView
|
||||
from django.views.generic import CreateView, FormView, DetailView, RedirectView, UpdateView
|
||||
|
||||
from participation.forms import TeamForm, JoinTeamForm
|
||||
from participation.forms import TeamForm, JoinTeamForm, ParticipationForm
|
||||
from participation.models import Team
|
||||
|
||||
|
||||
@ -74,3 +74,27 @@ class MyTeamDetailView(LoginRequiredMixin, RedirectView):
|
||||
|
||||
class TeamDetailView(LoginRequiredMixin, DetailView):
|
||||
model = Team
|
||||
|
||||
|
||||
class TeamUpdateView(LoginRequiredMixin, UpdateView):
|
||||
model = Team
|
||||
form_class = TeamForm
|
||||
template_name = "participation/update_team.html"
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
context["participation_form"] = ParticipationForm(data=self.request.POST or None,
|
||||
instance=self.object.participation)
|
||||
return context
|
||||
|
||||
@transaction.atomic
|
||||
def form_valid(self, form):
|
||||
participation_form = ParticipationForm(data=self.request.POST or None, instance=self.object.participation)
|
||||
if not participation_form.is_valid():
|
||||
return self.form_invalid(form)
|
||||
|
||||
participation_form.save()
|
||||
return super().form_valid(form)
|
||||
|
||||
def get_success_url(self):
|
||||
return reverse_lazy("participation:team_detail", args=(self.object.pk,))
|
||||
|
Reference in New Issue
Block a user