[WIP] Minor updates

This commit is contained in:
Gregory Trullemans 2024-03-29 14:06:05 +01:00
parent 42467ac19c
commit 4597b859f5
4 changed files with 101 additions and 87 deletions

View File

@ -455,7 +455,7 @@ class RoutineSkill(models.Model):
"""
class Meta:
ordering = ("rank",)
ordering = ["routine", "rank"]
unique_together = ["routine", "rank"]
routine = models.ForeignKey(
@ -582,7 +582,7 @@ class GymnastTrainingRound(Markdownizable):
class Meta:
verbose_name = "Gymnast Training Round"
verbose_name_plural = "Gymnast Training Rounds"
ordering = ("rank",)
ordering = ["gymnast_training", "rank"]
unique_together = ["gymnast_training", "rank"]
gymnast_training = models.ForeignKey(

View File

@ -11,41 +11,50 @@
<h4 class="">Compose Training : {{ gymnast_training.gymnast }} - {{ gymnast_training.date|date:"l j F Y" }}</h4>
</div>
<div class="card-body">
<ol>
{% for training_round in training_round_list %}
<ul class="no-bullets" id="ul-rounds-list"><li>
<div class="form-group row pb-0 mb-0">
<label class="col-2 col-form-label pb-0 text-center">Rank</label>
<label class="col-7 col-form-label pb-0">Label</label>
<label class="col-3 col-form-label pb-0">Repetition</label>
</div>
</li>
{% for gtr in gtr_list %}
<li>
<div class="form-group row pb-0 mb-0">
<label class="col-1 col-form-label pt-2 pb-0 text-right">&nbsp;</label>
<div class="col-11 pt-2 text-danger">
{{ training_round.label }}
</div>
<div class="form-group row pb-0 mb-0">
<label class="col-2 col-form-label pt-2 pb-0 text-center">{{ gtr.rank }}</label>
<div class="col-7 pt-2 text-danger">
{{ gtr.training_round.label }}
</div>
</li>
{% endfor %}
<li>
<div class="form-group row pb-0 mb-0">
<label class="col-1 col-form-label text-right mb-0">&nbsp;</label>
<div class="col-7 mb-0">
<input type="hidden" name="traininground_id" id="id_traininground_id" value="">
<input type="text" name="traininground" placeholder="Training round" class="form-control selectpicker" id="id_traininground">
</div>
<div class="col-3 mb-0">
<select type="text" name="repetition" placeholder="Repetition" class="form-control selectpicker" id="id_repetition">
<option>-----</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
</select>
</div>
<div class="col-3 pt-2 text-danger">
{{ gtr.repetition }}
</div>
</div>
</li>
</ol>
{% endfor %}
<li>
<div class="form-group row pb-0 mb-0">
<label class="col-2 col-form-label text-right mb-0 text-center" id="label_rank">{{ rank }}</label>
<div class="col-7 mb-0">
<input type="hidden" name="traininground_id" id="id_traininground_id" value="">
<input type="text" name="traininground" placeholder="Training round" class="form-control selectpicker" id="id_traininground">
</div>
<div class="col-3 mb-0">
<select type="text" name="repetition" placeholder="Repetition" class="form-control selectpicker" id="id_repetition">
<option>-----</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
</select>
</div>
</div>
</li>
</ul>
</div>
<div class="card-footer pt-0">
<a href="{% url 'gymnast_training_details' gymnast_training.id %}">
@ -62,12 +71,14 @@
{% block footerscript %}
<script type="text/javascript">
const csrf_token = "{{ csrf_token|escapejs }}";
const traininground_lookup = "{% url 'traininground_lookup' %}";
$(document).ready(function() {
var repetition = 0;
var round_label = "";
var rank = {{ rank }};
$('#id_traininground').focus();
function insert_round()
function insert_round(repetition)
{
$.ajax({
url: "{% url 'link_round_to_training' %}",
@ -80,25 +91,22 @@
csrfmiddlewaretoken: '{{ csrf_token }}'
},
}).done(function() {
// landing_position_id = ui.item.landingid;
insert_selected_round(ui.item.notation);
insert_selected_round(round_label, repetition);
});
}
$('#id_traininground').focus();
$('#id_repetition').on("change", function(){
if($("select option:selected").val() != "-----") {
// alert($('#id_traininground').val());
insert_round();
}
if($("select option:selected").val() != "-----")
insert_round($("select option:selected").val());
});
function insert_selected_round(skill_label="Test")
function insert_selected_round(round_label="Test", repetition=1)
{
$('#minusButton').remove();
$('ol li:last-child').before('<li><div class="form-group row pb-0 mb-0"><label class="col-1 col-sm-1 col-md-1 col-lg-1 col-xl-1 col-form-label text-right mb-0">&nbsp;</label><div class="col-3 col-sm-3 col-md-3 col-lg-3 col-xl-3 pt-2 mb-0">' + skill_label + '</div><div class="col-1 col-sm-1 col-md-1 col-lg-1 col-xl-1"><button type="button" class="btn btn-warning btn-sm" id="minusButton">-</button></div></div></li>');
$('#id_skill').val('').focus();
// $('#minusButton').remove();
$('#ul-rounds-list li:last-child').before('<div class="form-group row pb-0 mb-0"><label class="col-2 col-form-label pt-2 pb-0 text-center">' + rank + '</label><div class="col-7 pt-2 text-danger">' + round_label + '</div><div class="col-3 pt-2 text-danger">' + repetition + '</div></div>');
rank += 1;
$('#label_rank').html(rank);
$('#id_traininground').val('').focus();
}
// $('body').on('click', '#minusButton', function(event){
@ -118,7 +126,7 @@
$('#id_traininground').autocomplete({
source: function(request, response) {
$.ajax({
url: traininground_lookup,
url: "{% url 'traininground_lookup' %}",
method: "POST",
data: {
pattern: request.term,
@ -147,8 +155,8 @@
},
minLength: 2,
select: function (event, ui) {
$('#id_traininground_id').val(ui.item.trainingroun_id)
rank += 1;
$('#id_traininground_id').val(ui.item.trainingroun_id);
round_label = ui.item.label;
$('#id_repetition').focus();
},
{% if request.session.template == 0 %}

View File

@ -485,12 +485,8 @@ def link_skill_to_combination(request):
form = CombinationSkillForm(data)
if form.is_valid():
link, created = RoutineSkill.objects.get_or_create(
routine=form.cleaned_data["routine"],
skill=form.cleaned_data["skill"],
rank=form.cleaned_data["rank"],
)
return HttpResponse(200, (link, created))
form.save()
return HttpResponse(200)
return HttpResponse(406)
@ -598,7 +594,7 @@ def traininground_create_or_update(request, traininground_id=None):
return HttpResponseRedirect(
reverse("traininground_details", args=(traininground.pk,))
)
# print(form.errors)
return render(request, "trainingrounds/create.html", {"form": form})
form = TrainingRoundForm(instance=traininground)
@ -612,7 +608,7 @@ def gymnast_training_details(request, gymnast_training_id):
"""Détails d'un entraînement."""
gymnast_training = get_object_or_404(GymnastTraining, pk=gymnast_training_id)
training_rounds = gymnast_training.training_rounds.all()
training_rounds = gymnast_training.rounds_links.all()
context = {
"gymnast": gymnast_training.gymnast,
@ -631,6 +627,8 @@ def switch_traininground(request):
J'utilise `32767` comme valeur intermédiaire pour le `rank` car c'est la limite supérieure d'un
PositiveSmallIntegerField.
TODO: ramener un rank d'un seul coup.
"""
try:
target_trainingprogram_id = request.POST.get("tpid", None)
@ -646,21 +644,33 @@ def switch_traininground(request):
.order_by("-id")
.first()
)
new_rank = (target_trainingpround.rank - source_traininground.rank) - 1
is_switchable = (source_traininground.rank == new_rank)
else:
source_traininground = (
GymnastTrainingRound.objects.filter(rank__gt=target_trainingpround.rank)
.order_by("id")
.first()
)
new_rank = (source_traininground.rank - target_trainingpround.rank) - 1
is_switchable = (source_traininground.rank == new_rank)
saved_source_rank = source_traininground.rank
saved_target_rank = target_trainingpround.rank
source_traininground.rank = 32767
source_traininground.save()
target_trainingpround.rank = saved_source_rank
target_trainingpround.save()
source_traininground.rank = saved_target_rank
source_traininground.save()
if is_switchable:
saved_source_rank = source_traininground.rank
saved_target_rank = target_trainingpround.rank
source_traininground.rank = 32767
source_traininground.save()
target_trainingpround.rank = saved_source_rank
target_trainingpround.save()
source_traininground.rank = saved_target_rank
source_traininground.save()
else:
if direction == 0:
new_rank = source_traininground.rank + 1
else:
new_rank = source_traininground.rank - 1
target_trainingpround.rank = new_rank
target_trainingpround.save()
return HttpResponse(200)
except Exception:
@ -694,7 +704,7 @@ def gymnast_training_create_or_update(request, gymnasttraining_id=None, gymnast_
return HttpResponseRedirect(
reverse("gymnast_training_details", args=(gymnasttraining.pk,))
)
# print(form.errors)
return render(request, "gymnasttrainings/create.html", {"form": form})
form = GymnastTrainingForm(instance=gymnasttraining, initial=data)
@ -729,13 +739,17 @@ def gymnast_training_compose(request, gymnast_training_id):
"""
gymnast_training = get_object_or_404(GymnastTraining, pk=gymnast_training_id)
training_round_list = gymnast_training.trainingrounds.all()
gtr_list = gymnast_training.rounds_links.all()
rank = gtr_list.count()
if not rank:
print("rank vide")
rank = 1
print(rank)
context = {
"gymnast_training": gymnast_training,
"training_round_list": training_round_list,
"rank": training_round_list.count(),
# "skill_list": skill_list,
"gtr_list": gtr_list,
"rank": rank,
}
return render(request, "gymnasttrainings/compose.html", context)
@ -750,7 +764,6 @@ def traininground_lookup(request):
trainingrounds = []
pattern = request.POST.get("pattern", None)
# Ignore queries shorter than length 2
if pattern is not None and len(pattern) > 1:
traininground_list = TrainingRound.objects.filter(label__icontains=pattern)
trainingrounds = [
@ -776,23 +789,10 @@ def link_round_to_training(request):
"repetition": request.POST.get("repetition", 0),
"rank": request.POST.get("rank", 0),
}
print()
print()
print(data)
print()
print()
form = GymnastTrainingRoundForm(data)
if form.is_valid():
# print("Form valid.")
link, created = GymnastTrainingRound.objects.get_or_create(
gymnast_training=form.cleaned_data["gymnast_training"],
training_round=form.cleaned_data["training_round"],
repetition=form.cleaned_data["repetition"],
rank=form.cleaned_data["rank"],
)
print(created)
return HttpResponse(200, (link, created))
form.save()
return HttpResponse(200)
print("Form not valid.")
return HttpResponse(406)

View File

@ -144,3 +144,9 @@ textarea.form-control {
background-size: 210% 210%;
background-position: top right;
}
ul.no-bullets {
list-style-type: none; /* Remove bullets */
padding: 0; /* Remove padding */
margin: 0; /* Remove margins */
}