From cb410dd7c2aabf98ab3577c26916487f249e97a8 Mon Sep 17 00:00:00 2001 From: Gregory Trullemans Date: Sun, 10 Mar 2024 13:20:09 +0100 Subject: [PATCH] Add JS Script to Passe admin --- jarvis/objective/admin.py | 6 ++++ jarvis/objective/models.py | 62 +++++++++++++++++++++++--------------- static/js/admin/passe.js | 35 +++++++++++++++++++++ 3 files changed, 78 insertions(+), 25 deletions(-) create mode 100644 static/js/admin/passe.js diff --git a/jarvis/objective/admin.py b/jarvis/objective/admin.py index aa27276..fb2c398 100644 --- a/jarvis/objective/admin.py +++ b/jarvis/objective/admin.py @@ -207,6 +207,12 @@ class PasseAdmin(admin.ModelAdmin): ("difficulty", DropdownFilter), ) + class Media: + js = ( + "js/core/jquery-3.6.0.min.js", + "js/admin/passe.js", + ) + class TrainingProgramAdmin(admin.ModelAdmin): model = TrainingProgram diff --git a/jarvis/objective/models.py b/jarvis/objective/models.py index a7936e9..784ae4d 100644 --- a/jarvis/objective/models.py +++ b/jarvis/objective/models.py @@ -48,7 +48,7 @@ class Educative(Markdownizable): """ - AGE_CHOICES = ( + AGE_CHOICES = [ (6, "6-7"), (7, "7-8"), (8, "8-9"), @@ -61,7 +61,7 @@ class Educative(Markdownizable): (15, "15-16"), (16, "16-17"), (17, "17+"), - ) + ] class Meta: verbose_name = "Educatif" @@ -486,10 +486,10 @@ class Passe(Markdownizable): self.difficulty = 0 self.number_of_skill = 0 super().save(*args, **kwargs) - print("Dans le save") + # print("Dans le save") if self.educatives.count() == 0: - print("educative is none") + # print("educative is none") present = False operation_list = self.regexp.split(" ") for item in ROUTINE_TYPE_CHOICE: @@ -498,7 +498,7 @@ class Passe(Markdownizable): break if present and len(operation_list) == 2: - print("present") + # print("present") content = operation_list[1].replace("[", "").replace("]", "") ranks = content.split("-") @@ -724,47 +724,50 @@ class Passe(Markdownizable): return False # <-- - operation_list = regexp.split(" ") + argument_list = regexp.split(" ") - if len(operation_list) >= 3: + if len(argument_list) >= 3: return False - if len(operation_list) == 2: - if not Passe.is_valid_dot(operation_list[0]): - return False - - value = operation_list[0].replace(".", "") - is_valid_routine = Passe.is_valid_routine_type(value) - - if is_valid_routine: - return Passe.is_valid_subset(operation_list[1]) + if len(argument_list) == 2 and educatives_list.count() == 1: + return Passe.is_valid_regexp_two_args(argument_list[0], argument_list[1]) else: if ( - operation_list[0] == "WC" + argument_list[0] == "WC" and educatives_list is not None and len(educatives_list) == 2 ): return True - if re.match(r"[1-9]+\|", operation_list[0]) and len(educatives_list) >= 1: + if re.match(r"[1-9]+\|", argument_list[0]) and len(educatives_list) >= 1: return True - if not Passe.is_valid_dot(operation_list[0]): + if not Passe.is_valid_dot(argument_list[0]): return False - value = operation_list[0].replace(".", "") + value = argument_list[0].replace(".", "") is_valid_routine = Passe.is_valid_routine_type(value) if is_valid_routine: return True - return Passe.is_valid_subset(operation_list[0]) + return Passe.is_valid_subset(argument_list[0]) return False class TrainingProgram(Seasonisable, Markdownizable): - """Classe représentant un entraînement (ensemble de passage).""" + """Classe représentant un entraînement (ensemble de passage). + + TODO: + - renommer (supprimer/remettre) TrainingProgram en TrainingPasse + - supprimer Seasonisable + - supprimer Markdownizable + - supprimer le champ Gymnast + - supprimer score + - supprimer rank ??? (--> dans la M2M) + - supprimer difficulty ??? (--> dans la M2M) + """ class Meta: verbose_name = "Training Program" @@ -774,13 +777,13 @@ class TrainingProgram(Seasonisable, Markdownizable): ] unique_together = ["date", "gymnast", "rank"] - gymnast = models.ForeignKey("people.Gymnast", on_delete=models.CASCADE) + gymnast = models.ForeignKey("people.Gymnast", on_delete=models.CASCADE) # TO DELETE passe = models.ForeignKey(Passe, on_delete=models.CASCADE) repetition = models.PositiveSmallIntegerField(default=1) number_of_skill = models.PositiveSmallIntegerField(default=0) difficulty = models.DecimalField(max_digits=4, decimal_places=1, default=0.0) - rank = models.PositiveSmallIntegerField(default=1) - score = models.PositiveSmallIntegerField(blank=True, null=True) + rank = models.PositiveSmallIntegerField(default=1) # TO DELETE + score = models.PositiveSmallIntegerField(blank=True, null=True) # TO DELETE updated_at = models.DateTimeField(auto_now=True) def __str__(self): @@ -794,3 +797,12 @@ class TrainingProgram(Seasonisable, Markdownizable): self.difficulty = self.passe.difficulty * self.repetition self.number_of_skill = self.passe.number_of_skill * self.repetition super().save(*args, **kwargs) + + +# class Training(Seasonisable, Markdownizable): +# """Classe représentant un entraînement.""" + +# gymnast = models.ForeignKey("people.Gymnast", on_delete=models.CASCADE) +# difficulty = models.DecimalField(max_digits=4, decimal_places=1, default=0.0) +# number_of_skill = models.PositiveSmallIntegerField(default=0) +# score = models.PositiveSmallIntegerField(default=1) diff --git a/static/js/admin/passe.js b/static/js/admin/passe.js new file mode 100644 index 0000000..bf3f7f4 --- /dev/null +++ b/static/js/admin/passe.js @@ -0,0 +1,35 @@ +"use strict"; + +if (!$) { + const $ = django.jQuery; +} + +$(function(){ + + $('#id_regexp').on("focusout", function(){ + if($('#id_label').val() == "") + $('#id_label').val($('#id_regexp').val()); + else + $('#id_label').val($('#id_label').val() + " " + $('#id_regexp').val()); + + var operators = $('#id_regexp').val().split(' '); + const pattern = /\[[2-9]+\-[2-9]+\]/ + + if(operators.length >= 2) + var subset = operators[1]; + else + var subset = operators[0]; + + if(pattern.test(subset)){ + subset = subset.replace("[", "").replace("]", ""); + var values = subset.split("-"); + + if(values[1] != "") + if(values[0] != "") + $('#id_number_of_skill').val(parseInt(values[1]) - parseInt(values[0])); + else + $('#id_number_of_skill').val(parseInt(values[1])); + } + }); + +}); \ No newline at end of file