Permet au gymnaste de voir les détails de ses Injury et de les modifier

This commit is contained in:
Gregory Trullemans 2024-08-21 11:34:54 +02:00
parent 2b5bd18ae5
commit 3a5b7ac420
4 changed files with 35 additions and 71 deletions

View File

@ -86,6 +86,8 @@ Ajoutez les lignes suivantes :
- Créer l'application sur Heroku
- Ajouter le Buildpacks : heroku/python
- Créer les variables d'environnement :
- ALLOWED_HOSTS : avengers-jarvis.herokuapp.com
- DATABASE_NAME : ultron

View File

@ -1,62 +0,0 @@
from django.test import TestCase, Client
from django.urls import reverse
from jarvis.people.models import Gymnast
from jarvis.followup.models import Chrono, ChronoDetails
from django.contrib.auth.models import User
class ChronoViewTests(TestCase):
def setUp(self):
# Create a user for authentication
self.user = User.objects.create_user(username='testuser', password='12345')
self.client = Client()
self.client.login(username='testuser', password='12345')
# Create a Chrono and ChronoDetails
self.gymnast = Gymnast.objects.create(
last_name="Pauchou", first_name="Fred", birthdate="1987-07-03", gender=0
)
self.chrono = Chrono.objects.create(
gymnast=self.gymnast, chrono_type=0, score_type=0, score=15, tof=13
)
def test_remove_jump_chrono_value_success(self):
"""Test removing a chrono detail successfully."""
url = reverse('remove_jump_chrono_value') # Ensure you have named your URL in urls.py
self.chrono_detail = ChronoDetails.objects.create(chrono=self.chrono, order=1, value=9.5)
response = self.client.post(url, {'chrono_id': self.chrono.id, 'order': 1})
self.assertEqual(response.status_code, 200)
self.assertFalse(ChronoDetails.objects.filter(id=self.chrono_detail.id).exists())
def test_remove_jump_chrono_value_not_found(self):
"""Test removing a non-existing chrono detail."""
url = reverse('remove_jump_chrono_value') # Ensure you have named your URL in urls.py
self.chrono_detail = ChronoDetails.objects.create(chrono=self.chrono, order=1, value=9.5)
response = self.client.post(url, {'chrono_id': self.chrono.id, 'order': 999})
self.assertEqual(response.status_code, 404)
# def test_remove_jump_chrono_value_missing_parameters(self):
# """Test the response when required parameters are missing."""
# url = reverse('remove_jump_chrono_value')
# self.chrono_detail = ChronoDetails.objects.create(chrono=self.chrono, order=1, value=9.5)
# response = self.client.post(url, {'chrono_id': self.chrono.id}) # 'order' is missing
# self.assertEqual(response.status_code, 400)
def test_add_jump_chrono_value_success(self):
"""Test adding a new chrono detail successfully."""
url = reverse('add_jump_chrono_value')
response = self.client.post(url, {'chrono_id': self.chrono.id, 'order': 1, 'value': 9.5})
self.assertEqual(response.status_code, 201)
self.assertTrue(ChronoDetails.objects.filter(chrono=self.chrono, order=1, value=9.5).exists())
def test_add_jump_chrono_value_conflict(self):
"""Test adding a chrono detail that already exists."""
ChronoDetails.objects.create(chrono=self.chrono, order=1, value=9.5)
url = reverse('add_jump_chrono_value')
response = self.client.post(url, {'chrono_id': self.chrono.id, 'order': 1, 'value': 9.5})
self.assertEqual(response.status_code, 409)
# def test_add_jump_chrono_value_missing_parameters(self):
# """Test the response when required parameters are missing."""
# url = reverse('add_jump_chrono_value')
# response = self.client.post(url, {'chrono_id': self.chrono.id, 'order': 1}) # 'value' is missing
# self.assertEqual(response.status_code, 400)

View File

@ -43,32 +43,26 @@ def jump_chrono_details(request, chrono_id):
Args:
chrono_id (int) identifiant chrono
"""
# Using prefetch_related to optimize database access for related details
chrono = get_object_or_404(Chrono.objects.prefetch_related('details'), pk=chrono_id)
# Access control for non-superusers
if not request.user.is_superuser and (
request.session.has_key("available_gymnast") and
chrono.gymnast.id not in request.session["available_gymnast"]
):
return chrono_listing(request)
# Aggregate values from details
details = chrono.details.all()
sum_value = details.aggregate(total=Sum("value"))['total']
mean_value = details.aggregate(mean=Avg("value"))['mean']
min_value = details.aggregate(min=Min("value"))['min']
max_value = details.aggregate(max=Max("value"))['max']
# Update chrono score if different
if chrono.score != sum_value:
chrono.score = sum_value
if chrono.score_type == 0:
chrono.tof = Chrono.compute_tof(sum_value)
chrono.save()
# Calculate chart values
chart_min_value = mean_value - (min_value / 20)
chart_max_value = mean_value + (max_value / 20)
@ -153,7 +147,7 @@ def remove_jump_chrono_value(request):
order = request.POST.get("order")
if not chrono_id or not order:
return HttpResponse(400, "Missing required parameters.")
return HttpResponse(status=400)
chrono = get_object_or_404(Chrono, pk=chrono_id)
deleted, _ = ChronoDetails.objects.filter(chrono=chrono, order=order).delete()
@ -175,7 +169,7 @@ def add_jump_chrono_value(request):
value = request.POST.get("value")
if not chrono_id or not order or value is None:
return HttpResponse(400, "Missing required parameters.")
return HttpResponse(status=400)
chrono = get_object_or_404(Chrono, pk=chrono_id)
row, created = ChronoDetails.objects.get_or_create(
@ -185,7 +179,7 @@ def add_jump_chrono_value(request):
if created:
return HttpResponse(status=201)
return HttpResponse(status=409) # 409 Conflict
return HttpResponse(status=409)
@login_required
@ -274,6 +268,32 @@ def average_jump_chrono_details_between_two_date(
return render(request, "chronos/list_details.html", context)
# @require_http_methods(["GET"])
# def get_chrono_detail_distinct(request, gymnast_id, season=None):
# """Retrieves all distinct seasons for which the gymnast has detailed chronos.
# Args:
# gymnast_id (int) Identifiant d'un gymnaste
# """
# get_object_or_404(Gymnast, pk=gymnast_id)
# if season:
# result_list = list(
# Chrono.objects.filter(gymnast_id=gymnast_id, season=season)
# .values_list("week_number", flat=True)
# .distinct()
# .order_by("week_number")
# )
# else:
# result_list = list(
# Chrono.objects.filter(gymnast_id=gymnast_id)
# .values_list("season", flat=True)
# .distinct()
# .order_by("season")
# )
# return JsonResponse(result_list, safe=False)
@require_http_methods(["GET"])
def get_chrono_detail_distinct_season(request, gymnast_id):
"""Retrieves all distinct seasons for which the gymnast has detailed chronos.

View File

@ -80,6 +80,8 @@ def injury_create_or_update(request, injury_id=None, gymnast_id=None):
if not request.user.is_superuser and (
request.session.has_key("available_gymnast")
and injury.gymnast.id not in request.session["available_gymnast"]
) and (
injury.gymnast.id != request.user.gymnast.id
):
return injury_listing(request)
data = {
@ -151,6 +153,8 @@ def injury_details(request, injury_id):
if not request.user.is_superuser and (
request.session.has_key("available_gymnast")
and injury.gymnast.id not in request.session["available_gymnast"]
) and (
injury.gymnast.id != request.user.gymnast.id
):
return injury_listing(request)