From 35a197be07000d9b43c101f65730af31a7eb6b52 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 8 Jan 2021 13:37:14 +0100 Subject: [PATCH] Questions can be seen during the third phase --- apps/participation/models.py | 2 +- .../participation/participation_detail.html | 33 ++++++++++--------- apps/participation/views.py | 11 +++++++ 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/apps/participation/models.py b/apps/participation/models.py index f83fd98..89fc55b 100644 --- a/apps/participation/models.py +++ b/apps/participation/models.py @@ -288,7 +288,7 @@ class Phase(models.Model): ) @classmethod - def current_phase(cls): + def current_phase(cls) -> "Phase": """ Retrieve the current phase of this day """ diff --git a/apps/participation/templates/participation/participation_detail.html b/apps/participation/templates/participation/participation_detail.html index a232fbd..4e77064 100644 --- a/apps/participation/templates/participation/participation_detail.html +++ b/apps/participation/templates/participation/participation_detail.html @@ -194,13 +194,14 @@ {% trans "This video platform is not supported yet." as unsupported_platform %} {% include "base_modal.html" with modal_id="displayOtherSolution" modal_action="" modal_button="" modal_additional_class="modal-lg" modal_content=participation.received_participation.solution.as_iframe|default:unsupported_platform %} {% endif %} -{% endif %} -{% if user.registration.participates and current_phase.phase_number == 2 %} - {% trans "Add question" as modal_title %} - {% trans "Add" as modal_button %} - {% url "participation:add_question" pk=participation.pk as modal_action %} - {% include "base_modal.html" with modal_id="addQuestion" modal_button_type="success" %} + {% if current_phase.phase_number == 2 %} + {% trans "Add question" as modal_title %} + {% trans "Add" as modal_button %} + {% url "participation:add_question" pk=participation.pk as modal_action %} + {% include "base_modal.html" with modal_id="addQuestion" modal_button_type="success" %} + {% endif %} + {% for question in participation.questions.all %} {% with number_str=forloop.counter|stringformat:"d"%} {% with modal_id="updateQuestion"|add:number_str %} @@ -260,27 +261,29 @@ }); {% endif %} - {% if user.registration.participates and current_phase.phase_number == 2 %} + {% if current_phase.phase_number == 2 %} $('button[data-target="#addQuestionModal"]').click(function() { let modalBody = $("#addQuestionModal div.modal-body"); if (!modalBody.html().trim()) modalBody.load("{% url "participation:add_question" pk=participation.pk %} #form-content"); }); + {% endif %} - {% for question in participation.questions.all %} - $('button[data-target="#updateQuestion{{ forloop.counter }}Modal"]').click(function() { - let modalBody = $("#updateQuestion{{ forloop.counter }}Modal div.modal-body"); - if (!modalBody.html().trim()) - modalBody.load("{% url "participation:update_question" pk=question.pk %} #form-content"); - }); + {% for question in participation.questions.all %} + $('button[data-target="#updateQuestion{{ forloop.counter }}Modal"]').click(function() { + let modalBody = $("#updateQuestion{{ forloop.counter }}Modal div.modal-body"); + if (!modalBody.html().trim()) + modalBody.load("{% url "participation:update_question" pk=question.pk %} #form-content"); + }); + {% if current_phase.phase_number == 2 %} $('button[data-target="#deleteQuestion{{ forloop.counter }}Modal"]').click(function() { let modalBody = $("#deleteQuestion{{ forloop.counter }}Modal div.modal-body"); if (!modalBody.html().trim()) modalBody.load("{% url "participation:delete_question" pk=question.pk %} #form-content"); }); - {% endfor %} - {% endif %} + {% endif %} + {% endfor %} $('button[data-target="#uploadSolutionModal"]').click(function() { let modalBody = $("#uploadSolutionModal div.modal-body"); diff --git a/apps/participation/views.py b/apps/participation/views.py index d68ea42..e23743e 100644 --- a/apps/participation/views.py +++ b/apps/participation/views.py @@ -477,6 +477,12 @@ class UpdateQuestionView(LoginRequiredMixin, UpdateView): return super().dispatch(request, *args, **kwargs) raise PermissionDenied + def form_valid(self, form): + if not self.request.user.registration.is_admin and Phase.current_phase().phase_number != 2: + form.add_error(None, _("You can update your questions now.")) + return self.form_invalid(form) + return super().form_valid(form) + def get_success_url(self): return reverse_lazy("participation:participation_detail", args=(self.object.participation.pk,)) @@ -499,6 +505,11 @@ class DeleteQuestionView(LoginRequiredMixin, DeleteView): return super().dispatch(request, *args, **kwargs) raise PermissionDenied + def delete(self, request, *args, **kwargs): + if not request.user.registration.is_admin and Phase.current_phase().phase_number != 2: + raise PermissionDenied(_("You can update your questions now.")) + return super().delete(request, *args, **kwargs) + def get_success_url(self): return reverse_lazy("participation:participation_detail", args=(self.object.participation.pk,))