From a87d4f41c82f9838bc050bee7d5c6703d1e8626d Mon Sep 17 00:00:00 2001 From: Gregory Trullemans Date: Sun, 3 Mar 2024 14:29:11 +0100 Subject: [PATCH] =?UTF-8?q?Un=20paquet=20d'am=C3=A9liorations=20django,=20?= =?UTF-8?q?JS,=20=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...s_trainingprogram_informations_and_more.py | 33 +++++++ jarvis/objective/models.py | 12 +-- .../objective/templates/passes/details.html | 86 +++++++++++++++++-- .../templates/trainingprograms/details.html | 37 +++++--- jarvis/objective/urls.py | 5 ++ jarvis/objective/views.py | 39 +++++++-- 6 files changed, 178 insertions(+), 34 deletions(-) create mode 100644 jarvis/objective/migrations/0019_alter_passe_options_trainingprogram_informations_and_more.py diff --git a/jarvis/objective/migrations/0019_alter_passe_options_trainingprogram_informations_and_more.py b/jarvis/objective/migrations/0019_alter_passe_options_trainingprogram_informations_and_more.py new file mode 100644 index 0000000..68ce75d --- /dev/null +++ b/jarvis/objective/migrations/0019_alter_passe_options_trainingprogram_informations_and_more.py @@ -0,0 +1,33 @@ +# Generated by Django 4.2 on 2024-03-03 10:45 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("objective", "0018_alter_trainingprogram_options_and_more"), + ] + + operations = [ + migrations.AlterModelOptions( + name="passe", + options={"ordering": ["label"]}, + ), + migrations.AddField( + model_name="trainingprogram", + name="informations", + field=models.TextField( + blank=True, + help_text="Only MarkDown is authorized", + null=True, + verbose_name="Comments", + ), + ), + migrations.AlterField( + model_name="passe", + name="label", + field=models.CharField(default="-", max_length=30), + preserve_default=False, + ), + ] diff --git a/jarvis/objective/models.py b/jarvis/objective/models.py index 5586fb0..fbef545 100644 --- a/jarvis/objective/models.py +++ b/jarvis/objective/models.py @@ -465,7 +465,10 @@ class RoutineSkill(models.Model): class Passe(Markdownizable): """Classe représentant les passages (à faire pendant un entraînement).""" - label = models.CharField(max_length=30, null=True, blank=True) + class Meta: + ordering = ["label"] + + label = models.CharField(max_length=30) educatives = models.ManyToManyField(Educative) regexp = models.CharField(max_length=50, null=True, blank=True) number_of_skill = models.PositiveSmallIntegerField(default=0) @@ -522,13 +525,10 @@ class Passe(Markdownizable): super().save(*args, **kwargs) def __str__(self): - if self.label: - return f"{self.label} ({self.number_of_skill} | {self.difficulty})" - else: - return f"- ({self.number_of_skill} | {self.difficulty})" + return f"{self.label} ({self.number_of_skill} | {self.difficulty})" -class TrainingProgram(Seasonisable): +class TrainingProgram(Seasonisable, Markdownizable): """Classe représentant un entraînement (ensemble de passage).""" class Meta: diff --git a/jarvis/objective/templates/passes/details.html b/jarvis/objective/templates/passes/details.html index 5558adf..33d52d5 100644 --- a/jarvis/objective/templates/passes/details.html +++ b/jarvis/objective/templates/passes/details.html @@ -1,21 +1,89 @@ {% extends "base.html" %} {% load has_group %} -{% block page_title %}{{ combination.short_label }}{% endblock %} +{% block page_title %}Passe details{% endblock %} {% block content %}
-
- Label : {{ passe.label }}
- Content : {% for educative in passe.educative.all %}{{ educative }}{% if not forloop.last %} & {% endif %}{% endfor %}
- Répétition : {{ passe.repetition }}
- {% if passe.regexp %}RegExp : {{ passe.regexp }}
{% endif %} -
- {{ passe.label }}{% if passe.regexp %}{{ passe.regexp }}{% endif %}   {{ passe.repetition }}

- {{ number_of_skill }} - {{ difficulty }} + +
+

Passe details

+
{{ passe.label }}{% if passe.regexp %}{{ passe.regexp }}{% endif %}   {{ passe.repetition }}
+ +
+
+ +
+ +
+ {{ passe.label }} +
+
+ +
+ +
+ {% if passe.educatives.all.count > 1%} +
    + {% for educative in passe.educatives.all %} +
  • {{ educative }}
  • + {% endfor %} +
