Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Count gaps in ScheduleItem duration #619

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions wafer/schedule/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,26 +333,24 @@ def __str__(self):
def get_duration(self):
"""Return the total duration of the item.

This is the sum of all the slot durations."""
# This is intended for the pentabarf xml file
# It will do the wrong thing if the slots aren't
# contigious, which we should address sometime.
slots = list(self.slots.all())
result = {'hours': 0, 'minutes': 0}
if slots:
for slot in slots:
dur = slot.get_duration()
result['hours'] += dur['hours']
result['minutes'] += dur['minutes']
# Normalise again
hours, result['minutes'] = divmod(result['minutes'], 60)
result['hours'] += hours
return result
This is the time from start to end, including any discontinuities
between slots."""
minutes = self.get_duration_minutes()
hours, minutes = divmod(minutes, 60)
return {
'hours': hours,
'minutes': minutes,
}

def get_duration_minutes(self):
"""Return the duration in total number of minutes."""
duration = self.get_duration()
return int(duration['hours'] * 60 + duration['minutes'])
slots = list(self.slots.all())
if not slots:
return 0
start = slots[0].get_start_time()
end = slots[-1].end_time
duration = end - start
return duration.total_seconds() // 60

@property
def guid(self):
Expand Down
39 changes: 39 additions & 0 deletions wafer/schedule/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,45 @@ def test_invalid_slot_block(self):
self.assertRaises(ValidationError, slot4.clean)


class ScheduleItemTests(TestCase):
def setUp(self):
utc = timezone.utc
self.blocks = [
ScheduleBlock.objects.create(
start_time=D.datetime(2013, 9, 22, 9, 0, 0, tzinfo=utc),
end_time=D.datetime(2013, 9, 22, 19, 0, 0, tzinfo=utc)),
]
self.slots = [
Slot.objects.create(
start_time=D.datetime(2013, 9, 22, 9, 0, 0, tzinfo=utc),
end_time=D.datetime(2013, 9, 22, 9, 20, 0, tzinfo=utc)),
Slot.objects.create(
start_time=D.datetime(2013, 9, 22, 9, 30, 0, tzinfo=utc),
end_time=D.datetime(2013, 9, 22, 9, 50, 0, tzinfo=utc)),
]
self.venues = [
Venue.objects.create(order=1, name='Venue 1'),
]
self.pages = make_pages(1)

def test_duration_minutes(self):
"""Test events spanning multiple slots with a gap in between"""
item = make_items(self.venues, self.pages)[0]
# Single Slot
item.slots.add(self.slots[0])
self.assertEqual(item.get_duration_minutes(), 20)
# Spanning 2 slots with a gap in-between
item.slots.add(self.slots[1])
self.assertEqual(item.get_duration_minutes(), 50)

def test_get_duration(self):
"""Test ScheduleItem.get_duration()"""
item = make_items(self.venues, self.pages)[0]
# Single Slot
item.slots.add(self.slots[0])
self.assertEqual(item.get_duration(), {'minutes': 20, 'hours': 0})


class LastUpdateTests(TestCase):

def setUp(self):
Expand Down