From 29347268992d6709467a29912fcbc0f26d7a2986 Mon Sep 17 00:00:00 2001 From: GitLukeW <102833183+GitLukeW@users.noreply.github.com> Date: Mon, 18 Sep 2023 17:16:31 -0400 Subject: [PATCH] Deploy 09182023 (#143) * updated cors with staging (#136) (#138) (#139) * Sessionserializer (#141) * updated cors with staging (#136) (#138) * added mentor to session serializer --------- Co-authored-by: GitLukeW <102833183+GitLukeW@users.noreply.github.com> Co-authored-by: Laura-D-Andrews * fix(availability): fix overlapping error (#142) --------- Co-authored-by: Laura Andrews <125932649+Laura-D-Andrews@users.noreply.github.com> Co-authored-by: Laura-D-Andrews --- .../serializers/availability.py | 34 +++++++++++++++---- team_production_system/serializers/session.py | 2 +- .../test_availability_list_create.py | 23 +++++++++++++ .../test_availability_create_list_view.py | 2 +- 4 files changed, 52 insertions(+), 9 deletions(-) diff --git a/team_production_system/serializers/availability.py b/team_production_system/serializers/availability.py index a4fc8f5..edfb406 100644 --- a/team_production_system/serializers/availability.py +++ b/team_production_system/serializers/availability.py @@ -1,6 +1,5 @@ from rest_framework import serializers from django.utils import timezone -from datetime import timedelta from team_production_system.models import ( Mentor, Availability, @@ -20,17 +19,38 @@ def create(self, validated_data): user=self.context['request'].user) start_time = validated_data['start_time'] end_time = validated_data['end_time'] - overlapping_start = Availability.objects.filter( + # Check if start time is between a start time and end time of + # an existing availability + overlapping_condition1 = Availability.objects.filter( mentor=mentor, - start_time__lte=end_time, - start_time__gte=start_time + timedelta(minutes=1) + start_time__lt=start_time, + end_time__gt=start_time, ).exists() - overlapping_end = Availability.objects.filter( + # Check if end time is between a start time and end time of + # an existing availability + overlapping_condition2 = Availability.objects.filter( mentor=mentor, - end_time__gte=start_time + timedelta(minutes=1), + start_time__lt=end_time, + end_time__gt=end_time + ).exists() + # Check if start time is between the new start_time and new end_time + overlapping_condition3 = Availability.objects.filter( + mentor=mentor, + start_time__gte=start_time, + start_time__lt=end_time + ).exists() + # Check if end time is between the new start_time and new end_time + overlapping_condition4 = Availability.objects.filter( + mentor=mentor, + end_time__gt=start_time, end_time__lte=end_time ).exists() - availability_overlap = overlapping_start or overlapping_end + + availability_overlap = ( + overlapping_condition1 + or overlapping_condition2 + or overlapping_condition3 + or overlapping_condition4) if not availability_overlap: availability = Availability.objects.create( mentor=mentor, **validated_data) diff --git a/team_production_system/serializers/session.py b/team_production_system/serializers/session.py index 4ed40ef..c43eab0 100644 --- a/team_production_system/serializers/session.py +++ b/team_production_system/serializers/session.py @@ -14,7 +14,7 @@ class SessionSerializer(serializers.ModelSerializer): class Meta: model = Session - fields = ('pk', 'mentor_first_name', 'mentor_last_name', + fields = ('pk', 'mentor', 'mentor_first_name', 'mentor_last_name', 'mentor_availability', 'mentee', 'mentee_first_name', 'mentee_last_name', 'start_time', 'end_time', 'status', 'session_length',) diff --git a/team_production_system/tests/end_to_end_tests/test_availability_list_create.py b/team_production_system/tests/end_to_end_tests/test_availability_list_create.py index b4947c0..573c915 100644 --- a/team_production_system/tests/end_to_end_tests/test_availability_list_create.py +++ b/team_production_system/tests/end_to_end_tests/test_availability_list_create.py @@ -165,3 +165,26 @@ def test_create_availability_with_end_time_before_start_time(self): self.assertEqual(str(response.data['end_time'][0]), 'End time must be in the future.') self.assertEqual(Availability.objects.count(), 2) + + def test_create_availability_inside_existing_availability(self): + """ + Test that a POST request to create a new Availability with a start time + and end time that are inside an existing Availability returns a status + code of 400 BAD REQUEST. + """ + # Authenticate as the Mentor + self.client.force_authenticate(user=self.user) + + availability_data = { + 'start_time': self.availability1.start_time + + timezone.timedelta(minutes=15), + 'end_time': self.availability1.end_time - + timezone.timedelta(minutes=15), + 'mentor': self.mentor.pk + } + response = self.client.post('/availability/', + availability_data, format='json') + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + self.assertEqual(str(response.data[0]), + 'Input overlaps with existing availability.') + self.assertEqual(Availability.objects.count(), 2) diff --git a/team_production_system/tests/unit_tests/views/test_availability_create_list_view.py b/team_production_system/tests/unit_tests/views/test_availability_create_list_view.py index 76f1c64..976a213 100644 --- a/team_production_system/tests/unit_tests/views/test_availability_create_list_view.py +++ b/team_production_system/tests/unit_tests/views/test_availability_create_list_view.py @@ -24,7 +24,7 @@ def setUp(self): end_time=timezone.now() + timezone.timedelta(hours=2) ) - @patch('team_production_system.views.timezone') + @patch('django.utils.timezone') def test_get_availability_list(self, mock_timezone): """ Test that the get method returns a list of Availabilities