Skip to content

Commit

Permalink
fix(availability): fix overlapping error (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
GitLukeW authored Sep 18, 2023
1 parent 2b8ff3e commit d6fcc7c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
34 changes: 27 additions & 7 deletions team_production_system/serializers/availability.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d6fcc7c

Please sign in to comment.