1
0
mirror of https://gitlab.crans.org/bde/nk20 synced 2025-08-14 09:56:38 +02:00

Achievement unicity && management pop-up behaviour

This commit is contained in:
Ehouarn
2025-08-13 23:57:05 +02:00
parent 61057b71ba
commit 4d567cdcc7
6 changed files with 111 additions and 47 deletions

View File

@@ -75,13 +75,24 @@ class BatchAchievementsAPIView(APIView):
challenge_ids = request.data.get('challenges')
families = Family.objects.filter(id__in=family_ids)
challenges = Challenge.objects.filter(id__in=challenge_ids)
results = []
for family in families:
for challenge in challenges:
a = Achievement(family=family, challenge=challenge)
a.save(update_score=False)
a, created = Achievement.objects.get_or_create(family=family, challenge=challenge)
if created:
results.append({
'family': family.name,
'challenge': challenge.name,
'status': 'created'
})
else:
results.append({
'family': family.name,
'challenge': challenge.name,
'status': 'error',
})
for family in families:
family.update_score()
Family.update_ranking()
return Response({'status': 'ok'}, status=status.HTTP_201_CREATED)
return Response({'results': results}, status=status.HTTP_201_CREATED)

View File

@@ -0,0 +1,17 @@
# Generated by Django 5.2.4 on 2025-08-13 20:26
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('family', '0004_remove_challenge_obtained'),
]
operations = [
migrations.AlterUniqueTogether(
name='achievement',
unique_together={('challenge', 'family')},
),
]

View File

@@ -170,6 +170,7 @@ class Achievement(models.Model):
)
class Meta:
unique_together = ('challenge', 'family',)
verbose_name = _('achievement')
verbose_name_plural = _('achievements')

View File

@@ -25,11 +25,6 @@ $(document).ready(function () {
location.hash = this.getAttribute('href')
})
// Ensure we begin in single consumption. Fix issue with TurboLinks and BootstrapJS
document.getElementById("consume_all").addEventListener('click', consumeAll)
})
notes = []
@@ -140,13 +135,25 @@ function consumeAll () {
headers: {
'X-CSRFToken': CSRF_TOKEN
},
success: function () {
success: function (data) {
reset()
addMsg("Défis validés pour les familles!", 'success', 5000)
},
error: function (e) {
reset()
addMsg("Erreur lors de la création des achievements.",'danger',5000)
data.results.forEach(function (result) {
if (result.status === 'created') {
addMsg(
interpolate(gettext('Invalid achievement for challenge %s ' +
'and family %s created.'), [result.challenge, result.family]),
'success',
5000
)
} else {
addMsg(
interpolate(gettext('An achievement for challenge %s ' +
'and family %s already exists.'), [result.challenge, result.family]),
'danger',
8000
)
}
})
}
})
}

View File

@@ -177,25 +177,27 @@ SPDX-License-Identifier: GPL-3.0-or-later
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content border-success">
<div class="modal-header bg-success text-white">
<h5 class="modal-title" id="validationModalLabel">{% trans "Challenge validated" %}</h5>
<h5 class="modal-title" id="validationModalLabel">{% trans "Confirmation" %}</h5>
<button type="button" class="close text-white" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<p><strong>Are you sure you want to validate this challenge?</strong></p>
<p>To have your challenge officially validated, please send a message with:</p>
<ul>
<li>The name of the family</li>
<li>The name of the challenge</li>
<li>A photo or video as proof</li>
</ul>
<p>
<strong>Send it via WhatsApp to:
{% for num in phone_numbers %}
<a href="https://wa.me/{{ num }}" target="_blank">{{ num }}</a>{% if not forloop.last %}, {% endif %}
{% endfor %}
</p>
<p><strong>{% trans "Are you sure you want to validate this challenge?" %}</strong></p>
<p>{% trans "To have your challenge officially validated, please send a message with:" %}</p>
<ul>
<li>{% trans "The name of the family" %}</li>
<li>{% trans "The name of the challenge" %}</li>
<li>{% trans "A photo or video as proof" %}</li>
</ul>
<p>
<strong>{% trans "Send it via WhatsApp to:" %}</strong>
{% if phone_numbers %}"
{% for num in phone_numbers %}
<a href="https://wa.me/{{ num }}" target="_blank">{{ num }}</a>{% if not forloop.last %}, {% endif %}
{% endfor %}
{% endif %}
</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">{% trans "OK" %}</button>
@@ -227,6 +229,10 @@ SPDX-License-Identifier: GPL-3.0-or-later
document.getElementById("consume_all").addEventListener("click", function () {
$('#validationModal').modal('show');
});
$('#validationModal .btn-primary').on('click', function () {
consumeAll();
});
{% if user_family %}
document.getElementById("select_my_family").addEventListener("click", function () {