From 3a5b7ac420636dd67e7e2c66292939f88875e454 Mon Sep 17 00:00:00 2001 From: Gregory Trullemans Date: Wed, 21 Aug 2024 11:34:54 +0200 Subject: [PATCH] =?UTF-8?q?Permet=20au=20gymnaste=20de=20voir=20les=20d?= =?UTF-8?q?=C3=A9tails=20de=20ses=20Injury=20et=20de=20les=20modifier?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 + jarvis/followup/tests_views.py | 62 -------------------------- jarvis/followup/views_chrono.py | 38 ++++++++++++---- jarvis/followup/views_physiological.py | 4 ++ 4 files changed, 35 insertions(+), 71 deletions(-) delete mode 100644 jarvis/followup/tests_views.py diff --git a/README.md b/README.md index fe948ac..c966cb1 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/jarvis/followup/tests_views.py b/jarvis/followup/tests_views.py deleted file mode 100644 index 9f17059..0000000 --- a/jarvis/followup/tests_views.py +++ /dev/null @@ -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) \ No newline at end of file diff --git a/jarvis/followup/views_chrono.py b/jarvis/followup/views_chrono.py index daaa204..0a30de0 100644 --- a/jarvis/followup/views_chrono.py +++ b/jarvis/followup/views_chrono.py @@ -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. diff --git a/jarvis/followup/views_physiological.py b/jarvis/followup/views_physiological.py index b6f3494..b07b43c 100644 --- a/jarvis/followup/views_physiological.py +++ b/jarvis/followup/views_physiological.py @@ -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)