From ead59e28b8c121f040867f7807c395a2ee9d9747 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Tue, 12 Jan 2021 17:51:55 +0100 Subject: [PATCH] Upload to the good place --- apps/participation/models.py | 16 +++++++++++++--- .../participation/participation_detail.html | 6 ++---- .../templates/participation/upload_solution.html | 2 +- apps/participation/views.py | 9 ++++----- 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/apps/participation/models.py b/apps/participation/models.py index f5b92b2..78db948 100644 --- a/apps/participation/models.py +++ b/apps/participation/models.py @@ -312,6 +312,15 @@ class Pool(models.Model): verbose_name_plural = _("pools") +def get_solution_filename(instance, filename): + return f"solutions/{instance.participation.team.trigram}_{instance.problem}" \ + + ("final" if instance.final_solution else "") + + +def get_random_synthesis_filename(instance, filename): + return "syntheses/" + get_random_string(64) + + class Solution(models.Model): participation = models.ForeignKey( Participation, @@ -331,14 +340,15 @@ class Solution(models.Model): file = models.FileField( verbose_name=_("file"), - upload_to="solutions/", + upload_to=get_solution_filename, unique=True, blank=True, default="", ) def __str__(self): - return repr(self) + return _("Solution of team {team} for problem {problem}")\ + .format(team=self.participation.team.name, problem=self.problem) class Meta: verbose_name = _("solution") @@ -369,7 +379,7 @@ class Synthesis(models.Model): file = models.FileField( verbose_name=_("file"), - upload_to="syntheses/", + upload_to=get_random_synthesis_filename, unique=True, blank=True, default="", diff --git a/apps/participation/templates/participation/participation_detail.html b/apps/participation/templates/participation/participation_detail.html index eafd4a5..4aae765 100644 --- a/apps/participation/templates/participation/participation_detail.html +++ b/apps/participation/templates/participation/participation_detail.html @@ -19,9 +19,7 @@
{% trans "Solutions:" %}
{% for solution in participation.solutions.all %} - - {% blocktrans trimmed with problem=solution.problem %}problem {{ problem }}{% endblocktrans %}{% if not forloop.last %}, {% endif %} - + {{ solution }}{% if not forloop.last %}, {% endif %} {% empty %} {% trans "No solution was uploaded yet." %} {% endfor %} @@ -36,7 +34,7 @@ {% trans "Upload solution" as modal_title %} {% trans "Upload" as modal_button %} {% url "participation:upload_solution" pk=participation.pk as modal_action %} - {% include "base_modal.html" with modal_id="uploadSolution" %} + {% include "base_modal.html" with modal_id="uploadSolution" modal_enctype="multipart/form-data" %} {% endblock %} {% block extrajavascript %} diff --git a/apps/participation/templates/participation/upload_solution.html b/apps/participation/templates/participation/upload_solution.html index 8acce11..378585f 100644 --- a/apps/participation/templates/participation/upload_solution.html +++ b/apps/participation/templates/participation/upload_solution.html @@ -3,7 +3,7 @@ {% load crispy_forms_filters i18n %} {% block content %} -
+
{% csrf_token %} {{ form|crispy }} diff --git a/apps/participation/views.py b/apps/participation/views.py index 2f1be04..5eedd70 100644 --- a/apps/participation/views.py +++ b/apps/participation/views.py @@ -456,11 +456,10 @@ class SolutionUploadView(LoginRequiredMixin, FormView): """ form_sol = form.instance # Drop previous solution if existing - Solution.objects.filter( - participation=self.participation, - problem=form_sol.problem, - final_solution=self.participation.final, - ).delete() + for sol in Solution.objects.filter(participation=self.participation, + problem=form_sol.problem, + final_solution=self.participation.final).all(): + sol.delete() form_sol.participation = self.participation form_sol.final = self.participation.final form_sol.save()