+ {% else %} + {{ passe.educatives }} + {% endif %} +
+
+ + {% if passe.regexp %} +
+ +
+ {{ passe.regexp }} +
+
+ {% endif %} + +
+ +
+ {{ number_of_skill }} +
+
+ +
+ +
+ {{ difficulty }} +
+
+ + {% if information %} +
+ +
+ {{ informations }} +
+
+ {% endif %} + +
+ +
+ + +
diff --git a/jarvis/objective/templates/trainingprograms/details.html b/jarvis/objective/templates/trainingprograms/details.html index 1af95d3..38a7bc2 100644 --- a/jarvis/objective/templates/trainingprograms/details.html +++ b/jarvis/objective/templates/trainingprograms/details.html @@ -1,7 +1,7 @@ {% extends "base.html" %} {% load has_group %} -{% block page_title %}{{ combination.short_label }}{% endblock %} +{% block page_title %}Training Program{% endblock %} {% block content %}
@@ -18,18 +18,19 @@ {% for trainingprogram in trainingprogram_list %} - {{ trainingprogram.rank }} - {{ trainingprogram.passe.label }}{% if trainingprogram.repetition != 1 %}   {{ trainingprogram.repetition }}{% endif %} + {{ trainingprogram.rank }} + {{ trainingprogram.passe.label }}{% if trainingprogram.repetition != 1 %}   {{ trainingprogram.repetition }}{% endif %} {{ trainingprogram.number_of_skill}} {{ trainingprogram.difficulty }} {% if request.user|has_group:"trainer" %} - - + + - + + @@ -60,16 +61,26 @@ $(document).ready(function(){ $(".up,.down").click(function(){ var row = $(this).parents("tr:first"); + var first_td = row.find("td:first"); + saved_rank = first_td.text(); // jusque là, ca marche ! + // alert("Rank : " + saved_rank); + if ($(this).is(".up")) { $.ajax({ url: "{% url 'switch_trainingprogram_line' %}", method: "POST", data: { - tp1: 5, - tp2: 6, + tpid: $(this).data('tp_id'), + direction: 0, csrfmiddlewaretoken: '{{ csrf_token }}' }, }).done(function() { + var previous_td = row.prev().find('td:first'); + saved_previous_rank = previous_td.text(); + + first_td.html("" + String(saved_previous_rank) + ""); + previous_td.html("" + String(saved_rank) + ""); + row.insertBefore(row.prev()); }); } else { @@ -77,11 +88,17 @@ $(document).ready(function(){ url: "{% url 'switch_trainingprogram_line' %}", method: "POST", data: { - tp1: 6, - tp2: 5, + tpid: $(this).data('tp_id'), + direction: 1, csrfmiddlewaretoken: '{{ csrf_token }}' }, }).done(function() { + var next = row.next().find('td:first'); + saved_next_rank = previous_td.text(); + + first_td.html("" + String(saved_next_rank) + ""); + next.html("" + String(saved_rank) + ""); + row.insertAfter(row.next()); }); } diff --git a/jarvis/objective/urls.py b/jarvis/objective/urls.py index e77b490..bec4be4 100644 --- a/jarvis/objective/urls.py +++ b/jarvis/objective/urls.py @@ -7,6 +7,11 @@ urlpatterns = [ # # path(r"educative/lookup/", views.educative_lookup, name="educative_lookup"), + path( + r"educative/details/", + views.educative_details, + name="educative_details", + ), # # SKILLS # diff --git a/jarvis/objective/views.py b/jarvis/objective/views.py index b69c920..d1b4776 100644 --- a/jarvis/objective/views.py +++ b/jarvis/objective/views.py @@ -49,6 +49,17 @@ def educative_lookup(request): return JsonResponse(results, safe=False) +@login_required +@require_http_methods(["GET"]) +def educative_details(request, educative_id): + """Renvoie les détails d'un educatif suivant que ce soit une combination ou un skill""" + try: + Routine.objects.get(pk=educative_id) + return combination_details(request, educative_id) + except Routine.DoesNotExist: + return skill_details(request, educative_id) + + @login_required @require_http_methods(["POST"]) def skill_lookup(request): @@ -596,22 +607,32 @@ def trainingprogram_details(request, date=None, gymnast_id=None): def switch_trainingprogram_line(request): """ Recoit dans request deux identifiants de trainingprogram qu'il faut échanger () : - - tp1 (int) identifiant d'une instance de TraiingProgram - - tp2 (int) identifiant d'une instance de TraiingProgram + - tp_id (int) identifiant d'une instance de TraiingProgram + - direction (str) la direction du changement (0: haut, 1: bas) J'utilise `32767` comme valeur intermédiaire pour le `rank` car c'est la limite supérieure d'un PositiveSmallIntegerField. """ try: - target_trainingprogram_id = request.POST.get("tp1", None) - source_trainingprogram_id = request.POST.get("tp2", None) + target_trainingprogram_id = request.POST.get("tpid", None) + direction = int(request.POST.get("direction", 0)) target_trainingprogram = get_object_or_404( TrainingProgram, pk=target_trainingprogram_id ) - source_trainingprogram = get_object_or_404( - TrainingProgram, pk=source_trainingprogram_id - ) + + if direction == 0: + source_trainingprogram = ( + TrainingProgram.objects.filter(rank__lt=target_trainingprogram.rank) + .order_by("-id") + .first() + ) + else: + source_trainingprogram = ( + TrainingProgram.objects.filter(rank__gt=target_trainingprogram.rank) + .order_by("id") + .first() + ) saved_source_rank = source_trainingprogram.rank saved_target_rank = target_trainingprogram.rank @@ -621,7 +642,7 @@ def switch_trainingprogram_line(request): target_trainingprogram.save() source_trainingprogram.rank = saved_target_rank source_trainingprogram.save() + + return HttpResponse(200) except Exception: return HttpResponse(409) - - return HttpResponse(200)